Wednesday, 3 April 2019

batch script for loop and if statement not interacting properly

For archiving purposes, I have one directory full of files called note_1.txt, note_3.txt, note_4.txt, etc. I am writing a script to find the biggest number N among those files, and rename a new note.txt file to note_N+1.txt.


I am using a batch for loop for the first time and can't make it work properly. I tried replacing % by ! but I am not sure I understand how it works.


SETLOCAL ENABLEDELAYEDEXPANSION
SET "maxfile=1"
for /f %%i in ('dir /b note_*.txt') do (
SET archivename=%%~ni
SET archivenumber=%archivename:~5%
if %archivenumber% GTR %maxfile% SET /a maxfile=%archivenumber%+1
)
echo %maxfile%
ENDLOCAL

No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...