This seems like a really easy question (running Windows 7). But I have looked for the answer and couldn't find anything at Microsoft's sites (XP PING & Everything else PING) or from a bunch of Google searches.
I am learning DOS/Batch file. My beginning code is to PING a server.
If I enter this in the command prompt it runs fine, and gives me some results
PING -n 1 Server04
Results:
Pinging Server04.aaaa.xxxx.net [x.x.x.x] with 32 bytes of data:
Reply from x.x.x.x: bytes=32 time=1ms TTL=125
If I run this in a batch file, it never stops, but the -n (or /n) should only send the PING once, and the PAUSE should also stop and hold the cmd.exe window open.
PING -n 1 winsvmnsdev04
PAUSE
Results:
C:\Users\Me\Desktop>PING -n 1 Server04
C:\Users\Me\Desktop>PING -n 1 Server04
C:\Users\Me\Desktop>PING -n 1 Server04
Continues until I use Ctrl + C
I have looked at a bunch of Q&A where many include 'errorlevel' to echo the response and some that have a trailing '> null'. In trying to understand, I have looked and neither of these are listed as attributes of 'PING' in the Microsoft Libraries.
When I run
PING -n 1 winsvmnsdev04 > null
PAUSE
Or run
PING -n 1 winsvmnsdev04 > null
I get one line
C:\Users\Me\Desktop>PING -n 1 Server04 1>null
I have no idea why. Can someone explain these two attributes of PING, or point towards the official documents? /n
and -n
don't not limit to one PING, the >null
and the PAUSE
don't work when the PING is running wild. None of these line up with what the Microsoft Documentation is describing.
Note: errorlevel
is not listed as Command-Line Reference by Microsoft
Best Answer
I suspect that you've named your batch file
ping.bat
, which as explained here would be the source of your problem.If this indeed is the case, what's happening is that
ping
isn't actually running theping
command, butping.bat
with those arguments - sending you into an infinite recursion that doesn't end until you break it.That's why you see the command constantly iterating in Command Prompt without any output when you run the batch file, because it's not actually executing the
ping
command, but calling on itself with those very arguments, which then calls itself, which - well, you get the picture.The
> null
part is a redirection; the>
operator means that the program's output will be written to the following file rather than the console. The special file namenul
(notnull
) is handled by Windows internally; anything you write there is simply discarded. This way a script can run a program and just check if it succeeded, without cluttering the console. (Similarly,<
would redirect input from file.)1>
is the full form of>
and it means "redirect the first file descriptor".On Unix-like operating systems, a file descriptor is a number used by programs to reference files that the program currently holds open. By convention, file descriptors 0, 1, 2 correspond to "standard input" (stdin), "standard output" (stdout), and "standard error output" (stderr); all higher numbers correspond to files that the program itself has opened.
Windows differs from this in many ways – it uses "file handles" and doesn't number them 0-1-2-3... are just a few small differences. However, the
cmd.exe
script interpreter still understands1>
and2>
to mean redirection of regular output and error output specifically, to make things just a little bit easier for those coming from Linux or other Unix-likes.As for the
errorlevel
, it is not a command, but a mode in theif
command. Each program, upon exiting, returns a number called an "exit status" to the system (and to the parent program); by convention, 0 means success, and ≥1 means some sort of failure.In
cmd.exe
, you can access the last command's exit status through the special%errorlevel%
variable, for example:There is another syntax, coming from old Windows and MS-DOS versions that did not have the special variable; it checks if the exit status is equal to or greather than a given number: