Acabei de adicionar o !
às variáveis dentro do FOR
loop para garantir que elas sejam todas expandidas no tempo de execução dentro do loop para garantir que novos valores definidos sejam lidos de acordo para ajudar a obter o valor% !maxfile!
final conforme cada iteração de loop.
Além disso, adicionei o CD /D "%%~F0"
à linha acima do início do ciclo FOR
para garantir que o diretório seja alterado para o diretório que o script reside, pois você não está explicitamente especificando o diretório em seu exemplo de comando, mas eu adicionou um script de exemplo explícito abaixo também.
Script em lote (implícito)
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
Script em lote (explícito)
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
Mais recursos
-
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).
-
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