There could be used the following batch file for this task:
@echo off
setlocal EnableExtensions EnableDelayedExpansion
for /F "tokens=1,2 delims=/" %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "YearMonth=%%G%%H" & goto SearchFile
:SearchFile
set "SerialNumber=0"
set "FileNameScheme=AS%YearMonth:~2%-"
for %%G in ("%FileNameScheme%*.txt") do for /F "tokens=2 delims=-" %%H in ("%%~nG") do if %%H GTR !SerialNumber! (set "SerialNumber=%%H" & set "FileName=%%G")
set /A SerialNumber+=1
echo %FileNameScheme%%SerialNumber%| %SystemRoot%\System32\clip.exe
if defined FileName (
ren "%FileName%" "%FileNameScheme%%SerialNumber%.txt"
) else (
rem del /Q "AS????-*.txt" >nul 2>nul
echo %FileNameScheme%%SerialNumber%>"%FileNameScheme%%SerialNumber%.txt"
)
endlocal
The first two lines define the required execution environment completely which is:
- command echo mode turned off
- command extensions enabled
- delayed variable expansion enabled
Please read the chapter Usage of ROBOCOPY to get current date/time in my answer on Time is set incorrectly after midnight for an explanation of the FOR command line which uses ROBOCOPY to get current year and month independent on which country (region) is configured for the used account which determines the date format of the dynamic variable DATE
.
The environment variable SerialNumber
is defined first with default value 0
.
The environment variable FileNameScheme
is defined with AS
at beginning, the current year without the century with always two digits, the current month with always two digits and -
which results today in the string AS2204-
.
The command FOR is used to search in current directory (can by any directory) for one or more non-hidden files matching the wildcard pattern AS2204-*.txt
.
It is unclear how my files can be in the current directory matching the wildcard pattern AS????-*.txt
. Therefore the code is written to work also with multiple files matching this wildcard pattern in the current directory.
For each file name matching the wildcard pattern with current year and month already included one more FOR loop is used to get the string between the hyphen and the file extension which is hopefully always a number in range 0
to 2147483647
without leading zeros.
This number string is compared with the current value of the environment variable SerialNumber
. Delayed variable expansion must be enabled because of this number comparison. If the number of the current file is greater the current serial number, the greater number is used further as serial number and the name of the current file is assigned to the environment variable FileName
.
Please note that GTR
does not work on number in file has a leading zero as in this case the number would be interpreted as an octal instead of a decimal number which means 08
and 09
would be interpreted as invalid octal numbers with using in this case value 0
for the number comparison.
The default value 0
or the greatest number of files matching AS2204-*.txt
is next incremented by one using an arithmetic expression.
The file name with the incremented serial number is copied without file extension to the clipboard for whatever purpose.
If there was really found a file with current year and month in its name, this file is now renamed to contain the serial number incremented by one. Otherwise a new file is created with the new file name and containing the file name without file extension as data, for example AS2204-1
.
There is a line commented out with command REM which would delete all files matching the wildcard pattern AS????-*.txt
, except hidden files ignored by command DEL by default and read-only files which are not deleted by command DEL by default and matching files being currently opened by an application. It is not clear what to do with the other file(s) matching this pattern from former month(s).
The batch file code could be optimized to following command lines if there is either no or always just one file matching the wildcard pattern AS????-*.txt
in the current directory:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
for /F "tokens=1,2 delims=/" %%G in ('%SystemRoot%\System32\robocopy.exe "%SystemDrive%\|" . /NJH') do set "YearMonth=%%G%%H" & goto SearchFile
:SearchFile
set "SerialNumber=0"
set "FileNameScheme=AS%YearMonth:~2%-"
for %%G in ("%FileNameScheme%*.txt") do for /F "tokens=2 delims=-" %%H in ("%%~nG") do set "SerialNumber=%%H"
set /A SerialNumber+=1
echo %FileNameScheme%%SerialNumber%| %SystemRoot%\System32\clip.exe
if exist "AS????-*.txt" (
ren "AS????-*.txt" "%FileNameScheme%%SerialNumber%.txt"
) else (
echo %FileNameScheme%%SerialNumber%>"%FileNameScheme%%SerialNumber%.txt"
)
endlocal
Delayed variable expansion is no longer needed which makes the processing of the batch file a very little bit faster. Serial numbers with one or more leading zeros would be still a problem because of command SET on evaluation of an arithmetic expression converts the number string also to an integer with interpreting the number as octal number on having the character 0
at beginning of the number string.
To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
del /?
echo /?
endlocal /?
for /?
goto /?
if /?
rem /?
robocopy /?
set /?
setlocal /?
See also: