Occasionally I have to rename masses of files resp. a part of their filename.
Only pattern REPLACEMENTS are should take place. NO inserts, NO deletions and no regular expression replacements.
Just simple replacements.
This should be done from inside a script.
So is there an easy way to rename e.g. the pattern " backup varA " to " (new bkp)"?
A filename like
project25235235 backup varA -A12.pdf
should get the filename
project25235235 (new bkp)-A12.pdf
3rd party tools are acceptable if necessary.
Thank you
Peter
Easiest way to rename parts of filenames? NO regular expressions needed
Moderator: DosItHelp
Re: Easiest way to rename parts of filenames? NO regular expressions needed
If this prints the right command lines, remove ECHO and PAUSE to actually rename the files.
Steffen
Code: Select all
@echo off &setlocal
set "search= backup varA "
set "replace= (new bkp)"
for %%i in ("*%search%*.*") do (
set "fname=%%~ni"
set "fext=%%~xi"
setlocal EnableDelayedExpansion
ECHO ren "!fname!!fext!" "!fname:%search%=%replace%!!fext!"
endlocal
)
PAUSE
Re: Easiest way to rename parts of filenames? NO regular expressions needed
Hello Steffen,
thank you for the code. It works for most of the cases.
However when the source file pattern contains german Umlaute (like ÄÜÖ) it doesn't work.
Example
set "search= Änderung rel3 "
set "replace= change old"
.....
Any way to handle this as well?
Peter
thank you for the code. It works for most of the cases.
However when the source file pattern contains german Umlaute (like ÄÜÖ) it doesn't work.
Example
set "search= Änderung rel3 "
set "replace= change old"
.....
Any way to handle this as well?
Peter
Re: Easiest way to rename parts of filenames? NO regular expressions needed
Depends on which encoding you used to save the script. In a German environment the codepage the CMD uses is 850. If you can manage to save your script in CP850 it should be working without any other setting.
Otherwise, open your script in the Windows editor (notepad.exe), write line ...
>nul chcp 1252
... somewhere at the beginning of your script, menue "save as", make sure encoding "ANSI" is selected, and save/overwrite.
Steffen
Otherwise, open your script in the Windows editor (notepad.exe), write line ...
>nul chcp 1252
... somewhere at the beginning of your script, menue "save as", make sure encoding "ANSI" is selected, and save/overwrite.
Steffen
Re: Easiest way to rename parts of filenames? NO regular expressions needed
Thank you, it works.
One last question:
I want to apply the script onto another directory and wrote
for %%i in ("D:\\indata\\new\\*%search%*.*") do ( ....)
resp
for %%i in ("D:\indata\new\*%search%*.*") do ( ....)
But none of the two methods work.
How do I let the script work with other directories otherwise?
One last question:
I want to apply the script onto another directory and wrote
for %%i in ("D:\\indata\\new\\*%search%*.*") do ( ....)
resp
for %%i in ("D:\indata\new\*%search%*.*") do ( ....)
But none of the two methods work.
How do I let the script work with other directories otherwise?
Re: Easiest way to rename parts of filenames? NO regular expressions needed
To avoid that the replacement is applied to possible findings in the path I recomment you to change the current working direcory using PUSHD and POPD.
Steffen
Code: Select all
pushd "D:\indata\new"
for %%i ... do (
...
)
popd
Re: Easiest way to rename parts of filenames? NO regular expressions needed
It work. Thank you
Where can I find the syntax of the replacement command you used:
"<sourcefilename>:<frompattern>=<replacepattern>"
When I type
ren /?
this is not mentioned
Where can I find the syntax of the replacement command you used:
"<sourcefilename>:<frompattern>=<replacepattern>"
When I type
ren /?
this is not mentioned
Re: Easiest way to rename parts of filenames? NO regular expressions needed
The help message of SET does explain how it works.
Steffen
Steffen