Page 1 of 1
Output of command to variable
Posted: 19 Apr 2013 07:44
by wingnut0420
Trying to build a version checking batch file for various programs, one of which is Citrix
Trying to pipe to output of this:
Code: Select all
wmic datafile where name='C:\\Program Files (x86)\\Citrix\\ICA Client\\pnagent.exe' get version
into a variable to run against if statements.
Have tried piping to a temporary text file to then read back in but output is unexpected.
Have tried many variations of
Code: Select all
for /f %%i in ('wmic datafile where name='C:\\Program Files (x86)\\Citrix\\ICA Client\\pnagent.exe') do set _CmdResult=%%i
But get varying results of "%%I was unexpected" and other errors
What am I missing here, is there a simpler way?
Re: Output of command to varialbe
Posted: 19 Apr 2013 09:32
by Sponge Belly
Hi Wingnut and welcome to DosTips!

Read this topic on
WMIC and for /f. Also, you might try using double quotes around the path\to\executable inside the “in (...) clause” of your for /f loop. The apostrophes (') nested inside apostrophes might be causing confusion. Oh, and for /f "delims=" will capture the whole line of output, not just the first token.
HTH!
- SB
Re: Output of command to varialbe
Posted: 20 Apr 2013 01:49
by foxidrive
This is more robust, with a temp file.
Code: Select all
@echo off
wmic datafile where name="C:\\Program Files (x86)\\Citrix\\ICA Client\\pnagent.exe" get version /format:csv |find /v "Node,">file.tmp
for /f "tokens=1,* delims=," %%a in (file.tmp) do if not defined num set "num=%%b"
del file.tmp
echo."%num%"
pause
Re: Output of command to varialbe
Posted: 20 Apr 2013 02:25
by Endoro
The wmic output line ends with "CRCRCRLF".
You can try:
Code: Select all
setlocal enabledelayedexpansion
for /f "tokens=1,* delims=," %%i in ('wmic datafile where name="C:\\Program Files (x86)\\Citrix\\ICA Client\\pnagent.exe" get version ^|find /v "Node,"') do set "_CmdResult=%%j"& set "_CmdResult=!_CmdResult:~0,-3!"
echo(%_CmdResult%
Re: Output of command to varialbe
Posted: 20 Apr 2013 02:54
by foxidrive
Endoro wrote:The wmic output line ends with "CRCRCRLF".
You can try:
Code: Select all
setlocal enabledelayedexpansion
for /f "tokens=1,* delims=," %%i in ('wmic datafile where name="C:\\Program Files (x86)\\Citrix\\ICA Client\\pnagent.exe" get version ^|find /v "Node,"') do set "_CmdResult=%%j"& set "_CmdResult=!_CmdResult:~0,-3!"
echo(%_CmdResult%
With your code I get an error " - Invalid alias verb." from wmic, yet the wmic portion works on the cmd line.
Your code is missing the
/format:csv switch which eliminates the trailing spaces, too.
You can wrap the wmic string in double quotes and use this in the for command ('cmd c "wmic datafile where name="C:\\Program Files (x86)\\Citrix\\ICA Client\\pnagent.exe" get version /format:csv"') but it then fails with an & in the string, so is not universal.
Re: Output of command to variable
Posted: 22 Apr 2013 07:58
by wingnut0420
This is more robust, with a temp file.
Code:
@echo off
wmic datafile where name="C:\\Program Files (x86)\\Citrix\\ICA Client\\pnagent.exe" get version /format:csv |find /v "Node,">file.tmp
for /f "tokens=1,* delims=," %%a in (file.tmp) do if not defined num set "num=%%b"
del file.tmp
echo."%num%"
pause
This was exactly what I was looking for, thank you so much!