Can't run batch file in Server 2012

1

This site rocks! I am trying to run a simple .bat file on Server 2012 that will ping another server, filter out all the lines except one, and then write to a file for later use (I then want that file sent somewhere, but that is a project for another discussion!).

I have pieced together some commands from searching this forum, and have the commands needed to make this happen. When I run these commands one at a time from ms-dos, it works. However, when it stick them all into a bat file something gets messed up. I don't have much experience with Server os's, so it could be something simple I am missing. Any help would be appreciated.

Here are my instructions

ping 10.18.89.154 >C:\pinglog.txt
FOR /F "skip=8 delims=" %i in (C:\pinglog.txt) do @echo %i >C:\TEMP.TXT
SET /P SHOW= < C:\TEMP.TXT
ECHO %SHOW% >C:\pinglog.txt

And here is the result when run manually:

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

But when I run those commands in a batch file, the cmd window hangs on the first line, shows the below text in the cmd window and produces a blank file...

C:\Users\administrator.WSI\Documents>ping 10.18.89.154 1>C:\pinglog.txt

Any Thoughts?

Clay Whitenack

Posted 2015-06-19T10:22:46.547

Reputation: 13

Answers

1

Can't run batch file in Server 2012

Here is a much simpler way to do what you want (in a one line command):

ping 10.18.89.154 | findstr "Packets" > c:\pinglog.txt

The above command will work as is from the command line or from a batch file.

The following batch file uses google.com as the ping target as I can't ping something on your network with a private IP address.

test.cmd:

ping www.google.com | findstr "Packets" > pinglog.txt

example output:

C:\test>test

C:\test>ping www.google.com   | findstr "Packets"  1>pinglog.txt

C:\test>type pinglog.txt
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

C:\test>

To use your original commands, in the for /f command you need to replace % with %%:

FOR /F "skip=8 delims=" %%i in (C:\pinglog.txt) do @echo %%i >C:\TEMP.TXT

Is there a way to isolate the values in that line, or can you only produce the entire line?

The reason I wanted to bring back the the "received" value only, is if that file is "0" I could trigger an alert based on that.

The following batch file will find the value of "Received" and will output "Ping failed" if that value is 0, otherwise it will output nothing.

As a bonus it doesn't use any files.

You can replace echo "Ping failed" with your alert command when you decide what this should be.

test.cmd:

@echo off
for /f "usebackq skip=8 tokens=7" %%i in (`ping 10.18.89.154`) do (
   set _result=%%i
   goto :done
   )
:done
if "%_result:~0,1%"=="0" echo "Ping failed"

Further reading

%%parameter A replaceable parameter: in a batch file use %%G (on the command line %G)

DavidPostill

Posted 2015-06-19T10:22:46.547

Reputation: 118 938

0

I had the same problem described in the original question. I found no answers. I finally resolved the issue by creating a shortcut to the batch file. When launching the batch file from a shortcut, the file executes as expected. Otherwise, it hangs on the first line.

user2832948

Posted 2015-06-19T10:22:46.547

Reputation: 1

Based on what you've written, I'm guessing you did not have the same problem as the original question. That dealt with syntax, and your question doesn't note a syntax-issue-resolving solution. Please read questions carefully and make sure you have something to actually add to the conversation before you post. – music2myear – 2017-06-12T23:07:36.060

This is part of the original question: "When I run these commands one at a time from ms-dos, it works. But when I run those commands in a batch file, the cmd window hangs on the first line, shows the below text in the cmd window and produces a blank file..." This is exactly the problem I faced and resolved as stated in my original post. The problem I just quoted was not addressed. – user2832948 – 2017-06-14T13:46:35.407

Adding that information to your answer post would make how it fits into this context more clear. – music2myear – 2017-06-14T15:51:59.517

Also, when OP uses the word "hang" they aren't using the correct term. When you run a PING command and output it to text, nothing it written to the terminal while the command is running, as you have redirected all output to the designated text file. This gives the appearance of a hang, but nothing is actually hanging. It is just working in the background. You'll get this behavior with any command that takes time to run whose output you redirect. There can be actual hangs (the program has encountered an error it cannot handle), but OP was not describing a hang, simply unexpected behavior. – music2myear – 2017-06-14T15:55:40.737