VideoHelp Forum




+ Reply to Thread
Results 1 to 11 of 11
  1. Member
    Join Date
    Jun 2022
    Location
    Dublin
    Search Comp PM
    Basically how to test if a string variable value is greater than the batch file 32 bit signed limit of 2,147,483,647.

    This can be held in a batch string variable but any attempt to set it to an arithmetic value causes an error, as expected.

    To avoid the error I thought if a test using PS first could be used to know if value was greater or not than limit.

    Ideally I could then put the result into a batch file string var say YES or NO.

    I'm not at all familiar with PS.

    Thanks for any help with this.
    Quote Quote  
  2. Little experience with PowerShell, and not sure if I understand the issue... Why is the error expected?
    Without at least an example of commands you tried and errors you got, it's very difficult to figure it out.

    And here may not be the most suitable place for such a question. Perhaps SuperUser.com (if you manage to get the question approved, as they only want “quality” questions over there... and a “quality” question over there is a question that is phrased with such clarity that you have to be able to write a PhD thesis on the whole topic surrounding it to be allowed to ask it... or else whenever a question of questionable quality gets reluctantly approved, it will get down-voted into oblivion before it has a chance of eliciting halfway decent answers, unless you come up with a perfectly good answer yourself, which kinda defeats the purpose of asking in the first place... but if you try sometimes... you just might find... you get what you neee-eeed!).
    Quote Quote  
  3. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    test_x32.bat
    Code:
    @echo off
    powershell -command ^
        $maxInt32 = [int]::MaxValue; ^
        $testNumber = 2147483630; ^
    
    if ($testNumber -gt $maxInt32) { ^
        Write-Host "Number exceeds 32-bit maximum!" -ForegroundColor Red; ^
    } else { ^
        Write-Host "Number is within 32-bit range." -ForegroundColor Green; ^
    };
    pause
    Quote Quote  
  4. Wow, there are PowerShell wizards on here after all!
    Why is “[int]::MaxValue” equal to 2147483647 here? (Since PowerShell can normally handle much higher values.)
    What are the “^” for?
    Why isn't there an error if the number is higher than the 32-bit limit, running this as a .bat script? (Since the basic Windows shell can not handle higher values.)
    I tested with: “set /P test=” and “$testNumber = %test%; ^”, it seems to be working as well, even with a high value like 4294967296.
    Quote Quote  
  5. Member
    Join Date
    Jun 2022
    Location
    Dublin
    Search Comp PM
    Thanks for responding. I may not have explained well enough what I am trying to achieve.

    The following batch file, which includes the PS piece works aok.

    For bigger than 2GB file size ...
    If I used the %_INPUT_FILES_SIZE% var in the PS section instead of the _SIZE var it wouldn't work because it needs to be a string value, the arithmetic value of the %_INPUT_FILES_SIZE% var is = nul.

    @echo off
    mode CON:cols=228 lines=30
    color 0A
    SETLOCAL EnableDelayedExpansion
    color 0A

    REM If SET _INPUT_FILE="%~n1%~x1" is used then all program code references to input file should be so ... %_INPUT_FILE%. File will display as "My File.mp4"
    SET _INPUT_FILE="%~n1%~x1"

    REM Get the input file size.
    REM This will fail and cause abrupt program exit if the input file size is greater than the 32 bit signed value of 2,147,483,647 bytes = 2GB.

    REM A fix to prevent abrupt termination is to first give an arithmetic value of 0 to the _INPUT_FILES_SIZE var below ... SET /A _INPUT_FILES_SIZE=0

    SET _ANY_FILE=%_INPUT_FILE%
    SET _SIZE=
    CALL :SETSIZE %_ANY_FILE%

    SET /A _INPUT_FILES_SIZE=0
    REM If file size is greater than 2GB then the next line causes this message to display ... Invalid number. Numbers are limited to 32-bits of precision.
    REM So putting the mode command immediately after the message will clear it quickly.
    SET /A _INPUT_FILES_SIZE=%_SIZE%
    mode CON:cols=216 lines=60

    echo Input file: [%_ANY_FILE%].
    echo Input file size in bytes: [%_INPUT_FILES_SIZE%]
    echo.

    echo --------------------------------------------------------------------- PS Section

    REM $testNumber = 2147483630; ^


    powershell -command ^
    $maxInt32 = [int]::MaxValue; ^
    $testNumber = %_SIZE%; ^

    if ($testNumber -gt $maxInt32) { ^
    Write-Host "Number exceeds 32-bit maximum!" -ForegroundColor Red; ^
    } else { ^
    Write-Host "Number is within 32-bit range." -ForegroundColor Green; ^
    };

    echo --------------------------------------------------------------------- PS Section

    pause
    exit
    Last edited by JN-; 16th Mar 2026 at 11:59.
    Quote Quote  
  6. Member
    Join Date
    Jun 2022
    Location
    Dublin
    Search Comp PM
    Hi abolibibelot. Good suggestion. A few years back I joined Superuser, for 24 hours. I went to a lot of trouble to prepare my 1st. and only post. They rejected it, so I left. Ok, they have their rules.
    Quote Quote  
  7. Member
    Join Date
    Jun 2022
    Location
    Dublin
    Search Comp PM
    Thanks PCSPEAK. Although your PS script works to display a message on screen, can it be modified to SET a batch file variable instead of the line Write-Host "Number is within 32-bit range." ?

    Ideally say SET _TOO_BIG=YES or SET _TOO_BIG=NO.

    I can then use that "flag" elswhere in code.

    I meant to say that while the “flag” can be set using batch it causes on screen error message if the file is “too big”,which is avoided using PS.
    Last edited by JN-; 16th Mar 2026 at 11:38.
    Quote Quote  
  8. Good suggestion. A few years back I joined Superuser, for 24 hours. I went to a lot of trouble to prepare my 1st. and only post. They rejected it, so I left. Ok, they have their rules.
    Well at least I'm not alone with that perception of that place! It's not exactly newbie friendly indeed... But I did manage to get a few questions approved, and even upvoted... usually, as I said, once I provided the solution myself!

    I'll let “pcspeak” hopefully chime in about your next question...
    Only a brief remark, plus a few incident remarks: “%~n1%~x1” can be simplified as “%~nx1”. Works with other modifiers too: “%~dpnx1” = “%~d1%~p1%~n1%~x1” which is the full path of a file – or a folder, as I found out that when the name of a folder contains a period, what follows is interpreted as an extension in the context of the cmd.exe shell (even though a folder can't have an extension), so it's necessary to use both the ~n and ~x argument modifiers in order to get its whole name.
    Another weirdness I found: when a name contains a comma but no space, the variable gets expanded with no quotation marks, which makes the command fail.
    For instance I made this small script which renames a dragged-and-dropped file based on its modified date:
    Code:
    chcp 1252
    set filepath=%~1
    FOR %%F in ("%filepath%") DO (
    	set drive=%%~dF
    	set name=%%~nF
    	set ext=%%~xF
    )
    FOR /F "tokens=1,2,3,4,5 delims=/: " %%T in ("%~t1") DO set date=%%V%%U%%T%%W%%X
    %drive%
    rename "%filepath%" "%name% [%date%]%ext%"
    It works with a file named “test_x32.bat”, or a file named “test x32.bat”, but it doesn't work with a file named “test,x32.bat”.
    Doesn't work either with:
    Code:
    chcp 1252
    set filepath="%~1"
    FOR %%F in (%filepath%) DO (
    	set drive=%%~dF
    	set name=%%~nF
    	set ext=%%~xF
    )
    FOR /F "tokens=1,2,3,4,5 delims=/: " %%T in ("%~t1") DO set date=%%V%%U%%T%%W%%X
    %drive%
    rename %filepath% "%name% [%date%]%ext%"
    I couldn't find a workaround.
    Quote Quote  
  9. Member
    Join Date
    Jun 2022
    Location
    Dublin
    Search Comp PM
    “%~n1%~x1” can be simplified as “%~nx1”. Works with other modifiers too: “%~dpnx1” = “%~d1%~p1%~n1%~x1”

    Good to know, thanks.
    Quote Quote  
  10. Member
    Join Date
    Apr 2007
    Location
    Australia
    Search Comp PM
    Once we leave the PowerShell environment we lose all the created variables.
    To make the answer persistent we can write the result to a file.

    test2_x32.bat
    Code:
    @echo off
    powershell -command ^
        $maxInt32 = [int]::MaxValue; ^
        $testNumber = 2147483660; ^
    
    if ($testNumber -gt $maxInt32) { ^
        Write-Host "YES" > tmp.txt; ^
    } else { ^
        Write-Host "NO" > tmp.txt; ^
    };
    
    :: Create the env.var '_TOO_BIG' from the content of tmp.txt.
    SET /p _TOO_BIG= < tmp.txt &del tmp.txt
    set _TOO_BIG
    pause
    Cheers.
    Quote Quote  
  11. Member
    Join Date
    Jun 2022
    Location
    Dublin
    Search Comp PM
    Truly excellent!!

    Many thanks PCSPEAK.
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!