I'm trying to write a batch file to sort some pdf to respective directory. eg. xxx1_date.pdf to DIR1, xxx2_date.pdf to DIR2. Below are my code taken from one a script I found in this site. Thank you.
SetLocal EnableDelayedExpansion
dir /b /a-d *.pdf > file.log
for /f "tokens=1* delims=_" %%f in (file.log) do (
set line=%%f
call :processToken
)
goto :eof
:processToken
for /f "tokens=1* delims=_" %%a in ("%line%") do (
IF "%%a"=="04693139" move %%a* DIR1
IF "%%a"=="34051646" move %%a* DIR2
)
if not "%line%" == "" goto :processToken
goto :eof
CodePudding user response:
In your code, file.log should contain lines, eg:
xxx1_date1.pdf
xxx2_date2.pdf
The first loop would assign xxx1 to %%f, then to line and process it; then
assign xxx2 to %%f, then to line and process that.
"Token 1" is the string up to the first _ (the delimiter) and is assigned to %%f.
"Token *" is the string after the first _ (the delimiter) and is assigned to %%g.
Since line contains xxx1 and xxx2, the next for will assign xxx1, xxx2 to %%a and nothing to %%b since the delimiter is again _.
Since neither xxx1 nor xxx2 matches to two date strings, the move will not take place.
Regardless, line is not being changed and is not empty, so the if not ... will always be true, and you will enter an endless loop.
Now you could fix your code once you realise what portions of you filename will appear where, or you could do the task by simply executing
move *_04693139.pdf DIR1
move *_34051646.pdf DIR2
