Jump to content

Batch File Wont Compare Numbers?


Recommended Posts

I found a neat little program that lists the modes you can use for your monitor. I run it and output its results to a .txt file that ends up like this:

768x1280, 32 bits @ 60 Hz.
1280x768, 32 bits @ 60 Hz.
768x1280, 32 bits @ 60 Hz.
900x1440, 8 bits @ 60 Hz.
1440x900, 8 bits @ 60 Hz.
900x1440, 8 bits @ 60 Hz.
900x1440, 16 bits @ 60 Hz.
1440x900, 16 bits @ 60 Hz.
900x1440, 16 bits @ 60 Hz.
900x1440, 32 bits @ 60 Hz.
1440x900, 32 bits @ 60 Hz.
900x1440, 32 bits @ 60 Hz.

So I wanted to write a simple batch file to extract the highest resolution and apply it with this program. What I seem to be getting stuck on is the fact that IF <numeric_variable> GTR <numeric_variable> <command> is NOT working for me. Unless ofc 900 is greater than 1440.

Basically I have this code:


@ECHO OFF

SetLocal EnableDelayedExpansion

SET X=0
SET Y=0

rem Extract Resolution in the Format XRESxYRES
FOR /F "skip=2 tokens=1 delims=, " %%i IN (Qres.txt) DO (
rem Sets Resolution to RES, not sure if this is necessary but I didn't know how else to pass to next FOR
SET RES=%%i
rem Extract XRes from Resolution and Should set X to that value if it is higher than its current value
FOR /F "tokens=1 delims=x" %%a IN ("!RES!") DO IF %%a GTR %X% SET X=%%a
)

ECHO %RES%
ECHO %X%

According to my input and my code I should be getting 1440 for X and I am consistently getting 900.

I am completely dumbfounded as to why and would really appreciate someone pointing out my stupid mistake.

Edited by cdusseau
Link to comment
Share on other sites


I would do it like this:

@ECHO OFF

SetLocal enabledelayedexpansion

SET /A X=0
SET /A Y=0
set RES=""
rem Extract Resolution in the Format XRESxYRES
FOR /F "skip=2 tokens=1 delims=, " %%i IN (test1.txt) DO ( call :evalres %%i)


ECHO %MAXRES%
ECHO %X%
goto end
:evalres
SET RES=%1
set /A aux=0
for /f "delims=x tokens=1 usebackq" %%g in (`echo %1`) do (
set /A aux=%aux%+%%g
if !aux! GTR !X! set /A X=!aux! && set MAXRES=%1
)
GOTO :EOF
:end
endlocal

Link to comment
Share on other sites

I'd suggest that you don't need to look into the output results too deeply. The output appears, from the example you provided, to list each horizontal x Vertical lowest to highest and follows that for colour depth and frequency. Your script will therefore be okay to just retrieve the data from the bottom most line containing your chosen mode.

If you wanted the largest resolution using a larger Horizontal than Vertical then:

@ECHO OFF & SETLOCAL
FOR /F "TOKENS=1-3,5 DELIMS=x,@ " %%a IN (Qres.txt) DO (
IF %%a GTR %%b (SET "X_=%%a" & SET "Y_=%%b" & SET "C_=%%c" & SET "H_=%%d"))
ECHO=Highest RES IN Qres.txt IS %X_%x%Y_% %C_% bits @ %H_% Hz.
PING -n 6 127.0.0.1 1>NUL

If you required a larger Vertical than Horizontal then:

@ECHO OFF & SETLOCAL
FOR /F "TOKENS=1-3,5 DELIMS=x,@ " %%a IN (Qres.txt) DO (
IF %%a LSS %%b (SET "X_=%%a" & SET "Y_=%%b" & SET "C_=%%c" & SET "H_=%%d"))
ECHO=Highest RES IN Qres.txt IS %X_%x%Y_% %C_% bits @ %H_% Hz.
PING -n 6 127.0.0.1 1>NUL

Hope this helps!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...