2

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
Antoine
  • 21
  • Antoine - You were real close man so I added a couple example below to help you out get that trivial task resolved and also provided some references to explain this a little better as well. It might help if you echo the variables within the loop to a log file for example . . . echo !archivenumber!>>log.txt for each variable and then look over that log after it runs. If you echo those same variables with the % signs rather than the ! and look over the log, now you will see the differences. Let me know if I can help clarify further but that should be all there is to this. – Vomit IT - Chunky Mess Style Sep 11 '18 at 01:13

1 Answers1

1

I just added the ! to the variables within the FOR loop to ensure they are all expanded at execution time within the loop to ensure new set values are read accordingly to help get the final !maxfile! value as per each loop iteration.

Furthermore, I added the CD /D "%%~F0" to the line above the start of the FOR loop to ensure the directory is changed to the directory the script resides since you're not explicitly specifying the directory in your command example but I added an explicit example script below as well.

Batch Script (Implicit)

SETLOCAL ENABLEDELAYEDEXPANSION
SET "maxfile=1"
cd /d "%%~F0"
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

Batch Script (Explicit)

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

Further Resources

  • EnableDelayedExpansion

    Delayed Expansion will cause variables within a batch file to be expanded at execution time rather than at parse time, this option is turned on with the SETLOCAL EnableDelayedExpansion command.


    When delayed expansion is in effect, variables can be immediately read using !variable_name! you can also still read and use %variable_name% that will show the initial value (expanded at the beginning of the line).

  • For

    Variable Substitutions (FOR /?)

    In addition, substitution of FOR variable references has been enhanced. You can now use the following optional syntax:

    %~I         - expands %I removing any surrounding quotes (")
    %~fI        - expands %I to a fully qualified path namey
    
  • @Antoine - See Accepting an Answer to ensure you understand how that works for the solution you find for any answers that help you resolve your problem by simply checking the little gray check box to the upper left side of the answer that resolves your problem assuming one does and turn it green to show it's the answer you accepted since that's how that is supposed to work; you ask a question, you get answers, and if you get an answer that works for your needs, you simply accept that answer. – Vomit IT - Chunky Mess Style Sep 11 '18 at 01:13