Page 1 of 1
String Substitution: replace at one go & particular letters?
Posted: 28 Oct 2012 06:47
by tinfanide
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.
Re: String Substitution: replace at one go & particular lett
Posted: 28 Oct 2012 06:59
by foxidrive
The better the information is the better info you will get.
Re: String Substitution: replace at one go & particular lett
Posted: 28 Oct 2012 08:00
by abc0502
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.
Re: String Substitution: replace at one go & particular lett
Posted: 31 Oct 2012 06:00
by tinfanide
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.