I am trying to create a batch file to process some items. I am quite familiar with C/C++/Java, etc., but unfortunately for this use case I need to use a batch file. I am not very familiar with batch files and this site has been a life saver.
As part of what I am trying to do, I need to do some numeric comparisons in the batch file. But the comparison seems to keep failing (I presume they error out but I haven't actually checked errorvalue). I have looked at many examples on this site and others but am unable to find where I am going wrong.
Below is the snippet of the code where I am struggling. Complete batch file is longer and more complex. Variable "stringlength" is calculated by a function. In the example that I am running it on, "stringlength" is set as 42 by the function that calculates the string length (the function is a copy paste of Jeb's answer at How do you get the string length in a batch file?). Thinking that maybe the function was returning the result as a string rather than a numerical value, I have an explicit statement to assign variable "strLength" as a numerical expression.
When I run the below code, I hit the 1st 'PAUSE' with all values as expected. Upon continuing, the batch file just closes unexpectedly. It does not hit any of the other 'PAUSE' commands.
SETLOCAL EnableExtensions EnableDelayedExpansionREM searchString is a 42 character string calculated elsewhere in the batchfile. For the purpose of this question, I have set it to a random value below.SET searchString="abcdefgh"CALL :strlen stringlength searchStringSET /A strLength=%stringlength%echo %strLength%echo %stringlength%PAUSEIF %strLength% GTR 0 ( echo %strLength% echo %stringlength% PAUSE) ELSE ( echo "strLength issue" PAUSE)REM ********* function *****************************:strlen <resultVar> <stringVar>( (set^ tmp=!%~2!) if defined tmp ( set "len=1" for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( if "!tmp:~%%P,1!" NEQ "" ( set /a "len+=%%P" set "tmp=!tmp:~%%P!" ) ) ) else ( set len=0 ))( set "%~1=%len%" exit /b)
Thinking that maybe a different variation might work (delayed expansion or another method of coding), I have tried the following 3 variations as well, but I am still stuck with the same exact result.
SETLOCAL EnableExtensions EnableDelayedExpansionREM searchString is a 42 character string calculated elsewhere in the batchfile. For the purpose of this question, I have set it to a random value below.SET searchString="abcdefgh"CALL :strlen stringlength searchStringSET /A strLength=!stringlength!echo !strLength!echo !stringlength!PAUSEIF !strLength! GTR 0 ( echo !strLength! echo !stringlength! PAUSE) ELSE ( echo "strLength issue" PAUSE)REM ********* function *****************************:strlen <resultVar> <stringVar>( (set^ tmp=!%~2!) if defined tmp ( set "len=1" for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( if "!tmp:~%%P,1!" NEQ "" ( set /a "len+=%%P" set "tmp=!tmp:~%%P!" ) ) ) else ( set len=0 ))( set "%~1=%len%" exit /b)
SETLOCAL EnableExtensions EnableDelayedExpansionREM searchString is a 42 character string calculated elsewhere in the batchfile. For the purpose of this question, I have set it to a random value below.SET searchString="abcdefgh"CALL :strlen stringlength searchStringSET /A "strLength=!stringlength!"echo !strLength!echo !stringlength!PAUSEIF !strLength! GTR 0 ( echo !strLength! echo !stringlength! PAUSE) ELSE ( echo "strLength issue" PAUSE)
SETLOCAL EnableExtensions EnableDelayedExpansionREM searchString is a 42 character string calculated elsewhere in the batchfile. For the purpose of this question, I have set it to a random value below.SET searchString="abcdefgh"CALL :strlen stringlength searchStringSET /A "strLength=%stringlength%"echo %strLength%echo %stringlength%PAUSEIF %strLength% GTR 0 ( echo %strLength% echo %stringlength% PAUSE) ELSE ( echo "strLength issue" PAUSE)REM ********* function *****************************:strlen <resultVar> <stringVar>( (set^ tmp=!%~2!) if defined tmp ( set "len=1" for %%P in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do ( if "!tmp:~%%P,1!" NEQ "" ( set /a "len+=%%P" set "tmp=!tmp:~%%P!" ) ) ) else ( set len=0 ))( set "%~1=%len%" exit /b)
The output I see for all these different variants is:
42
42
Press any key to continue...
This corresponds to the 'echo' and 'PAUSE' commands before the 'IF' condition. I do not get anything after that. The batch file simply closes.
Any help/guidance on where I may be going wrong will be greatly appreciated.