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 the ping
command, but ping.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 name nul
(not null
) 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 understands 1>
and 2>
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 the if
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:
if %errorlevel% NEQ 0 echo The previous command failed!
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:
if errorlevel 1 echo The previous command failed!
The batch file is working correctly. find
won't count a line unless it has the Windows LFCR*
symbols on the line. You need to change the text file to have the LFCR
symbols at the end of each line.
*LFCR
is Line Feed, Carriage Return
.
Best Answer
I want to count the number of lines in a file using batch
Specific solution
From a command line:
From a batch file (countlines.cmd):
Example:
Flexible solution
Use the following batch file (countlines.cmd):
Notes:
%lines%
.echo ...
commands as appropriate.Usage:
Example:
Further Reading