0

I'm learning windows batch-file script and creating my own scripts to practice the coding but kind of hit a block while try find whether the numbers in a text file is in sequence or not.I have two files,one file(file.txt) contains the number of lines in file_received.txt. The file_received.txt content is below:

1021
1022
1023
1024
1025
1027
1028

I'm building a script to test whether all the numbers in the text file are in sequence .so as a first step I'm trying to extract each line of the file_received to be assigned to a variable through if / for loop but the if command loop assigning all the lines to the variable num from file_received.txt at the same time. Is it possible to assign first line of the file to variable num and increment it as the if loops increment?

   setlocal EnableDelayedExpansion
    rem assign the number of lines to a variable
    set /P var=<C:\files.txt
    for /F "tokens=1" %%a in ("%var%") do echo.%%a
    
    rem assign the first variable to var1
    set /P var1=<C:\files_received_sequence.txt
    for /F "tokens=1" %%a in ("%var1%") do echo.%%a
    
    set /a x=1
    :while
    if %x% leq %var% (
        echo %x%
        rem assigning each line to the variable num inside the if loop and will be used in comparison and reser
        for /F "tokens=%x%" %%i in (C:\files_received.txt) do set num=%%i
        echo %num%
        set /a x+=1
        goto :while
    )
    
    echo test :D

the output is as below in loop 1 the entire file content is assigned to the variable num and from loop 2 to 7 the last number is assigned.

C:\>setlocal EnableDelayedExpansion 
C:\>set /P var= 0<C:\files.txt 
C:\>for /F "tokens=1" %a in ("7 ") do echo.%a
C:\>echo.7
7
C:\>set /P var1= 0<C:\files_received.txt 
C:\>for /F "tokens=1" %a in ("1021") do echo.%a
C:\>echo.1021
1021
C:\>set /a x=1 
C:\>if 1 LEQ 7 (
echo 1  
for /F "tokens=1" %i in (C:\files_received.txt) do set num=%i  
echo   
set /a x+=1  
goto :while 
) 
1
C:\>set num=1021 
C:\>set num=1022 
C:\>set num=1023 
C:\>set num=1024 
C:\>set num=1025 
C:\>set num=1027 
C:\>set num=1028 
ECHO is on.
C:\>if 2 LEQ 7 (
echo 2  
 for /F "tokens=2" %i in (C:\files_received.txt) do set num=%i  
 echo 1028  
 set /a x+=1  
 goto :while 
) 
2
1028
JPRLCol
  • 749
  • 11
  • 28
  • 1
    Your question is unclear. Are you trying to determine whether the lines are in sequence, i.e. first line is the lowest/highest number and each subsequent line is a positive or negative fixed increment of the previous one. Or trying to determine if each of your lines are ordered numerically, i.e. the lowest or highest number is on the first line and each subsequent line is lower/higher than the previous one, but without a fixed increment. Or are you trying to determine if, when the content is numerically ordered, one of those explanations is true. – Compo Aug 16 '20 at 14:45

3 Answers3

1

Here's the logic I'd use to test that each number is in sequence incrementing by one (and only one) each time:

@Echo off & Setlocal EnableDelayedExpansion
Set "ln="
For /F "Delims=" %%i in (C:\file_received.txt) Do (
 If Not "!ln!"=="" For /F "UseBackQ Delims=" %%v in (`"Set /A Nx=!ln!+1"`) Do (If Not "%%i"=="%%v" (Echo/OoS:!ln!/%%i & Goto :False))2> Nul
 Set "ln=%%i"
)
Echo/True
Exit /B 0
:False
Echo/False
Exit /B 1
T3RR0R
  • 2,747
  • 3
  • 10
  • 25
  • it works and solution is simple. I need to learn and practice a lot. Thanks T3rr0r – tennisnovice001 Aug 16 '20 at 12:56
  • If the requirement is to be GTR than the previous number as opposed to exactly 1 higher than the previous number, changing `"%%i"=="%%v" ` to `%%i GEQ %%v` will match that requirement. – T3RR0R Aug 16 '20 at 13:20
0

besides you don't use delayed expansion (although you have enabled it), your logic is far too complicated:

@echo off
setlocal enabledelayedexpansion
set last=-1
for /f %%a in (t.txt) do (
  if %%a lss !last! (
    echo not in sequence
    goto :eof
  )
  set "last=%%a"
)
echo all in sequence.

the variable !last! holds the value from the previous iteration of the loop.

(Depending on your needs, you may want to replace lss with leq)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • this may be a bit too simplified, as the example shows numbers incrementing by steps of one. if the list skips from say 1024 to 1028, this logic would still show as being in sequence, despite missing numbers between the sequence. That said, the question could do with a bit more clarification as to the specific requiremnent. – T3RR0R Aug 16 '20 at 12:42
  • 1
    @T3RR0R: in fact, in the example, `1026` is missing, so it isn't necessarily consecutive numbering. – Stephan Aug 16 '20 at 12:49
  • @T3RR0R, OP mentions Sequence, not missing numbers, so it could be `1023 1037 1099` as long as it is in sequence not 1037 1023..` – Gerhard Aug 16 '20 at 12:50
  • the numbers in the files are always sorted and incremented by one. I'm trying to find if they are in sequence or not. – tennisnovice001 Aug 16 '20 at 12:51
  • missed that in the example, will adjust my answer accordingy. to me that would be a matter of incrementing upon a previous number more so than a matter of sequence. – T3RR0R Aug 16 '20 at 12:52
  • 1
    to be clear @tennisnovice001 is your requirement to flag any instance where the next number in the sequence is anything other than an increment of one? – T3RR0R Aug 16 '20 at 12:56
  • No. I'm trying to provide explanation to Gerhard. – tennisnovice001 Aug 16 '20 at 13:02
0

Here is a version that will highlight the item not in sequence. Do note, that it will test sequence only, in other words ensure the previous result is lower than the next

@echo off & setlocal enabledelayedexpansion
set rev=-1
for /f "delims=" %%a in (files_received.txt) do for %%i in (%%a) do (
   if %%i leq !rev! (
    echo %%i Out of sequence
  ) else (
    echo %%i
    set rev=%%i
  )
)
Gerhard
  • 22,678
  • 7
  • 27
  • 43