Replace string in filename, handling of exclamation marks!

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bangaio64
Posts: 2
Joined: 23 Nov 2016 16:38

Replace string in filename, handling of exclamation marks!

#1 Post by bangaio64 » 23 Nov 2016 20:12

Was writing a simple script for replacing a string for another in filenames (in the provided folder), but I noticed it failed if the strings include "!". I try not to use any special characters in my filenames, but the exclamation marks are not all that rare.

The important bits: (I can paste the whole code if needed)

Code: Select all

::usage: strep <folder> <old_string> <new_string>

SETLOCAL DISABLEDELAYEDEXPANSION
::test if ! present
set "_test="
for /f "delims=!" %%A in ("%_search%%_replace%") do set "_test=%%A"
IF "%_test%" NEQ "%_search%%_replace%" set "_excFlag=1"

FOR /F "tokens=*" %%A IN (' dir /b %_dirOpt% "*%_search%*" ') do (
   set "_file=%%A"
   SETLOCAL ENABLEDELAYEDEXPANSION
   IF "%_excFlag%" == "1" (call :renamer _file _search _replace) ELSE REN "!_file!" "!_file:%_search%=%_replace%!"
   ENDLOCAL
   )
   
:renamer
set "_fileR=!%~1!"
set "_searchR=!%~2!"
set "_replaceR=!%~3!"

SETLOCAL DISABLEDELAYEDEXPANSION
call set "_new=%%_fileR:%_searchR%=%_replaceR%%%"
REN "%_fileR%" "%_new%"
ENDLOCAL
exit /b


This works, at least if the strings don't include both "!" and "%". But surely there's a more elegant way to do this, maybe escaping the "!" inside the variable, but I can't figure it out.

Thanks.

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Replace string in filename, handling of exclamation marks!

#2 Post by dbenham » 26 Nov 2016 15:15

The find/replace feature of batch variable expansion is useful, but it has a number of limitations that simply cannot be avoided:

  • The search always ignores case
  • The search cannot include the equal character (=)
  • The search cannot begin with an asterisk literal (*)
  • If using delayed expansion, then the search cannot begin with !, and the replacement cannot contain ! anywhere
  • If using normal expansion, then the search cannot begin with %, and the replacement cannot contain % anywhere

There may be some more esoteric limitations that I have forgotten.

The above limitations can be a problem when trying to rename files (although * should not be an issue, since it cannot be used in a name anyway).

There is no way to escape ! or % within a find/replace operation - so there is no elegant method to do what you want with pure batch.

My JREN.BAT regular expression renaming utility eliminates these issues, though you will need to know how to use regular expressions. If you want to do a literal find/replace, then regex meta-characters will need to be escaped. For example a dot (.) would be escaped as (\.)

JREN is pure script (hybrid batch/JScript) that runs natively on any Windows machine from XP onward - no 3rd party exe file required. Full documentation is available via JREPL /? from the command line.

I'm not sure exactly what you are trying to achieve, but below is one simple example of usage:

Remove ! characters from all .txt files within the c:\myFolder folder:

Code: Select all

jren "!" "" /p "c:\myFolder" /fm *.txt

Since JREPL is a batch script, you would need to use CALL JREPL if you put the command within another batch script.


Dave Benham

Post Reply