Following this answer I'm using a batch file to copy some files from my C: drive to another drive on shutdown. Due to the amount of the files this may take up to a few minutes. This is not a problem if I really shutdown my machine. But sometimes I need to just reboot and the batch file is then executed as well. Is there any possibility to find out in my batch file whether I'm rebooting or shutting down?
Asked
Active
Viewed 170 times
0
-
1Have your script create a temporary file with "time last run" in it? Using something like https://stackoverflow.com/questions/54632207/time-comparison-in-windows-batch-script – Mokubai Mar 03 '23 at 09:18
-
@Mokubai That won't help. If my script ran last yesterday, I want it to run when I do a shutdown today but not if I reboot today. – Bill Tür stands with Ukraine Mar 03 '23 at 09:23
-
1Does How to detect a shutdown or reboot? answer your question? – DavidPostill Mar 03 '23 at 10:02
-
@DavidPostill: Worth an answer, but should take notice of this answer. – harrymc Mar 03 '23 at 11:45
1 Answers
2
How can I distinguish a shutdown from a reboot?
You can use the following script:
@echo off for /f "tokens=3 delims= " %%i in ('wevtutil qe system /c:1 /rd:true /f:text /q:"*[System/EventID=1074]" ^| findstr /c:"Shutdown Type"') do ( set shutdownType=%%i ) if ["%shutdownType%"]==["shutdown"] ( :: your shutdown code here ) else ( :: if not a shutdown, do something else )
Source: How to detect a shutdown or reboot?
As per comment from harrymc please also read and take note of this answer.
DavidPostill
- 156,873
