String Substitution: replace at one go & particular letters?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

String Substitution: replace at one go & particular letters?

#1 Post by tinfanide » 28 Oct 2012 06:47

Code: Select all

@ECHO ON
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "tokens=*" %%A IN ('DIR /S /B') DO (
   IF NOT %%~xA==.bat (
      SET "old=%%A"
      SET "new=%%A"
      SET new=!new:s=S!
      SET new=!new:SFile=sFile!      
      MOVE "!old!" "!new!"
   )
)

PAUSE>NUL


I wonder how I can
1. replace all the letters at one go (instead of having a few lines of SET...)
2. more importantly, if I want to replace a single "e" only, (for example, the file names are something like "sFile s01", "sFile s02"), it will also replace the "s" in "sFile". How can I only replace the "s" in "s01"?

Thanks in advance.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: String Substitution: replace at one go & particular lett

#2 Post by foxidrive » 28 Oct 2012 06:59

The better the information is the better info you will get.

abc0502
Posts: 1007
Joined: 26 Oct 2011 22:38
Location: Egypt

Re: String Substitution: replace at one go & particular lett

#3 Post by abc0502 » 28 Oct 2012 08:00

:? From the code, It seems he wants to rename all files in folder and sub-folders by replacing a single letter in it's name ... or move :? :?


Edit:

That should do it:

Code: Select all

@ECHO ON
SETLOCAL ENABLEDELAYEDEXPANSION

FOR /F "tokens=*" %%A IN ('DIR /S /B') DO (
   IF NOT %%~xA==.bat (
      ren "%%A" "* R*"
   )
)

PAUSE>NUL


Note that the R in "* R*" is the replace of the "s" before the numbers.
you can change it to any thing else.

The starts * is to match the pattern of the file name, it till the command to rename the files with the same strings and when it comes to the first string after the first space it replace it with the "R" and then continue with the same strings of the original name including the extension.

tinfanide
Posts: 117
Joined: 05 Sep 2011 09:15

Re: String Substitution: replace at one go & particular lett

#4 Post by tinfanide » 31 Oct 2012 06:00

abc0502 wrote:Note that the R in "* R*" is the replace of the "s" before the numbers.
you can change it to any thing else.

The starts * is to match the pattern of the file name, it till the command to rename the files with the same strings and when it comes to the first string after the first space it replace it with the "R" and then continue with the same strings of the original name including the extension.


Your codes are amazing, making use of the pattern "*SPACE NUMBER*".
Just haven't thought of the use of regular expressions.
Thanks.

Post Reply