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?
-
1Have your script create a temporary file with "time last run" in it? Using something like stackoverflow.com/questions/54632207/…– Mokubai ♦Commented Mar 3, 2023 at 9: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 UkraineCommented Mar 3, 2023 at 9:23
-
1Does How to detect a shutdown or reboot? answer your question?– DavidPostill ♦Commented Mar 3, 2023 at 10:02
-
@DavidPostill: Worth an answer, but should take notice of this answer.– harrymcCommented Mar 3, 2023 at 11:45
Add a comment
|
1 Answer
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.