Batch command line variables:
MyProg.bat a b c d
Gives (without the quotes):
%* = "a b c d"
%~f0 → the fully qualified path + filename with extension of the running batch file
%0 → the name of the script as it was called (MyProg is this case)
%1 = "a"
%2 = "b"
%3 = "c"
%4 = "d"
also:
%~d0  → drive (e.g., C:)
%~p0  → path (e.g., \scripts\)
%~n0  → filename without extension
%~x0  → file extension
%~dp0 → drive + path (very commonly used)
%~dpn0 → drive + path + filename (no extension)
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 → the fully qualified path + filename with extension of the
running batch file
%0 → the name of the script as it was called (MyProg is this case)
%1 = "a"
%2 = "b"
%3 = "c"
%4 = "d"
also:
%~d0  → drive (e.g., C:)
%~p0  → path (e.g., \scripts\)
%~n0  → filename without extension
%~x0  → file extension
%~dp0 → drive + path (very commonly used)
%~dpn0 → 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.
Assigning variable from call in Batch call drive
me crazy. It is STUPID.
On 2026-04-25 13:08, T wrote:
Assigning variable from call in Batch call drive
me crazy. It is STUPID.
And the relevance to your OP and my reply is?
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. 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.
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 ¡÷ the fully qualified path + filename with extension of the running batch file
%0 ¡÷ the name of the script as it was called (MyProg is this case)
%1 = "a"
%2 = "b"
%3 = "c"
%4 = "d"
also:
%~d0 ¡÷ drive (e.g., C:)
%~p0 ¡÷ path (e.g., \scripts\)
%~n0 ¡÷ filename without extension
%~x0 ¡÷ file extension
%~dp0 ¡÷ drive + path (very commonly used)
%~dpn0 ¡÷ drive + path + filename (no extension)
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 → the fully qualified path + filename with extension of the running batch file
%0 → the name of the script as it was called (MyProg is this case)
%1 = "a"
%2 = "b"
%3 = "c"
%4 = "d"
also:
%~d0  → drive (e.g., C:)
%~p0  → path (e.g., \scripts\)
%~n0  → filename without extension
%~x0  → file extension
%~dp0 → drive + path (very commonly used)
%~dpn0 → drive + path + filename (no extension)
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. 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.
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.
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. 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.
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' → runs a command
"file name" → treated as a file name (only works cleanly without spaces)
With usebackq
`command` → runs a command
'file name' → treated as a file name (even with spaces)
"literal string" → treated as a plain string
THANK YOU!!!
On 4/25/26 17:51, Java Jive wrote:
    FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
        SET _CP=%%D
        )
    CHCP 1252
   CHCP 1252
   FOR /F "usebackq tokens=1-4" %%A IN (`CHCP`) DO (
       SET _CP=%%D
       )
   REM Restore original code page
   CHCP %_CP%
   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%
On Sun, 4/26/2026 1:21 AM, T wrote:
   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%
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
The "for" command wold make Rude Goldbreg very happy.
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.
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?
| Sysop: | Scott Duensing |
|---|---|
| Location: | Freeburg, IL, USA, Earth |
| Users: | 5 |
| Nodes: | 16 (0 / 16) |
| Uptime: | 494354:34:54 |
| Calls: | 5 |
| Messages: | 20,594 |