set a variable to the output of a command

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi All,

I need to get the output of a command and use it later in a batch file. So I
want to set the output string to a variable in the batch file. for example, I
want to set the current date to a batch variable called dd. I have tried the
following but does not work:

date /T | set /p dd=

Can anyone please help?
 
Plus, I don't want to use a file. I know the following works:

date /T > test.txt
set /p dd= < test.txt

Please let me know if there is another method to achieve the same result
without using the test.txt file.
 
Michelle said:
Hi All,

I need to get the output of a command and use it later in a batch file. So
I
want to set the output string to a variable in the batch file. for
example, I
want to set the current date to a batch variable called dd. I have tried
the
following but does not work:

date /T | set /p dd=

Can anyone please help?

Best help generally : see group alt.msdos.batch.nt - especially Timo Salmi's
occasional FAQ ("Hints and Tricks...")

WRT your 'output of a command' question:

Generally,

for /f "tokens=*" %%i in ( ' commandname parameters ^|find "keyword" ' ) do
set varname=%%i

* spaces around the single quotes for emphasis only
* "^|find..." to filter on a single item given that commandname will produce
multiple lines of output

Specifically WRT date:

1. %date% is a system-instantiated variable
* Caution - its format depends on the USER-defined short-date format. Not
only can it be "day mm-dd-yy" but also "dd/mm/yy" amongst other options.
- Get a substring using %date:~m,n% where m is the start-position (0-based)
and n is the length
* see SET /? from the prompt (or generally commandname /?) for help
2. See the afore-mentioned FAQ for many, many examples.
 
Schoolwork alert.

Plus, I don't want to use a file. I know the following works:

date /T > test.txt
set /p dd= < test.txt

Please let me know if there is another method to achieve the same result
without using the test.txt file.
 
Back
Top