Microsoft Backticks
September 22, 2005 11:49 PM   Subscribe

How do I get the ouput of a windows console program / script into a batch file variable?

Unix user here, trying to translate a shell script into a batch file that will run in a Windows command shell environment. I need to get the output of a program or script into a variable.

In a Unix shell it is easy : var=`program`, but I have not been able to figure out how to do the same in a batch file.
posted by Deepspace to Computers & Internet (6 answers total)
Windows batch files kind of suck, but I think you may be able to output the program to a file with the >> [filename] operator, then bring it back into the script with << [filename], or use that to self-write a batch file that is then called. It's not really an answer but is one area where I'd look.
posted by rolypolyman at 5:45 AM on September 23, 2005

You might consider just shipping your script with a Windows version of bash. It'll make your life a lot easier.
posted by grouse at 7:02 AM on September 23, 2005

Sad to say, but Windows batch language is not much enhanced from its roots in MS-DOS, when it was an operating system that fit on a 360 kb floppy.

If you want to do anything substantial, you'll have to use an external scripting language or command shell. The above suggestion about using a windows implementation of the BASH shell is a good one. If you don't want Microsoft to be dissapointed with you, you can use Windows Scripting Host.
posted by curtm at 7:21 AM on September 23, 2005

from a command prompt "ping > c:\ping.txt" works. It does not work from the Run box. You want to specify the directory path to the save location. I haven't taken the time to figure out how Windows selects what directory is current, but it's certainly not always the one I would expect.
posted by theora55 at 7:26 AM on September 23, 2005

Best answer: You can probably do this in an XP batch file. Read the help for the "for" statement,
   for /?
The example there is:
    FOR /F "usebackq delims==" %i IN (`set`) DO @echo %i
Which prints the environment variable names without their values. You can fool with the delimiters to get different token parsing.

There is a whole bunch of useful information hidden in help strings from the various CMD.EXE builtins, like
  set /?
  if /?
  call /?
You'll have to think differently than bash, but it's pretty powerful.
posted by soundslikeobiwan at 8:26 AM on September 23, 2005

The trick used by many people is setting an environment variable, and then using that. There is just no other variable available to use in a batch file.

Here's an example from a batch file I have on a Win2000 machine to get the date.

@echo off
SET %%PATH = "c:\winnt\system32\"

For /f "tokens=1-7 delims=:/-, " %%i in ('echo exit^|cmd /q /k"prompt $D $T"') do (
For /f "tokens=2-4 delims=/-,() skip=1" %%a in ('echo.^|date') do (
set dow=%%i
set %%a=%%j
set %%b=%%k
set %%c=%%l
set hh=%%m
set min=%%n
set ss=%%o

SET YY=%YY:~2,2%

REM Let's see the result.
For %%i in (dow dd mm yy hh min ss) do set %%i
posted by stovenator at 3:02 PM on September 23, 2005

« Older How can I decide if I'm management material?   |   Why do a majority of gay men speak in a camp voice... Newer »
This thread is closed to new comments.