I have a little loop where I search for all .txt files and pull a number from them before doing a calculation on it and renaming the file.
The original filenames look like:
Filename~0001.txt
I am then pulling 0001, performing a calculation and renaming them to:
Filename-0001__0_mins_1_secs.txt
This works perfectly fine, but the annoyance I'm running into is that in a folder that already contains renamed files it still tries to process them even though there is no ~ in them. Here's a snippet of my code:
Code: Select all
for /F "tokens=1,2 delims=~" %%G in ('dir /b *.txt') do (
set FilenameWithoutNumber=%%G
set NumberWithExtension=%%H
rem Right here is a bunch of calculations which I have removed
ren !FilenameWithoutNumber!~!NumberWithExtension! !FilenameWithoutNumber!-!FileSpecificNumber!__!TotalTime!.txt
echo Renamed !FilenameWithoutNumber!~!NumberWithExtension! to !FilenameWithoutNumber!-!FileSpecificNumber!__!TotalTime!.txt
)
Which correctly renames the files that have ~0001 but gives an error on the ones that have already been renamed to -0001__0_mins_1_secs.txt.
Here's the output:
Code: Select all
Missing operator.
The syntax of the command is incorrect.
Renamed Filename-0001__1_mins_0_secs.txt~ to Filename-0001__1_mins_0_secs.txt-~0,4__208_mins_20_secs
.txt
This isn't a huge deal as the previously renamed files aren't actually affected (since the rename filename input ends up being wrong), but it would be better if they were just ignored all together. Without doing anything complicated, is there some simple way I'm missing that I can modify my For Loop to skip processing the ones that don't contains a ~? Thanks in advance!