获取编号最大的文件夹,添加1

时间:2016-07-25 00:01:16

标签: batch-file

所以,我有一个简单的批处理程序,但是我遇到了一些代码问题。 假设我有这个:

set %expdir%=C:\Game\Deployment
echo Enter Export Number, or Enter X for Automatic
set /p basenumb="Number: " 
if "%basenumb%"=="x" ( goto foo ) else ( goto bar )
:foo
       rem //Calculate Biggest Number Here
set /a numb=%calc%+1
:bar
set numb=%basenumb%

我想要做的是在评论所在的部分,运行expdir中的所有目录并计算一个名称最大的目录,排除任何有字母的内容,并设置作为变量calc

的目录名称

所以,如果我在C:\Game\Deployment中有以下文件夹:

  • 1
  • 10
  • 19
  • 21
  • 45
  • 137

应将calc设置为137,因为137是目录列表中的最大数字。唯一的问题是我不知道有什么命令可以解决这个问题。

1 个答案:

答案 0 :(得分:1)

这是一个已评论的批处理代码,其中包含要求的扩展名和未要求扩展名的扩展名。

@echo off
setlocal EnableExtensions EnableDelayedExpansion
set "expdir=C:\Game\Deployment"

rem x is defined as default. The user has just to hit key RETURN or
rem ENTER to use the default value. This makes also sure that the
rem environment variable is always defined even if the user does
rem not enter anything at all. The string comparison for X is done
rem case-insensitive to make it possible that user enters x or X.

echo Enter export number, or enter X for automatic (default).
echo.
set "BaseNumber=x"
set /P "BaseNumber=Number: "
if /I "!BaseNumber!"=="x" goto FindNumber
echo.

rem Check if base number consists really only of digits 0-9.
set "Is_Not_Number="
for /F "delims=0123456789" %%I in ("!BaseNumber!") do set "Is_Not_Number=%%I"
if defined Is_Not_Number (
    echo Error: !BaseNumber! is not a valid number.
    echo.
    endlocal
    pause
    goto :EOF
)

rem More checks should be made for example on a too long string or
rem a number starting with a 0 like 020 which would be interpreted
rem as octal number and not as decimal number. (020 is decimal 16.)
goto ProcessNumber


rem The code below expects to find only folder names being a number.
rem The command DIR returns just directory names because of /AD in
rem bare format because of /B without full path.

rem Standard console application FINDSTR is used to filter out other
rem directory names containing any non digit character in name.

rem Note: Folder names with 1 or more leading 0 are a problem as they
rem could be interpreted as octal number and not as decimal number.
rem Extra code would be necessary to remove all leading 0 except last
rem one if the number consists only of zeros, e.g. being 0000.

:FindNumber
set "BaseNumber=0"
for /F "delims=" %%I in ('dir "%expdir%" /AD /B 2^>nul ^| %SystemRoot%\System32\findstr.exe /R "^[0-9][0-9]*$"') do (
    set "FolderNumber=%%I"
    if !FolderNumber! GTR !BaseNumber! set "BaseNumber=!FolderNumber!"
)
set /A BaseNumber+=1


:ProcessNumber
set "Number=%BaseNumber%"

echo The number is: %Number%
endlocal

如果输入的数字用于算术表达式,则应添加额外的代码以删除前导零以避免输入的数字被解释为八进制数。

我不确定是否需要137(=编号最大的目录)作为基数或138,即最大编号的目录名称加1。如果第一个变量的编号最大根据文件夹名称使用set "BaseNumber=1"(其他默认值)并删除set /A BaseNumber+=1(无增量)。

要了解使用的命令及其工作原理,请打开命令提示符窗口,执行以下命令,并完全阅读为每个命令显示的所有帮助页面。

  • dir /?
  • echo /?
  • endlocal /?
  • findstr /?
  • for /?
  • goto /?
  • if /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

另请参阅Microsoft有关Using command redirection operators的文章,以获取有关在获取和评估指定目录中的文件夹名称的循环中使用的2>nul|的说明。

重定向操作符>|必须使用^进行转义,以便在执行命令行时使用 DIR FINDSTR进行解释而不是处理 FOR 命令行,这会导致Windows命令解释程序处理批处理文件时出现语法错误消息。

2>nul将命令 DIR 输出的错误消息重定向到 STDERR ,以防指定目录C:\Game\Deployment不包含任何设备目录< strong> NUL 来抑制此错误消息。在这种情况下,基数值为1,因为默认值0增加1。