这是我的解决方案,它使用了一种不同的方法,除了执行 WMIC 命令之外,当当前日期和月份应从本地日期/时间确定时无法避免一种区域和语言独立的方式。重新格式化环境变量 DATE 的字符串会更快,但此日期字符串的格式取决于为使用的用户帐户定义的 Windows 区域设置。
带有大量注释行的批处理代码(以命令REM开头的行):
@echo off
goto GetFolderList
rem Run DIR to get a list of only folder names because of /AD and /B
rem matching the wildcard pattern ?? - ??-?? ordered reverse by name
rem because of /O-N with suppressing the error message DIR outputs if
rem no such folder can be found in current directory by redirecting
rem the error message to device NUL.
rem From folder name just the first two characters are assigned to loop
rem variable I as the default delimiters space/tab are used on splitting
rem up the folder name into tokens. Only the first token is processed by
rem outer FOR with the default options, i.e. the number to increment.
rem The inner FOR executes the SET command only if the first space/tab
rem delimited string of current folder name from DIR output contains any
rem non digit character. So if environment variable Number is defined
rem after inner FOR loop execution, the folder name is not valid as it
rem does not start with a number with 2 digits.
rem On current folder name not starting with a number, the outer FOR loop
rem is processed further with next folder name. Otherwise the number of
rem the current folder name being automatically the highest number because
rem of using leading zeros and reverse output by name is assigned to the
rem environment variable Number and outer FOR loop is exited with a jump
rem to label TrimNumber.
rem The numbering for first folder starts with 01 in case of no (valid)
rem folder matching wildcard pattern ?? - ??-?? is found in current folder.
:GetFolderList
set "Number="
for /F %%I in ('dir /AD /B /O-N "?? - ??-??" 2^>nul') do (
for /F "delims=0123456789" %%J in ("%%I") do set "Number=none"
if defined Number (
set "Number="
) else (
set Number=%%I
goto TrimNumber
)
)
set "Number=01"
goto CreateFolder
rem On incrementing a number with leading zeros using an arithmetic expression
rem it can happen easily that the number is interpreted octal, especially if
rem the number would have 3 digits which is not the case here. So it is better
rem to remove the leading zero(s) before incrementing the number by 1.
rem Of course with number from folder name being 00 it happens on removing
rem in a loop the first character from string when being 0 that the number
rem string is suddenly not defined anymore which must result also in an
rem exit of this loop trimming leading zeros.
:TrimNumber
if not "%Number:~0,1%" == "0" goto IncrementNumber
set "Number=%Number:~1%"
if defined Number goto TrimNumber
set "Number=01"
goto CreateFolder
rem After leading zeros are removed it is safe to increment the number by 1.
rem Then it is necessary to make sure that the number has always two digits
rem even on being 1 to 9. This can be achieved by inserting a leading 0 on
rem number string and then get just the last 2 characters from number string.
:IncrementNumber
set /A Number+=1
set "Number=0%Number%"
set "Number=%Number:~-2%"
goto CreateFolder
rem The WMIC command below with used options outputs among blank
rem lines in UTF-16 Little Endian encoding also a line with
rem LocalDateTime=20170617192138.218000+120
rem Of interest from this line is the string after the equal sign as it
rem contains year, month, day, hour, minute, second, microsecond and UTC
rem offset of configured time zone in fixed and region independent format.
rem From this date/time string just the month and day is needed for the
rem folder name in format DD-MM with incremented number being prepended.
:CreateFolder
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDateTime=%%I"
set "FolderName=%Number% - %LocalDateTime:~6,2%-%LocalDateTime:~4,2%"
md "%FolderName%"
编辑: 正如Magoo 所解释的那样,通过在文件夹名称中的数字字符串前面加上1 来避免使用前导0 对数字进行八进制解释的方法非常棒,并且使代码更加简单并且更快一点:
@echo off
goto GetFolderList
rem Run DIR to get a list of only folder names because of /AD and /B
rem matching the wildcard pattern ?? - ??-?? ordered reverse by name
rem because of /O-N with suppressing the error message DIR outputs if
rem no such folder can be found in current directory by redirecting
rem the error message to device NUL.
rem From folder name just the first two characters are assigned to loop
rem variable I as the default delimiters space/tab are used on splitting
rem up the folder name into tokens. Only the first token is processed by
rem outer FOR with the default options, i.e. the number to increment.
rem The inner FOR executes the SET command only if the first space/tab
rem delimited string of current folder name from DIR output contains any
rem non digit character. So if environment variable Number is defined
rem after inner FOR loop execution, the folder name is not valid as it
rem does not start with a number with 2 digits.
rem On current folder name not starting with a number, the outer FOR loop
rem is processed further with next folder name. Otherwise the number of
rem the current folder name being automatically the highest number because
rem of using leading zeros and reverse output by name is assigned to the
rem environment variable Number and outer FOR loop is exited with a jump
rem to label IncrementNumber.
rem The numbering for first folder starts with 01 in case of no (valid)
rem folder matching wildcard pattern ?? - ??-?? is found in current folder.
:GetFolderList
set "Number="
for /F %%I in ('dir /AD /B /O-N "?? - ??-??" 2^>nul') do (
for /F "delims=0123456789" %%J in ("%%I") do set "Number=none"
if defined Number (
set "Number="
) else (
set Number=%%I
goto IncrementNumber
)
)
set "Number=01"
goto CreateFolder
rem On incrementing a number with leading zeros using an arithmetic expression
rem it can happen easily that the number is interpreted octal, especially if
rem the number would have 3 digits which is not the case here. That can be
rem avoided here by changing the strings "00" to "99" to "100" to "199".
rem Next it is safe to increment the number by 1. Then it is necessary to get
rem from number in range 101 to 200 the last 2 characters from number string.
:IncrementNumber
set "Number=1%Number%"
set /A Number+=1
set "Number=%Number:~-2%"
goto CreateFolder
rem The WMIC command below with used options outputs among blank
rem lines in UTF-16 Little Endian encoding also a line with
rem LocalDateTime=20170617192138.218000+120
rem Of interest from this line is the string after the equal sign as it
rem contains year, month, day, hour, minute, second, microsecond and UTC
rem offset of configured time zone in fixed and region independent format.
rem From this date/time string just the month and day is needed for the
rem folder name in format DD-MM with incremented number being prepended.
:CreateFolder
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDateTime=%%I"
set "FolderName=%Number% - %LocalDateTime:~6,2%-%LocalDateTime:~4,2%"
md "%FolderName%"
还有一点说明:
类似的命令行
set "FolderName=%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%
在 FOR 命令行之后使用 WMIC 命令将定义格式为 YYYY-MM-DD 的文件夹名称,该格式最常用于文件夹和文件名,因为使用此国际日期format 按名称排序的文件和文件夹也会自动按日期排序。
所以当不需要在同一天创建多个文件夹,而文件夹名称中确实需要一个递增的数字时,最好在文件夹名称中使用国际日期格式,并且可以将批处理代码简化为:
@echo off
for /F "tokens=2 delims==." %%I in ('%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime /VALUE') do set "LocalDateTime=%%I"
md "%LocalDateTime:~0,4%-%LocalDateTime:~4,2%-%LocalDateTime:~6,2%
要了解所使用的命令及其工作原理,请打开命令提示符窗口,在其中执行以下命令,并仔细阅读每个命令显示的所有帮助页面。
dir /?
echo /?
for /?
goto /?
if /?
rem /?
set /?
wmic /?
wmic os /?
wmic os get /?
wmic os get localdatetime /?
另请阅读有关Using Command Redirection Operators 的Microsoft 文章,了解2>nul 的解释。重定向运算符> 必须在此处使用插入符号^ 进行转义,以便Windows 命令解释器在解析整个FOR 命令行以及稍后执行WMIC 时将其解释为文字字符strong> FOR 作为重定向操作符的命令行。