Easiest way to rename parts of filenames? NO regular expressions needed

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Easiest way to rename parts of filenames? NO regular expressions needed

#1 Post by pstein » 29 Aug 2020 07:14

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easiest way to rename parts of filenames? NO regular expressions needed

#2 Post by aGerman » 29 Aug 2020 07:32

If this prints the right command lines, remove ECHO and PAUSE to actually rename the files.

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
Steffen

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Easiest way to rename parts of filenames? NO regular expressions needed

#3 Post by pstein » 30 Aug 2020 06:31

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easiest way to rename parts of filenames? NO regular expressions needed

#4 Post by aGerman » 30 Aug 2020 08:13

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

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Easiest way to rename parts of filenames? NO regular expressions needed

#5 Post by pstein » 30 Aug 2020 09:47

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?

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easiest way to rename parts of filenames? NO regular expressions needed

#6 Post by aGerman » 30 Aug 2020 10:04

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.

Code: Select all

pushd "D:\indata\new"

for %%i ... do (
  ...
)

popd
Steffen

pstein
Posts: 125
Joined: 09 Nov 2011 01:42

Re: Easiest way to rename parts of filenames? NO regular expressions needed

#7 Post by pstein » 31 Aug 2020 07:59

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

aGerman
Expert
Posts: 4654
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Easiest way to rename parts of filenames? NO regular expressions needed

#8 Post by aGerman » 02 Sep 2020 16:35

The help message of SET does explain how it works.

Steffen

Post Reply