这样,您将能够计算“New_Folder *”的出现次数并使用下一个数字创建一个。
@echo off
set /a count=0
for /d %%d in (New_Folder*) do (
set /a count+=1
)
set /a count+=1
mkdir New_Folder%count%
请注意,最好的方法是在 New_Folder 的末尾找到最大的数字,但是 Windows Batch 非常有限,而且我是 Linux 人!
编辑:经过大约一小时的谷歌搜索和测试:
@echo off
setlocal EnableDelayedExpansion
set max_number=0
for /d %%d in (New_Folder*) do (
set current_directory=%%~nxd
call:StrLength name_length !current_directory!
call:Substring directory_number,!current_directory!,10,!name_length!
if !directory_number! gtr !max_number! (
set max_number=!directory_number!
)
)
set /a max_number+=1
mkdir New_Folder%max_number%
:Substring
::Substring(retVal,string,startIndex,length)
:: extracts the substring from string starting at startIndex for the specified length
SET string=%2%
SET startIndex=%3%
SET length=%4%
if "%4" == "0" goto :noLength
CALL SET _substring=%%string:~%startIndex%,%length%%%
goto :substringResult
:noLength
CALL SET _substring=%%string:~%startIndex%%%
:substringResult
set "%~1=%_substring%"
GOTO :EOF
:StrLength
::StrLength(retVal,string)
::returns the length of the string specified in %2 and stores it in %1
set #=%2%
set length=0
:stringLengthLoop
if defined # (set #=%#:~1%&set /A length += 1&goto stringLengthLoop)
::echo the string is %length% characters long!
set "%~1=%length%"
GOTO :EOF
请注意,命令行返回错误“命令的语法不正确”。但一切正常,所以我会让你找到原因......无论目录的顺序如何,或者它们是否从 1 开始,都会创建新文件夹 :) 希望你会喜欢!