Title description as stated. I'm very novice at this, so please go easy. I've searched articles here, and have tried several iterations of how to do this but my attempts have failed. I need to run Netstat -anbo every 30 minutes to a text file and keep them rolling for at least 5 files. Thanks.
Asked
Active
Viewed 2,887 times
2 Answers
0
Here's how you can run something every few secs in batch script, without the 'sleep' utility.. I will leave the implementation up to you :)
@ :loop
REM "do something every 10 secs"
@ ping localhost -n 11 > nul
@ goto loop
The ping acts as a sleep here.. you can even ping to any non-existent host.
If you want a more accurate time interval, you are better off using
@ :loop
REM Execute the MS-DOS dir command ever 20 seconds.
@ SLEEP 20
@ GOTO loop
For this to work, you will need to have a sleep MS-DOS utility on the computer. You can find it here

Shaunak
- 17,377
- 5
- 53
- 84
-
So, my batch would look something like this? : @ :loop REM "netstat -anbo > c:\folder\text.txt every 1800 secs" @ ping localhost -n 11 > nul @ goto loop – user3656918 Dec 16 '14 at 15:25
-
your would have to adjust the time interval and replace the REM statement with what ever your want your script to do after every n seconds.. – Shaunak Dec 16 '14 at 15:27
-
why would i need the ping if it is already running netstat every 1800 sec? – user3656918 Dec 16 '14 at 15:28
-
also, will this write to the same file each time or create copy files? i need to retain each file. – user3656918 Dec 16 '14 at 15:30
-
there's no logic for writing to file here, I leave it up to you to do exact implementation. The ping, is just used to simulate sleep. Code halts for it to execute. It has nothing to do with your main code, which will replace line 2. – Shaunak Dec 16 '14 at 15:32
-
downvote for using ping -n x which won't actually delay x seconds the execution of batch, -n as stated by ping -? is the "Number of echo requests to send." which may run in less than 10 seconds. It would be better to use timeout n, n being the number of seconds to wait/pause the batch, i.e.: timeout 120 would stop execution for 2 minutes (unless stopped by any key press) – Jhon Dec 16 '14 at 16:18
-
you are right. Intention was to show how to get delay effect, which ping still would. I have updated the answer to use a more elegant sleep utility, which will have to be installed of course.. Thanks for pointing it out.. – Shaunak Dec 16 '14 at 16:32
-
thanks. I believe I understand this better. However, I'm unable to install any additional utilities on this server (not mine) and have to work with existing utilities. That being said, is there not a simple command to just wait the timeframe inbetween (1800 seconds)? – user3656918 Dec 16 '14 at 17:57
-
there is, in my first solution just replace 11 in the ping statement with about 1810 :) or use @JohnAlx's wait solution and use timeout /t 1800 /nobreak – Shaunak Dec 16 '14 at 18:54
0
You would have to use a simple loop and timeout
A sample implementation
@echo off
:loop
netstat -anbo >> file.txt # >> Will append to file instead of erasing file content
timeout /t 1800 /nobreak # /t timeout in seconds /nobreak Ignore key presses and wait for specified time
goto :loop #Simple loop to keep batch running indefinitely
Remember to run this batch as administrator (For netstat)

Jhon
- 582
- 7
- 28
-
I tested the above, set T to 60. It creates my batch, but doesn't seem to be running the netstat as the file is blank. I also waited an additional 60, and again still blank. Thoughts? – user3656918 Dec 16 '14 at 18:06
-
Ummm, check where the file is being written, maybe it has not permissions to write on that location, change the path to desktop or something like that. I tested on my machine, running as administrator and file was written to system32 folder with no problem. – Jhon Dec 16 '14 at 20:02