How can I modify the code below to have it write to a new dated file after midnight every day?
set host="8.8.8.8"
set curdate="%date:~-10,2%-%date:~-7,2%-%date:~-2,2%"
set filep="c:\ping\ping-%curdate%.txt"
ping -t %host% | cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data! -)&ping -n 2 %host%>nul">>%filep%
If I can get the code to terminate prior to midnight, using some sort of "if time=11:59, then stop script", that should solve my problem. Task Scheduler only has specified values like 12 hours, or 1 day.
(update 5-31-23) I have tested numerous things and posted a work-around as an answer.
Although I haven't found a time-based termination of the batch (at 11:59pm, terminate), I did manage to do a work-around that will be good enough for me.
I made modifications to the first ping command. Instead of "ping -t" I did "ping -w 2000 -n 86400". This makes it only wait 2 seconds for failure, and ping for 86400 times (1 day's worth of seconds, may get slightly less if it pings well all day, or slightly more if request timed out, but close enough).
Next, the Task Scheduler doesn't seem to properly end the task even with all end/stop task after 1 day things are set. To get around this, along with the "timer" on the ping, on the last tab of task scheduler, the dropdown at the bottom, set it to "Run another instance in parallel". This will always start the script at 12:00:05am even if a batch is running, then the previous day's batch will stop after the pings run out.
set host="8.8.8.8"
set curdate="%date:~-10,2%-%date:~-7,2%-%date:~-2,2%"
set filep="c:\ping\ping-%curdate%.txt"
ping -w 2000 -n 86400 %host% | cmd /q /v /c "(pause&pause)>nul & for /l %%a in () do (set /p "data=" && echo(!date! !time! !data! -)&ping -n 2 %host%>nul">>%filep%
I will have to test this on other operating systems and other computers to ensure it functions the same across the board, but I believe it will accomplish what I need it to for now. (still interested to know if time-based termination is possible)