0

found this batch file with timer. I'm not very familiar with *.bat files but have foundout the following

THAT 01 is the time in seconds

PING -n 01 127.0.0.1>nul 

however im not sure what the rest means rather then just go and use it could someone please explain what all other items in this snippet do/ represent

thanks

mc coy
  • 1

1 Answers1

8

It's a sneaky sleep statement. I've used it before to get a delay in a cmd.exe script without having to resort to external utilities.

However,

ping -n 21 127.0.0.1 >nul:

will generally give you about a 20 second delay because the first ping goes out immediately (only the subsequent pings are sent after a 1-second delay).

If you try your variant (with 01) without the >nul bit, you'll see it returns immediately. If you try it with 21, it should take about 20 seconds, and you'll see why, hopefully :-).


As to what all the bits mean:

  • ping is the ping executable, meant to check whether you can communicate with a specific IP address (it's less useful than you think since many sites will block ICMP (ping) packets while still allowing real traffic.
  • -n 21 means to try 21 times (with one second between each, although you can change that with another parameter).
  • 127.0.0.1 is the loopback address, basically "this computer". You ping your own computer so that no network delays are introduced (and to not annoy your target if everyone on the planet decided to use slashdot.org for example).
  • >nul just means to send all the output to the bit-bucket so you don't see it on the console.
Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • i gatherd that i have a file that after its containing *.exe is executed deleates its self obviously if i just make a deleate batch that is called by my exe im going to run into the issue of that the file is still in use so I want to time my batch just long enougth for my exe to terminate properly, then the batch kicks in deleats my small program and its self. The only thing though is whats the -n and 127.0.0.1>nul: stand for or dont i need them at all? – mc coy Nov 02 '10 at 07:45
  • See the update where I detail each part. You do need the IP address since it's required for `ping`. The `-n 1` is to make it faster (default for Windows is 4 tries from memory). It seems to me this is potentially rife for a race condition, I'd probably prefer a value a little higher just in case (or a better method altogether). – paxdiablo Nov 02 '10 at 07:50
  • okay well like i stated before im not farmilliar with mutch of the bat method of things. I'm actually just using this on a local computer for local files (no network requierd). ping -n 21 will do fine then? thanks your explaination has been very healpfull – mc coy Nov 02 '10 at 07:53
  • paxdiablo i had change it to 1 to see what this value dose im self taught through trial and error – mc coy Nov 02 '10 at 07:54
  • Oooh, I wouldn't go for 21, that's a 20-second delay that will almost certainly annoy your users :-) Give 3 a shot (2 seconds), that should hopefully be plenty. – paxdiablo Nov 02 '10 at 07:54
  • sorry guys i have one more hrdle i just bumped in why is my /y not working? @ECHO OFF ECHO Deleting all associated files please wait... PING -n 5 rmdir /s/y "C:\test" del %0 – mc coy Nov 02 '10 at 08:16
  • @mc coy RMDIR has no "y" argument. And please don't post questions in comments. – Dr. belisarius Nov 02 '10 at 08:22
  • @mc coy, you need `rmdir /s/q` (q means quiet as in "don't ask) but be _very_ careful. In addition, it appears that your ping command is just `ping -n 5`, it should be `ping -n 5 127.0.0.1>nul:` - you do need the IP address. – paxdiablo Nov 02 '10 at 08:29
  • thanks last question, why would i want the Ip adress for this? afterall im going to use it on several machiens all of which would have difrent Ip adress's – mc coy Nov 02 '10 at 08:32
  • Because ping needs it. It's not an optional argument. You can try it from the command line: `ping -n 5` returns immediately with an error (i.e., you _don't_ get the expected four-second delay). And the loopback address `127.0.0.1` means "this machine" no matter what the _real_ IP address of that machine is. – paxdiablo Nov 02 '10 at 08:34
  • I get it universal IP name/value meaning this machine THANKS ALL – mc coy Nov 02 '10 at 08:42