• Tip: batch command line variables

    From T@T@invalid.invalid to alt.comp.os.windows-11 on Fri Apr 24 23:24:25 2026
    From Newsgroup: alt.comp.os.windows-11

    HI All,

    I do not know if any of your are crazy enough (who me????)
    to program in batch, but ...

    Anyway, I have a nice keeper on command line variables you
    find useful:

    -T


    Batch command line variables:

    MyProg.bat a b c d

    Gives (without the quotes):

    %* = "a b c d"
    %~f0 raA the fully qualified path + filename with extension of the running batch file
    %0 raA the name of the script as it was called (MyProg is this case)
    %1 = "a"
    %2 = "b"
    %3 = "c"
    %4 = "d"

    also:
    %~d0 raA drive (e.g., C:)
    %~p0 raA path (e.g., \scripts\)
    %~n0 raA filename without extension
    %~x0 raA file extension
    %~dp0 raA drive + path (very commonly used)
    %~dpn0 raA drive + path + filename (no extension)
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From Java Jive@java@evij.com.invalid to alt.comp.os.windows-11 on Sat Apr 25 12:16:48 2026
    From Newsgroup: alt.comp.os.windows-11

    On 2026-04-25 07:24, T wrote:

    Batch command line variables:

    MyProg.bat a b c d

    Gives (without the quotes):

    %* = "a b c d"
    %~f0 raA the fully qualified path + filename with extension of the running batch file
    %0 raA the name of the script as it was called (MyProg is this case)
    %1 = "a"
    %2 = "b"
    %3 = "c"
    %4 = "d"

    also:
    %~d0-a-a raA drive (e.g., C:)
    %~p0-a-a raA path (e.g., \scripts\)
    %~n0-a-a raA filename without extension
    %~x0-a-a raA file extension
    %~dp0-a raA drive + path (very commonly used)
    %~dpn0 raA drive + path + filename (no extension)

    The above is a subset of what is available for the index variables in
    'for' loops, which can be read by typing into a command console:

    for /?

    Other useful help pages are ...

    set /?
    if /?

    ... etc.
    --

    Fake news kills!

    I may be contacted via the contact address given on my website: www.macfh.co.uk

    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From T@T@invalid.invalid to alt.comp.os.windows-11 on Sat Apr 25 05:08:13 2026
    From Newsgroup: alt.comp.os.windows-11

    On 4/25/26 04:16, Java Jive wrote:
    On 2026-04-25 07:24, T wrote:

    Batch command line variables:

    MyProg.bat a b c d

    Gives (without the quotes):

    %* = "a b c d"
    %~f0 raA the fully qualified path + filename with extension of the
    running batch file
    %0 raA the name of the script as it was called (MyProg is this case)
    %1 = "a"
    %2 = "b"
    %3 = "c"
    %4 = "d"

    also:
    %~d0-a-a raA drive (e.g., C:)
    %~p0-a-a raA path (e.g., \scripts\)
    %~n0-a-a raA filename without extension
    %~x0-a-a raA file extension
    %~dp0-a raA drive + path (very commonly used)
    %~dpn0 raA drive + path + filename (no extension)

    The above is a subset of what is available for the index variables in
    'for' loops, which can be read by typing into a command console:

    -a-a-a-afor /?

    Other useful help pages are ...

    -a-a-a-aset /?
    -a-a-a-aif /?

    ... etc.



    Assigning variable from call in Batch call drive
    me crazy. It is STUPID.

    Here is my keeper on the subject (speaking of "for" calls):



    Batch example on assigning variables from return values:

    @echo off

    REM Get RDP enabled/disabled value
    for /f "tokens=3" %%A in ('
    reg query "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server" /v fDenyTSConnections ^| find "REG_DWORD"
    ') do set RDP=%%A

    REM Get NLA setting
    for /f "tokens=3" %%A in ('
    reg query "HKLM\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp" /v UserAuthentication ^| find "REG_DWORD"
    ') do set NLA=%%A

    echo Raw values:
    echo RDP=%RDP%
    echo NLA=%NLA%
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From Java Jive@java@evij.com.invalid to alt.comp.os.windows-11 on Sun Apr 26 00:08:01 2026
    From Newsgroup: alt.comp.os.windows-11

    On 2026-04-25 13:08, T wrote:

    Assigning variable from call in Batch call drive
    me crazy.-a It is STUPID.

    And the relevance to your OP and my reply is?

    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From T@T@invalid.invalid to alt.comp.os.windows-11 on Sat Apr 25 16:39:09 2026
    From Newsgroup: alt.comp.os.windows-11

    On 4/25/26 16:08, Java Jive wrote:
    On 2026-04-25 13:08, T wrote:

    Assigning variable from call in Batch call drive
    me crazy.-a It is STUPID.

    And the relevance to your OP and my reply is?


    I was following up on the "for" command you mentioned.

    And since assigning a returned value to a variable is
    bizarre, I included how to do it.

    The relevance is that I was building on what you
    had added. I though you raised a good point.
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From Java Jive@java@evij.com.invalid to alt.comp.os.windows-11 on Sun Apr 26 01:51:19 2026
    From Newsgroup: alt.comp.os.windows-11

    On 2026-04-26 00:39, T wrote:
    On 4/25/26 16:08, Java Jive wrote:
    On 2026-04-25 13:08, T wrote:

    Assigning variable from call in Batch call drive
    me crazy.-a It is STUPID.

    And the relevance to your OP and my reply is?


    I was following up on the "for" command you mentioned.

    And since assigning a returned value to a variable is
    bizarre, I included how to do it.

    The relevance is that I was building on what you
    had added.-a I though you raised a good point.

    Well, that wasn't clear, you seemed to want to rant about something, but alright. Your example seemed to work for me, and returned values 0x01 &
    0x00, but let's try to explain things a bit better by using something
    easier to understand than a REG command as the example ...

    Generally, using FOR /F seems to be the only known way of capturing the
    output of a command into one or more variables. I have a program that performs file maintenance across all my PCs and servers, and here's an
    example from that.

    In order to be able to manipulate files with accented characters, I need
    it to run under codepage 1252, but here in the UK the default is 850.
    Here's a section from it that captures the current codepage so that I
    can restore it later before exiting the BATch file program:

    FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    SET _CP=%%D
    )
    CHCP 1252

    Explanation:

    The raw output of the codepage command is of the form ...

    Active code page: 850

    ... of which we want just the last, 850. The for loop captures this as follows ...

    The /F parameter enables extended options including those that follow.

    The 'usebackq' parameter instructs that the command between back quotes
    (`), here CHCP, should be run, and its output assigned to variables in ascending order:

    1st word of output assigned to given for parameter, so here ...
    %%A=Active
    2nd to next letter, so here ...
    %%B=code
    3rd to the next, so here (note the inclusion of the colon, sometimes
    a nuisance) ...
    %%C=page:
    4th, the one we want, to the next, so here ...
    %%D=850
    ... which the loop body assigns to a variable named _CP.

    The 'tokens' parameter instructs to assign the given pattern of words
    from the results, here 1 to 4, but probably you can see that I could
    have used just "tokens=4", in which case the result I wanted would have
    been assigned directly to %%A, but that wouldn't have shown so clearly
    for this example the incremental way the assignations occur. There is
    also a possibility of using a '*' to assign all subsequent output to a
    single variable, but I had no need to use that here.
    --

    Fake news kills!

    I may be contacted via the contact address given on my website: www.macfh.co.uk

    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From JJ@jj4public@gmail.com to alt.comp.os.windows-11 on Sun Apr 26 10:31:38 2026
    From Newsgroup: alt.comp.os.windows-11

    On Fri, 24 Apr 2026 23:24:25 -0700, T wrote:

    HI All,

    I do not know if any of your are crazy enough (who me????)
    to program in batch, but ...

    Anyway, I have a nice keeper on command line variables you
    find useful:

    -T

    Batch command line variables:

    MyProg.bat a b c d

    Gives (without the quotes):

    %* = "a b c d"
    %~f0 i= the fully qualified path + filename with extension of the running batch file
    %0 i= the name of the script as it was called (MyProg is this case)
    %1 = "a"
    %2 = "b"
    %3 = "c"
    %4 = "d"

    also:
    %~d0 i= drive (e.g., C:)
    %~p0 i= path (e.g., \scripts\)
    %~n0 i= filename without extension
    %~x0 i= file extension
    %~dp0 i= drive + path (very commonly used)
    %~dpn0 i= drive + path + filename (no extension)

    CMD has built-in help for those, using `call /?` command.

    What's missing in CMD's built-in help is redirection and anything related to multiple commands in one line. i.e.: command group, pipe, and conditional command. Including the concept of standard I/O.
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From T@T@invalid.invalid to alt.comp.os.windows-11 on Sat Apr 25 22:03:11 2026
    From Newsgroup: alt.comp.os.windows-11

    On 4/24/26 23:24, T wrote:
    HI All,

    I do not know if any of your are crazy enough (who me????)
    to program in batch, but ...

    Anyway, I have a nice keeper on command line variables you
    find useful:

    -T


    Batch command line variables:

    MyProg.bat a b c d

    Gives (without the quotes):

    %* = "a b c d"
    %~f0 raA the fully qualified path + filename with extension of the running batch file
    %0 raA the name of the script as it was called (MyProg is this case)
    %1 = "a"
    %2 = "b"
    %3 = "c"
    %4 = "d"

    also:
    %~d0-a-a raA drive (e.g., C:)
    %~p0-a-a raA path (e.g., \scripts\)
    %~n0-a-a raA filename without extension
    %~x0-a-a raA file extension
    %~dp0-a raA drive + path (very commonly used)
    %~dpn0 raA drive + path + filename (no extension)


    Whilst we are on the subject of batch programming,
    here is one of my favorite utilities:

    https://github.com/ncruces/zenity/releases

    It allows for all kinds of pop ups.

    A quick example of a "yes", "NO" pop up (the time out
    is in seconds):


    @echo off

    set "RESULT="

    zenity.exe --question ^
    --title="Confirm Action" ^
    --text="Do you want to continue?" ^
    --width=400 ^
    --timeout=10

    rem capture the exit code immediately
    set "RESULT=!ERRORLEVEL!"

    echo Exit code was: !RESULT!

    if "!RESULT!"=="0" (
    echo YES
    ) else (
    echo NO or timeout
    )



    Note:
    %ERRORLEVEL% raA last programrCOs exit code
    set "VAR=%ERRORLEVEL%" raA store it safely
    use !ERRORLEVEL! inside code blocks

    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From T@T@invalid.invalid to alt.comp.os.windows-11 on Sat Apr 25 22:21:11 2026
    From Newsgroup: alt.comp.os.windows-11

    On 4/25/26 17:51, Java Jive wrote:
    On 2026-04-26 00:39, T wrote:
    On 4/25/26 16:08, Java Jive wrote:
    On 2026-04-25 13:08, T wrote:

    Assigning variable from call in Batch call drive
    me crazy.-a It is STUPID.

    And the relevance to your OP and my reply is?


    I was following up on the "for" command you mentioned.

    And since assigning a returned value to a variable is
    bizarre, I included how to do it.

    The relevance is that I was building on what you
    had added.-a I though you raised a good point.

    Well, that wasn't clear, you seemed to want to rant about something, but alright.-a Your example seemed to work for me, and returned values 0x01 & 0x00, but let's try to explain things a bit better by using something
    easier to understand than a REG command as the example ...

    Generally, using FOR /F seems to be the only known way of capturing the output of a command into one or more variables.-a I have a program that performs file maintenance across all my PCs and servers, and here's an example from that.

    In order to be able to manipulate files with accented characters, I need
    it to run under codepage 1252, but here in the UK the default is 850.
    Here's a section from it that captures the current codepage so that I
    can restore it later before exiting the BATch file program:

    -a-a-a FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    -a-a-a-a-a-a-a SET _CP=%%D
    -a-a-a-a-a-a-a )
    -a-a-a CHCP 1252

    Explanation:

    The raw output of the codepage command is of the form ...

    -a-a-a Active code page: 850

    ... of which we want just the last, 850.-a The for loop captures this as follows ...

    The /F parameter enables extended options including those that follow.

    The 'usebackq' parameter instructs that the command between back quotes
    (`), here CHCP, should be run, and its output assigned to variables in ascending order:

    -a-a-a 1st word of output assigned to given for parameter, so here ...
    -a-a-a-a-a-a-a %%A=Active
    -a-a-a 2nd to next letter, so here ...
    -a-a-a-a-a-a-a %%B=code
    -a-a-a 3rd to the next, so here (note the inclusion of the colon, sometimes
    -a-a-a-a-a a nuisance) ...
    -a-a-a-a-a-a-a %%C=page:
    -a-a-a 4th, the one we want, to the next, so here ...
    -a-a-a-a-a-a-a %%D=850
    -a-a-a ... which the loop body assigns to a variable named _CP.

    The 'tokens' parameter instructs to assign the given pattern of words
    from the results, here 1 to 4, but probably you can see that I could
    have used just "tokens=4", in which case the result I wanted would have
    been assigned directly to %%A, but that wouldn't have shown so clearly
    for this example the incremental way the assignations occur.-a There is
    also a possibility of using a '*' to assign all subsequent output to a single variable, but I had no need to use that here.



    Awesome!

    I copied it down in my keeper and added the following:

    rem Switch to a known code page (CHCP command)
    CHCP 1252
    FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    SET _CP=%%D
    )
    REM Restore original code page
    CHCP %_CP%

    Note: you really only need tokens=4 since you're only
    grabbing the last value

    and

    Without usebackq (default behavior)
    'command' raA runs a command
    "file name" raA treated as a file name (only works cleanly without spaces)

    With usebackq
    `command` raA runs a command
    'file name' raA treated as a file name (even with spaces)
    "literal string" raA treated as a plain string


    THANK YOU!!!



    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From Kerr-Mudd, John@admin@127.0.0.1 to alt.comp.os.windows-11,alt.msdos.batch on Sun Apr 26 09:03:48 2026
    From Newsgroup: alt.comp.os.windows-11

    On Sat, 25 Apr 2026 22:21:11 -0700
    T <T@invalid.invalid> wrote:

    On 4/25/26 17:51, Java Jive wrote:
    On 2026-04-26 00:39, T wrote:
    On 4/25/26 16:08, Java Jive wrote:
    On 2026-04-25 13:08, T wrote:

    Assigning variable from call in Batch call drive
    me crazy.-a It is STUPID.

    And the relevance to your OP and my reply is?


    I was following up on the "for" command you mentioned.

    And since assigning a returned value to a variable is
    bizarre, I included how to do it.

    The relevance is that I was building on what you
    had added.-a I though you raised a good point.
    []
    I copied it down in my keeper and added the following:

    rem Switch to a known code page (CHCP command)
    CHCP 1252
    FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    SET _CP=%%D
    )
    REM Restore original code page
    CHCP %_CP%

    Note: you really only need tokens=4 since you're only
    grabbing the last value

    and

    Without usebackq (default behavior)
    'command' raA runs a command
    "file name" raA treated as a file name (only works cleanly without spaces)

    With usebackq
    `command` raA runs a command
    'file name' raA treated as a file name (even with spaces)
    "literal string" raA treated as a plain string


    THANK YOU!!!



    Please note that amb is a much better fit for batch discussions; FU set
    --
    Bah, and indeed Humbug.
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From Java Jive@java@evij.com.invalid to alt.comp.os.windows-11 on Sun Apr 26 14:49:01 2026
    From Newsgroup: alt.comp.os.windows-11

    On 2026-04-26 06:21, T wrote:

    On 4/25/26 17:51, Java Jive wrote:

    What I wrote ...

    -a-a-a-a FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    -a-a-a-a-a-a-a-a SET _CP=%%D
    -a-a-a-a-a-a-a-a )
    -a-a-a-a CHCP 1252

    ... and then, although I didn't include this section of the program in
    my example code because it's trivial, just before exiting the program I
    do ...

    CHCP %_CP%

    ... to restore the original code page.

    Whereas what you wrote ...

    -a-a-a CHCP 1252
    -a-a-a FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    -a-a-a-a-a-a-a SET _CP=%%D
    -a-a-a-a-a-a-a )
    -a-a-a REM Restore original code page
    -a-a-a CHCP %_CP%

    is erroneous, can you really not see that???!!! The purpose of my code
    was to remember the current code page so that I can restore it later,
    then set the code page to the one I want for the running of the program,
    but your code would be setting the code page to be 1252 before asking
    what the current code page is, so by the time you do ask that, it's unnecessary to do so, because you already know it's 1252, because you've
    just set it to be that! Thus you will be unable to restore the original
    code page, because you overwrote it before asking what it was, and your example code is therefore completely pointless.
    --

    Fake news kills!

    I may be contacted via the contact address given on my website: www.macfh.co.uk

    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From Paul@nospam@needed.invalid to alt.comp.os.windows-11 on Sun Apr 26 10:55:39 2026
    From Newsgroup: alt.comp.os.windows-11

    On Sun, 4/26/2026 1:21 AM, T wrote:
    -a-a-a rem Switch to a known code page (CHCP command)
    -a-a-a CHCP 1252
    -a-a-a FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    -a-a-a-a-a-a-a SET _CP=%%D
    -a-a-a-a-a-a-a )
    -a-a-a REM Restore original code page
    -a-a-a CHCP %_CP%


    rem Record the existing code page, for restore later

    FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    SET _CP=%%D
    )

    rem Switch to a known code page (CHCP command)
    CHCP 1252
    ...

    REM Restore original code page
    CHCP %_CP%

    Paul
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From T@T@invalid.invalid to alt.comp.os.windows-11 on Sun Apr 26 14:42:08 2026
    From Newsgroup: alt.comp.os.windows-11

    On 4/26/26 07:55, Paul wrote:
    On Sun, 4/26/2026 1:21 AM, T wrote:
    -a-a-a rem Switch to a known code page (CHCP command)
    -a-a-a CHCP 1252
    -a-a-a FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    -a-a-a-a-a-a-a SET _CP=%%D
    -a-a-a-a-a-a-a )
    -a-a-a REM Restore original code page
    -a-a-a CHCP %_CP%


    rem Record the existing code page, for restore later

    FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
    SET _CP=%%D
    )

    rem Switch to a known code page (CHCP command)
    CHCP 1252
    ...

    REM Restore original code page
    CHCP %_CP%

    Paul

    Elegant. Thank you!
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From T@T@invalid.invalid to alt.comp.os.windows-11 on Sun Apr 26 16:34:48 2026
    From Newsgroup: alt.comp.os.windows-11

    I added Paul's comments to my keeper. I missed what you
    were trying to do because I was concentrating on the "for"
    command and how it can be used to assign values to variables
    from external calls.

    Thank you for the help!

    The "for" command wold make Rude Goldbreg very happy.
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From Andy Burns@usenet@andyburns.uk to alt.comp.os.windows-11 on Mon Apr 27 08:57:21 2026
    From Newsgroup: alt.comp.os.windows-11

    T wrote:

    The "for" command wold make Rude Goldbreg very happy.

    I've used many of its variations over the years, it's quite arcane, I do
    still maintain existing .cmd files, but I never start out writing new
    ones, powershell is typically what i use ...

    I wish MS would bite the bullet and install V7 as the default.
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From T@T@invalid.invalid to alt.comp.os.windows-11 on Mon Apr 27 03:46:21 2026
    From Newsgroup: alt.comp.os.windows-11

    On 4/27/26 00:57, Andy Burns wrote:
    T wrote:

    The "for" command wold make Rude Goldbreg very happy.

    I've used many of its variations over the years, it's quite arcane, I do still maintain existing .cmd files, but I never start out writing new
    ones, powershell is typically what i use ...

    I wish MS would bite the bullet and install V7 as the default.

    what is V7?
    --- Synchronet 3.21f-Linux NewsLink 1.2
  • From Andy Burns@usenet@andyburns.uk to alt.comp.os.windows-11 on Mon Apr 27 12:08:33 2026
    From Newsgroup: alt.comp.os.windows-11

    T wrote:

    Andy Burns wrote:
    powershell is typically what i use ...
    I wish MS would bite the bullet and install V7 as the default.

    what is V7?

    version 7 of powershell, Win11 comes with version 5.1 which MS
    acknowledge is a dead-end ...


    --- Synchronet 3.21f-Linux NewsLink 1.2