Command Redirection to Output File.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Posts: 3
Joined: 16 Feb 2015 09:20

Command Redirection to Output File.

#1 Post by geelsu » 16 Feb 2015 09:25

This was suppose to be easy, but alas ...
I found this little bit of code on the Internet to get me started and this works ok for my needs:
(This is not my code and I wish I could find the original location so I could give the author credit. If I do I will update this post.)

Code: Select all

if exist C:\temp\IPs.txt goto to IP_PING
echo Cannot find C:\temp\IPs.txt
goto EOF:

echo Pingtest on %date% at %time% > c:\temp\IP.out
echo ############################## >> C:\temp\IP.out

for /f %%i in (c:\temp\IPs.txt) do call :Sub %%i
echo Testing %1
set state=alive
ping -n 1 %1 | find /i "Approximate round trip" || set state="No Response to Ping"
echo %1 %state% >> C:\temp\IP.out

Here is where I started trying to expand on the above and ran into difficulty. Lets make this more useful for doing some inventory work, I thought ....

Code: Select all

if %state% == alive (
wmic /node=%1 csproduct get identifyingnumber >> C:\temp\IP.out

That's it. Can't get it to work. I've tried using cmd /c, all sorts of quoting, echoing, and such. It just goes to the screen and not the file. I've tried to figure out out the Batch File command substitution methods. (Sorry I have a Linux background so it is sort of like Martian to me). What would be ultimately cool would be syntax, like the ping find statement above, where it wrote to the file output like:

Identifying Number: RTY34IV
Identifying Number: Not found

Don't laugh. I know this is probably simple for some of you. But I'm just taking my first few baby steps in Batch Land.
Last edited by Squashman on 16 Feb 2015 10:11, edited 1 time in total.
Reason: MOD EDIT: Please use CODE TAGS.

Posts: 925
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Command Redirection to Output File.

#2 Post by ShadowThief » 16 Feb 2015 10:08

Code: Select all

%state% == alive

The spaces are breaking your code. Batch takes spaces as literal values, so you're checking to see if the value of %state% with a space on the end is equal to a space followed by the word "alive," which it can never be. Remove the spaces and wrap both values in some character like quotes or brackets (not parentheses, though) and it should work.

Code: Select all


Posts: 3
Joined: 16 Feb 2015 09:20

Re: Command Redirection to Output File.

#3 Post by geelsu » 16 Feb 2015 10:24

White Space!!! Who would have thunk. Thanks ShadowThief. Now just to get the NewLines to embed correctly in my output and we'll be looking good. Kind of got some lines running together after multiple pings and wmic commands.

Post Reply