JSI Tip 3950. How do I determine when the computer was restarted, in batch?

When you restart your computer, the date and time on your paging file, C:\Pagefile.sys is reset.

Using the technique from the universal date parser, I have scripted WhenRestarted.bat, which contain:

@echo off
set dtetme=N
for /f "Skip=4 tokens=1-5 delims=.:/-, " %%i in ('dir c:\pagefile.sys') do call :parse %%i %%j %%k %%l %%m
endlocal&@set mm=%mm%&@set dd=%dd%&@set yy=%yy%&@set hh=%hh%&@set min=%min%
goto :EOF
IF "%dtetme%" EQU "Y" goto end
set dtetme=Y
set hh=%4
Set min=%5
for /f "Skip=1 Tokens=2-4 Delims=/-,()." %%x in ('echo.^|date') do set %%x=%1&set %%y=%2&set %%z=%3
If you were to issue the @echo mm=%mm%, dd=%dd%, yy=%yy%, hh=%hh%, min=%min% command in the batch, or in the same CMD session, you would receive output similar to mm=08, dd=07, yy=2001, hh=23, min=46.

You can either use these environment variables directly, or use the General purpose date math routine and /or Time Math.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.