rakkad
asked on
DOS batch script check numbers in use in a file and identify next number
I trying to write a DOS batch that will check a file that has numbers in it e.g:-
5001
5005
5010
..etc......
Then identify the next number to use, so in this example it should display 'next available number is: 5002
Any help or suggestions be much appreciated
Thanks
5001
5005
5010
..etc......
Then identify the next number to use, so in this example it should display 'next available number is: 5002
Any help or suggestions be much appreciated
Thanks
I just made a fix.
Cheers,
Rene
Cheers,
Rene
@ECHO OFF
SETLOCAL EnableDelayedExpansion
REM CREATING A TEST FILE
SET File=%~n0.txt
ECHO 5002>"%File%"
ECHO 5010>>"%File%"
ECHO 5005>>"%File%"
REM READING THE NUMBERS FROM THE FILE
FOR /F "usebackq" %%A in ("%File%") DO SET Nb%%A=%%A
CALL :FindNb
ECHO %NbX%
PAUSE EXIT
:FindNb
REM FINDING THE NEXT AVAILABLE LOWER CONSECUTIVE NUMBER
FOR /F "tokens=2 delims==" %%A IN ('SET Nb') DO (
ECHO [%%A]
SET /a NbX=%%A + 1
IF NOT DEFINED Nb!NbX! EXIT /b
)
)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Can you explain how the code works, what happens when it reaches 5010, so the next number is 5011 will it get updated with this number ? Thanks
If it reaches 5010, the next consecutive number will be 5011.
If you need that your file if updated with the next available consecutive number:
If you need that your file if updated with the next available consecutive number:
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET File=%~n0.txt
REM IF TEST FILE DOES NOT EXIST, CREATING A TEST FILE
IF NOT EXIST "%File%" (
ECHO 5002>"%File%"
ECHO 5010>>"%File%"
ECHO 5005>>"%File%"
)
REM READING THE NUMBERS FROM THE FILE
FOR /F "usebackq" %%A in ("%File%") DO SET Nb%%A=%%A
CALL :FindNb
ECHO The next available consecutive number is [%NbX%]
ECHO %NbX%>>"%File%"
PAUSE
EXIT
:FindNb
REM FINDING THE NEXT AVAILABLE LOWER CONSECUTIVE NUMBER
FOR /F "tokens=2 delims==" %%A IN ('SET Nb') DO (
SET /a NbX=%%A + 1
IF NOT DEFINED Nb!NbX! EXIT /b
)
)
Are the numbers always in order in the incoming file?
~bp
~bp
If the numbers are in order in the incoming file, then here's a fairly straight forward approach to that:
@echo off
setlocal EnableDelayedExpansion
REM Define input file location
set Input=numbers.txt
set Next=0
REM Read each line of the file
for /F "usebackq tokens=1" %%N in ("%Input%") do (
REM If first line read, look for next number in sequence
if !Next! EQU 0 (
set /A Next=%%N+1
) else (
REM If the line is the one we expected (Last+1) then look for next number
if !Next! EQU %%N (
set /A Next+=1
) else (
REM Found a gap in the numbers, display the missing one and exit
echo Next=!Next!
goto :ExitLoop
)
)
)
:ExitLoop
~bp
Here is the same script, without the test portion and a bit shorter.
Do you need further help?
Cheers,
Rene
Do you need further help?
Cheers,
Rene
@ECHO OFF
SETLOCAL EnableDelayedExpansion
SET File=%~n0.txt
FOR /F "usebackq" %%A in ("%File%") DO (SET Nb%%A=%%A & CALL :FindNb)
ECHO THE NEXT AVAILABLE LOWER CONSECUTIVE NUMBER IS [%NbX%]
ECHO %NbX%>>"%File%"
PAUSE
EXIT
:FindNb
REM FINDING THE NEXT AVAILABLE LOWER CONSECUTIVE NUMBER
FOR /F "tokens=2 delims==" %%A IN ('SET Nb') DO (
SET /a NbX=%%A + 1
IF NOT DEFINED Nb!NbX! EXIT /b
)
)
ASKER
The numbers come in order of sequence. However, in the existing file if an existing number has been deleted, the script checks the list of numbers already in the list and calculates the next number to be available in the order of the list of numbers e.g:-
A existing file has a list of 4000, 4002, 4005 so in this example the 4003 should be listed as the next number, etc etc...
Thanks for your help much appreciated
A existing file has a list of 4000, 4002, 4005 so in this example the 4003 should be listed as the next number, etc etc...
Thanks for your help much appreciated
ASKER
Clicking on either hyperlink goes straight back to the asking question, it does direct me to the scripts
They are just links to our posts in this question, you can also just scroll up to see them.
~bp
~bp
Glad I could help, and thanks for the points
Cheers,
Rene
Cheers,
Rene
Open in new window