Page 1 of 1
Directing command with a pipe to a variable
Posted: 20 Sep 2010 09:18
by tbharber
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"
Re: Directing command with a pipe to a variable
Posted: 20 Sep 2010 09:34
by amel27
use shielding symbol "^" (one level):
Code: Select all
for /f "delims=" %%a in ('Systeminfo /S %ndnum%^| Find "Up Time"') do set myvar=%%a
Re: Directing command with a pipe to a variable
Posted: 20 Sep 2010 10:24
by tbharber
Yep that did it. Thanks!
Re: Directing command with a pipe to a variable
Posted: 20 Sep 2010 10:45
by tbharber
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?
Re: Directing command with a pipe to a variable
Posted: 20 Sep 2010 11:49
by aGerman
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
Re: Directing command with a pipe to a variable
Posted: 20 Sep 2010 15:41
by tbharber
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?
Re: Directing command with a pipe to a variable
Posted: 20 Sep 2010 15:52
by aGerman
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
Re: Directing command with a pipe to a variable
Posted: 20 Sep 2010 17:46
by tbharber
That did it. Thanks again!