Directing command with a pipe to a variable

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Directing command with a pipe to a variable

#1 Post by tbharber » 20 Sep 2010 09:18

I am trying to direct the contents of a command to a variable with the following code:

for /f "tokens=* delims= " %%a in ('Systeminfo /S %ndnum% | Find "Up Time"') do (
set myvar=%%a
)

The problem is because the command has a pipe symbol in it I get the error: "| was unexpected at this time." If I use just the Systeminfo command without the pipe symbol it processes the code ok.

Anyone have any ideas how I can do this with the pipe symbol?

When I goto view the variable the contents should be something like "System Up Time: 2 Days, 19 Hours, 25 Minutes, 50 Seconds"

amel27
Expert
Posts: 177
Joined: 04 Jun 2010 20:05
Location: Russia

Re: Directing command with a pipe to a variable

#2 Post by amel27 » 20 Sep 2010 09:34

use shielding symbol "^" (one level):

Code: Select all

for /f "delims=" %%a in ('Systeminfo /S %ndnum%^| Find "Up Time"') do set myvar=%%a

tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Re: Directing command with a pipe to a variable

#3 Post by tbharber » 20 Sep 2010 10:24

Yep that did it. Thanks!

tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Re: Directing command with a pipe to a variable

#4 Post by tbharber » 20 Sep 2010 10:45

One small issue I have with this command is when I run it and the computer I am checking Systeminfo for is not on the network I get the error on the screen "ERROR: The RPC server is unavailable."

Getting this error is completly fine as later on in the script I have a line that looks for this error and lets the user know it wont work. What I want is for the error not to pop up on the screen. I tried to put a @ at the beginning and a >nul at the end but the error still shows up on the screen. How does one hide the error messages that come up from commands?

aGerman
Expert
Posts: 4693
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Directing command with a pipe to a variable

#5 Post by aGerman » 20 Sep 2010 11:49

The errormessage comes from SYSTEMINFO, thats why you have to redirect it by 2>nul. The ">" must be escaped into the FOR loop (similar to the pipe character).

Code: Select all

for /f "delims=" %%a in ('Systeminfo /S %ndnum% 2^>nul ^| Find "Up Time"') do set myvar=%%a

Regards
aGerman

tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Re: Directing command with a pipe to a variable

#6 Post by tbharber » 20 Sep 2010 15:41

Thanks again! I was putting the 2>nul after the find command which is why I couldnt get it to work. There is one more problem I am having with this code....

Sometimes when the computer is not available the the result comes back "System Up Time: N/A". I want to put a line in so if this line comes up to bring up an messege to the user stating the computer is not available. The problem is when this happens I am getting the error "Up was unexpected at this time."


Here is the code:

set compname=nd1234567
for /f "delims=" %%a in ('Systeminfo /S %compname% 2^>nul ^| Find "Up Time"') do set myvar=%%a
if %myvar%="System Up Time: N/A" echo The computer is not available.



I seems the problem is that %myvar% has the result of System Up Time: N/A. I think because it is not in quotes it is causing a problem.

Any thoughts?

aGerman
Expert
Posts: 4693
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Directing command with a pipe to a variable

#7 Post by aGerman » 20 Sep 2010 15:52

A single equal sign doesn't work and you have to use quotes for the variable too. Try

Code: Select all

if "%myvar%"=="System Up Time: N/A" echo The computer is not available.

Regards
aGerman

tbharber
Posts: 32
Joined: 17 Sep 2010 17:08

Re: Directing command with a pipe to a variable

#8 Post by tbharber » 20 Sep 2010 17:46

That did it. Thanks again!

Post Reply