0

There's a file called "Settings.txt", this file contains a lot of lines including the following variable line:

X 100

The number in this line is the variable part, "X" is a fixed text.

What I need is to check if that number is (less than 100) OR (equal or greater than 100) then based on the result > goto A or B.

The script could be something like:

IF >> "X 100" in "D:\Settings.txt" GEQ 100 goto A else goto B
:A
@echo the value is equal or greater than 100
pause & goto continue
:B
@echo the value is less than 100
pause
:continue
#the rest of the script
Michael513
  • 25
  • 4

1 Answers1

-1

A mechanism to read from the Settings.txt file is needed to get the number.

@ECHO OFF
FOR /F "tokens=1,2" %%A IN ('findstr.exe /B "X [:digit:]+" "Settings.txt"') DO (SET "NUM=%%~B")
ECHO NUM is set to %NUM%
IF  %NUM% GEQ 100 (GOTO ALABEL) ELSE (GOTO BLABEL)
:ALABEL
    ECHO Do greater than or equal stuff
    GOTO TheEnd
:BLABEL
    ECHO Do less than stuff
    GOTO TheEnd
:TheEnd
EXIT /B 0
lit
  • 14,456
  • 10
  • 65
  • 119
  • 3
    findstr does not support regex such as `\d`. – T3RR0R Nov 25 '21 at 15:37
  • 1
    @T3RR0R, it might not "support" it, but it appears to find the correct string. I changed it to something documented in the `findstr.exe /?` information. It also works. – lit Nov 25 '21 at 15:41
  • `findstr /?` of Windows 7 outputs the information as documented also by Microsoft on the page about [findstr](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/findstr). There is nothing written about a multiplier `+` or a special named character class like `:digit:`. If I create a `Settings.txt` with the two lines `X 100` and `X digit` and run the command line used by you on the __FOR__ command line, `%SystemRoot%\System32\findstr.exe` of Windows 7 outputs both lines. – Mofi Nov 25 '21 at 15:53
  • 1
    it works by chance, because you search for `X` *and/or* the literal string `\d+`. The correct form (if the regex would work), would be `findstr /BRC:"X \d+` – Stephan Nov 25 '21 at 15:53
  • Did you run also false positive tests with `findstr` of Windows ? of which usage help output with /? explains `+` and `:digit:` too? Which lines did you have in `Settings.txt` on testing the batch file? Please note that a space in a search string enclosed in `"` is interpreted by __FINDSTR__ as __OR__. So there is searched for lines containing at the beginning of the line either `X` __OR__ the string `[:digit:]+`. – Mofi Nov 25 '21 at 15:53
  • 2
    @lit It could be helpful for you to read my answer on [How to use OR operator with command FINDSTR from a Windows command prompt?](https://stackoverflow.com/a/32159911/3074564) The regular expression syntax of __FINDSTR__ is very special. It is advisable to forget everything known from Perl compatible regular expressions. Even `[0-9]` does not match only `0123456789`, but also `¹²³` which nobody familiar with Perl regular expression syntax would expect. (PS: Solution is `/B /R /C:"X [0123456789][0123456789]*"`.) PPS: I have not yet down voted the answer. – Mofi Nov 25 '21 at 16:04
  • 1
    @KJ, ok, I removed the path until I can find out why it does not work. – lit Nov 25 '21 at 16:10
  • 1
    @KJ, you never know what poison Microsoft might put into a path. :-) – lit Nov 25 '21 at 16:13
  • Well, `/B /R /C:"X [0123456789][0123456789]*"` is also not really perfect as it matches also a line beginning with `X 1²00`. There could be used `/R /X /C:"X [0123456789][0123456789]*"` to match only lines starting with `X`, having next one space, then a number and nothing else on that line which matches a line like `X 1200`, but not a line like `X 1²00`. `windir` is an environment variable from Windows 95 and is an environment variable defined as __system__ environment variable. It is deprecated. There should be used since Windows NT4 `SystemRoot`. – Mofi Nov 25 '21 at 16:14
  • `reg query "HKLM\System\CurrentControlSet\Control\Session Manager\Environment"` might output also `windir REG_EXPAND_SZ %SystemRoot%`, but this __system__ environment variable explicitly defined can be also missing (should not for downward compatibility). `SystemRoot` is an environment variable defined by Windows Shell (`explorer.exe`). It always exists, except somebody uses in a command prompt window or a batch file `set SystemRoot=` to explicitly undefine it __locally__. – Mofi Nov 25 '21 at 16:24
  • The file that I'm working with has only 1 of [X digit], so your answer works great for me, Thanks lit! (PS: I don't know why I was downvoted, didn't I explain my question enough?) – Michael513 Nov 25 '21 at 17:28
  • @Lit I downvoted an answer which uses incorrect synax that returns false positives. If you correct those errors using the information other users have since provided, the downvote will be removed. – T3RR0R Nov 25 '21 at 17:33
  • @mofi `%SystemRoot%\System32\findstr.exe /XRC:"X [123456789][0123456789]*"` seems most appropriate, as this also eliminates matches to 0 prefixed numbers – T3RR0R Nov 25 '21 at 18:54
  • @T3RR0R Yes, that command line is most likely best as the __IF__ condition would interpret a number with one or more leading `0` definitely wrong as octal number than as decimal number. – Mofi Nov 26 '21 at 18:08