Search code examples
batch-filecmderrorlevel

Compare the absolute value of a number in a for loop to determine if it's to be used?


I'm now on the bigger problem I mentioned in this post...Searching a text file and sending only numbers greater than a certain absolute value to text file?

I figured if I set num=!num:-=! with an original value of, say, -17, then the !errorlevel! will evaluate to true or 0 right? Something's not working here though...

To clarify, I need to filter out only the first and fourth tokens of lines in which the fourth token is either greater than 3 or less than -3, as well as any lines that do not have a 4th token (this part is solved). I have tried using the /A option of set and it doesnt seem to work still.

setlocal enabledelayedexpansion

set "min=-"

for /f "tokens=1,4" %%a in ('findstr /b /r /c:"[^ ]*:S:" print.log') do (
    if %%b=="" (echo %%a ^*^*^* >>new.txt) else (
        set num="%%b"
        set num=!num:-=!
        if !errorlevel!==0 (
            if !num! GTR 3 echo %%a !min!!num! >> new.txt
        ) else (
            if !num! GTR 3 echo %%a !num! >> new.txt
        )
    )
)

exit /b

The text in print.log looks like:

ksdf 0 0 -4

as7d:S:asf 0 0 -4

kc:S:cd3 0 0 -2

asdk:S:s 0 0 6

lasd:S:dd 0 0


Solution

  • @echo off
        setlocal enableextensions disabledelayedexpansion
    
        >"new.txt" (
            for /f "tokens=1,4" %%a in ('
                findstr /b /r /c:"[^ ]*:S:" print.log
            ') do if "%%~b"=="" (echo %%a ***) else (
                set "print=1" 
                if %%b lss 4 if %%b gtr -4 set "print="
                if defined print echo %%a %%b
            )
        )
    

    Instead of printing when the value is lower than -3 OR greater than 3, it does not print when the value is lower than 4 AND greater than -4

    -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6
              ^.................^
                  don't print