Change first character in filename

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
msabeal
Posts: 4
Joined: 02 Sep 2010 08:03

Change first character in filename

#1 Post by msabeal » 02 Sep 2010 08:57

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Change first character in filename

#2 Post by orange_batch » 02 Sep 2010 09:26

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Change first character in filename

#3 Post by ghostmachine4 » 02 Sep 2010 19:18

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

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

Re: Change first character in filename

#4 Post by ghostmachine4 » 02 Sep 2010 19:20

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".

msabeal
Posts: 4
Joined: 02 Sep 2010 08:03

Re: Change first character in filename

#5 Post by msabeal » 03 Sep 2010 00:23

@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.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Change first character in filename

#6 Post by orange_batch » 03 Sep 2010 02:13

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.

msabeal
Posts: 4
Joined: 02 Sep 2010 08:03

Re: Change first character in filename

#7 Post by msabeal » 03 Sep 2010 02:30

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

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Change first character in filename

#8 Post by orange_batch » 03 Sep 2010 02:47

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.

msabeal
Posts: 4
Joined: 02 Sep 2010 08:03

Re: Change first character in filename

#9 Post by msabeal » 03 Sep 2010 03:18

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

Post Reply