I know how to replace words in a filename using a batch code but removing exclamations doesn't work. Here what I use to remove other characters. echo off
SETLOCAL EnableDelayedExpansion
FOR /f "tokens=*" %%a IN ('DIR /b "*.flv"') DO (
SET Var=%%~na
SET Var=!Var:a=_!
REN "%%a" "!Var!.flv"
)The above code will replace any .flv filename containing the letter "a" with a _, but I need a code that would replace ! with an _. I can't substitue ! in place of the letter "a" because the code itself uses an !. . Any help is appreciated. Thanks.
As nbrane said his method doesn't work with file names with more than one !, here is a method that does. It uses a sneaky trick to implement delayed expansion without using the ! character, and without using the EnableDelayedExpansion option. @echo off
setlocal DisableDelayedExpansionFOR %%a IN (*.flv) DO (
SET Var=%%~na
CALL SET Var=%%Var:!=_%%
CALL REN "%%a" "%%Var%%.flv"
)message edited by klint
What if you put the ! in quotes "!" How do you know when a politician is lying? His mouth is moving.
@guapo: right idea, just needs a boost: There are various ways, but maybe the easiest (and therefore, according to Occam, best) method is like this:
@echo off & setlocal enabledelayedexpansion for /f "tokens=1,2 delims=!" %%a in ('dir /b *.flv') do ( echo ren "%%a^!"%%b %%a_%%b )
other methods involve disabling delayedexpansion, but that gets messy, imo. This will of course fail (as written) if more than one "!" is in the filename-path.
As nbrane said his method doesn't work with file names with more than one !, here is a method that does. It uses a sneaky trick to implement delayed expansion without using the ! character, and without using the EnableDelayedExpansion option. @echo off
setlocal DisableDelayedExpansionFOR %%a IN (*.flv) DO (
SET Var=%%~na
CALL SET Var=%%Var:!=_%%
CALL REN "%%a" "%%Var%%.flv"
)message edited by klint
It's a quirk of CMD's command parsing, caused by the CALL command forcing another round of parsing. The "%%" are parsed to just "%" during the first pass, and that leads to a valid variable name. The now valid variable is expanded while executing the CALL statement.
http://www.computing.net/howtos/sho...How To Ask Questions The Smart Way
message edited by Razor2.3
Yes (14) | ![]() | |
No (14) | ![]() | |
I don't know (15) | ![]() |