I have this simple batch file I'd like to process files in subdirectories. I've played around with the for /R
but I can't seem to get it to work. Any help would be very appreciated.
Thank you.
Here is the batch I have
@echo off
setlocal EnableDelayedExpansion
for %%A in ("*.mkv") do (
echo file found %%A
for /f "tokens=1,2 delims=[im" %%D in ("%%~nA") do set "shortname=%%D[%%E"
echo short name !shortname! >>J:\list.txt
)
You misunderstand what the string between =1
and "
of the delims
option does.
It is not a delimiter-string.
It is a set of characters , each one of which is a delimiter.
SET "delimiterstring=[im"
for %%A in ("*.mkv") do (
echo file found %%A
for /f "tokens=1,2,3 delims=[im" %%D in ("%%~nA") do ECHO 1="%%D" 2="%%E" 3="%%F"
set "shortname=%%~nA"
SET "endpart=!shortname:*%delimiterstring%=!"
ECHO lopped shortname="!endpart!"
call SET "shortname=%%shortname:%delimiterstring%!endpart!=%%"
echo short name "!shortname!"
)
Note that the for /f…%%D
sets tokens 1,2 & 3 for the filename Thunderbolts [imdbid-tt20969586].mkv
. The string is interpreted as [stringofdelimiters][stringofdata], repeated. Since '[','i','m'
are defined as the delimiter characters, the tokens are generated as 1="Thunderbolts " 2="db" 3="d-tt20969586]"
Note also the Space at the end of the first token. The delims
option does not permit a space as a delimiter, other than as the final character of the delims=
string.
Your approach would thus also fail if "Thunderbolts " contained '[','i','m'
The remainder indicates one way to process the string. It sets endpart
by replacing all characters up to and including delimiterstring
with nothing then setsshortname
to its initial value with [delimiterstring+endpart] replaced by nothing using the call
parsing trick (used thousands of times on SO) to arrange the desired operation.
You should be careful about any "poison" characters (character with a special meaning to cmd
such as !%~&^()
Note that you could also change delimiterstring
to include the space in the desired position to remove the end-space from "Thunderbolts "