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"
Directing command with a pipe to a variable
Moderator: DosItHelp
Re: Directing command with a pipe to a variable
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
Yep that did it. Thanks!
Re: Directing command with a pipe to a variable
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?
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
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).
Regards
aGerman
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
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?
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
A single equal sign doesn't work and you have to use quotes for the variable too. Try
Regards
aGerman
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
That did it. Thanks again!