Page 1 of 1

Change first character in filename

Posted: 02 Sep 2010 08:57
by msabeal
Hi,

Need help to replace one character in filename, but only the first.

In Explorer the first character its a square and in DOS its ?. Now I want to change it to O, because I can´t move this file further to another directory with the software I want to use (Opalis Integration server).

The filename look like this
"square"uv10FEX.xml
"square"uv10FOA.xml
"square"uv10REG.xml
..
..

I want it like this
Ouv10FEX.xml
Ouv10FOA.xml
Ouv10REG.xml
..
..

Regards
Benny

Re: Change first character in filename

Posted: 02 Sep 2010 09:26
by orange_batch

Code: Select all

@echo off&setlocal enabledelayedexpansion

for %%x in ("C:\...\Target Folder\*.xml") do (
set "name=%%~nxx"
ren "%%~dpx?!name:~1!" O!name:~1!
)
Image

Re: Change first character in filename

Posted: 02 Sep 2010 19:18
by ghostmachine4
here's a vbscript you can use

Code: Select all

Set objFS = CreateObject("Scripting.FileSystemObject")
strFolder="c:\test"
Set objFolder = objFS.GetFolder(strFolder)
Set objRE = New RegExp
objRE.IgnoreCase = False
objRE.Pattern = "uv\d+[A-Z]{3}.xml"
For Each strFile In objFolder.Files
   If objFS.GetExtensionName(strFile) = "xml" Then      
      strFileName = strFile.Name
       Set Matches = objRE.Execute(strFile.Name)
       For Each Match in Matches   ' Iterate Matches collection.                       
           strFile.Name = "0"&Match
       Next   
   End If   
Next


on the command line

Code: Select all

c:\test> cscript //nologo myscript.vbs

Re: Change first character in filename

Posted: 02 Sep 2010 19:20
by ghostmachine4
orange_batch wrote:

Code: Select all

for %%x in ("C:\...\Target Folder\*.xml") do (


this may change other xml files that doesn't have "square".

Re: Change first character in filename

Posted: 03 Sep 2010 00:23
by msabeal
@echo off&setlocal enabledelayedexpansion

for %%x in ("C:\...\Target Folder\*.xml") do (
set "name=%%~nxx"
ren "%%~dpx?!name:~1!" O!name:~1!
)



This script takes all *.xml files, even if they starts with other character, only file that start with "square", shall be changed.

There are a lot of files that have the same beginning except the first character.

Re: Change first character in filename

Posted: 03 Sep 2010 02:13
by orange_batch
Mm sorry I didn't fully test it.

I can't help you if you don't post the actual square character causing this problem. I think it's solvable using some hack with TYPE.

Re: Change first character in filename

Posted: 03 Sep 2010 02:30
by msabeal
Some explanation about this problem.

This sign (Square) is the swedish character Ö. A file has been received in our enviroment on a ftp server. In the translation, it change to this square. I put one filename in here (from Explorer), I hope it will help.

�UV10FEX.xml

Re: Change first character in filename

Posted: 03 Sep 2010 02:47
by orange_batch
Okay, DOS has serious problems with putting unicode into a batch file, so do this.

Copy this � character. Open Command Prompt (MS-DOS), and type:

Code: Select all

set char=


Right click the window, paste and press enter.

Save this batch somewhere:

Code: Select all

@echo off&setlocal enabledelayedexpansion

for %%x in ("C:\...\Target Folder\%char%*.txt") do (
set "name=%%~nxx"
ren "%%x" O!name:~1!
)


Change the Target Folder, it's similar to before just slightly modified.

Navigate to where the batch is and run it.

Re: Change first character in filename

Posted: 03 Sep 2010 03:18
by msabeal
orange_batch wrote:Okay, DOS has serious problems with putting unicode into a batch file, so do this.

Copy this � character. Open Command Prompt (MS-DOS), and type:

Code: Select all

set char=


Right click the window, paste and press enter.

Save this batch somewhere:

Code: Select all

@echo off&setlocal enabledelayedexpansion

for %%x in ("C:\...\Target Folder\%char%*.txt") do (
set "name=%%~nxx"
ren "%%x" O!name:~1!
)


Change the Target Folder, it's similar to before just slightly modified.

Navigate to where the batch is and run it.


Thanks !!

Now it works fine, have try other file name to...

Regards Benny