Output of command to variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
wingnut0420
Posts: 5
Joined: 19 Apr 2013 07:38

Output of command to variable

#1 Post by wingnut0420 » 19 Apr 2013 07:44

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?

Sponge Belly
Posts: 203
Joined: 01 Oct 2012 13:32
Location: Ireland
Contact:

Re: Output of command to varialbe

#2 Post by Sponge Belly » 19 Apr 2013 09:32

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

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Output of command to varialbe

#3 Post by foxidrive » 20 Apr 2013 01:49

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

Endoro
Posts: 244
Joined: 27 Mar 2013 01:29
Location: Bozen

Re: Output of command to varialbe

#4 Post by Endoro » 20 Apr 2013 02:25

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%

foxidrive
Expert
Posts: 6033
Joined: 10 Feb 2012 02:20

Re: Output of command to varialbe

#5 Post by foxidrive » 20 Apr 2013 02:54

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.

wingnut0420
Posts: 5
Joined: 19 Apr 2013 07:38

Re: Output of command to variable

#6 Post by wingnut0420 » 22 Apr 2013 07:58

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!

Post Reply