Eduardo Perez
Eduardo Perez

Reputation: 573

Get folder with highest number, add 1 to it

So, I have a simple batch program, but I'm having trouble with some code. Let's say I have this:

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%

What I want to do is in the part where the comment is located, run through all the directories in expdir and calculate the one that has the biggest number for a name, excluding anything that has letters, and set the name of the directory as the variable calc

So, if I have the following folders in C:\Game\Deployment:

It should set calc to 137 since 137 is the biggest number in the directory list. Only problem is I don't know of any commands to pull this off.

Upvotes: 1

Views: 1360

Answers (1)

Mofi
Mofi

Reputation: 49127

Here is a commented batch code for the task with extensions as asked for and with extensions not asked for.

@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

Extra code should be added for removing leading zeros to avoid that an entered number is interpreted as octal number if the entered number is used in an arithmetic expression.

I am not sure if 137 (= directory with greatest number) is wanted as base number or 138, i.e. directory name with greatest number incremented by 1. In case of first variant with greatest number according to folder names use set "BaseNumber=1" (other default value) and remove set /A BaseNumber+=1 (no increment).

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

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

See also the Microsoft article about Using command redirection operators for an explanation of 2>nul and | used in the loop getting and evaluating the folder names in the specified directory.

The redirection operators > and | must be escaped here with ^ to be interpreted on execution of the command line with DIR and FINDSTR and not on processing the FOR command line which would result in a syntax error message by Windows command interpreter processing the batch file.

2>nul redirects the error message output by command DIR to STDERR in case of specified directory C:\Game\Deployment does not contain any directory to device NUL to suppress this error message. The base number has in this case the value 1 because default 0 is incremented by 1.

Upvotes: 1

Related Questions