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)
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.-a It is STUPID.
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?
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.
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)
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)
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.
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!!!
On 4/25/26 17:51, Java Jive 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
-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%
-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%
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
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: | Amessyroom |
|---|---|
| Location: | Fayetteville, NC |
| Users: | 65 |
| Nodes: | 6 (0 / 6) |
| Uptime: | 03:55:45 |
| Calls: | 862 |
| Files: | 1,311 |
| D/L today: |
796 files (9,096M bytes) |
| Messages: | 264,528 |