• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 478
  • Last Modified:

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
0
rakkad
Asked:
rakkad
  • 7
  • 4
  • 3
1 Solution
 
ReneGeCommented:
Here you go:

@ECHO OFF
SETLOCAL EnableDelayedExpansion

REM CREATING A TEST FILE
   SET File=%~n0.txt
   ECHO 5001>"%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=1,2 delims==" %%A IN ('SET Nb') DO (
      SET /a NbX=%%A + 1
      IF NOT DEFINED Nb!NbX! EXIT /b
      )
   )

Open in new window

0
 
ReneGeCommented:
I just made a fix.

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
      )
   )

Open in new window

0
 
ReneGeCommented:
Oops...

@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 (
      SET /a NbX=%%A + 1
      IF NOT DEFINED Nb!NbX! EXIT /b
      )
   )

Open in new window

0
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
rakkadAuthor Commented:
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
0
 
ReneGeCommented:
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:

@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
      )
   )

Open in new window

0
 
Bill PrewCommented:
Are the numbers always in order in the incoming file?

~bp
0
 
Bill PrewCommented:
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

Open in new window

~bp
0
 
ReneGeCommented:
Here is the same script, without the test portion and a bit shorter.

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
      )
   )

Open in new window

0
 
rakkadAuthor Commented:
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
0
 
Bill PrewCommented:
Okay, then my script from http:#a38385254 should work quite nicely for that.

~bp
0
 
ReneGeCommented:
Okay, then my script from http:#a38387311 works perfectly for that.

~ReneGe

;)
0
 
rakkadAuthor Commented:
Clicking on either hyperlink goes straight back to the asking question, it does direct me to the scripts
0
 
Bill PrewCommented:
They are just links to our posts in this question, you can also just scroll up to see them.

~bp
0
 
ReneGeCommented:
Glad I could help, and thanks for the points

Cheers,
Rene
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now