Resume batch script after reboot

15,634

Solution 1

This script is a very good solution! I tried it and can confirm it works!

However I'd recommend changing %~n0 and %~dpnx0 to "%~n0" and "%~dpnx0" to prevent Regedit Syntax errors.

@echo off
call :Resume
goto %current%
goto :eof

:one
::Add script to Run key
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /d %~dpnx0 /f
echo two >%~dp0current.txt
echo -- Section one --
pause
shutdown -r -t 0
goto :eof

:two
echo three >%~dp0current.txt
echo -- Section two --
pause
shutdown -r -t 0
goto :eof

:three
::Remove script from Run key
reg delete HKCU\Software\Microsoft\Windows\CurrentVersion\Run /v %~n0 /f
del %~dp0current.txt
echo -- Section three --
pause
goto :eof

:resume
if exist %~dp0current.txt (
    set /p current=<%~dp0current.txt
) else (
    set current=one
)

Solution 2

One way would be to create a registry key HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\ by pointing to script to run on startup. The scenario would be:

  • script01.bat does his job and write HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\ with value "path to script02.bat"

  • script02.bat does his job and write HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce\ with value "path to script03.bat" and so long.

Solution 3

Look at BoxStarter. http://boxstarter.org

This should get you what you need for reboot and continue.

Solution 4

Sorry for the late answer. Couldn't it be achieved with scheduled tasks ? Like create a scheduled task that runs once after reboot and launches the script where you want to resume it ?

Share:
15,634

Related videos on Youtube

Clacers
Author by

Clacers

Updated on September 18, 2022

Comments

  • Clacers
    Clacers over 1 year

    I want to write a single script (one double klick) that automatically executes a predefined set of commands but with some controlled reboots in between to prevent interdependencies. How to accomplish that?

    Example:

    command 1
    reboot
    command 2
    reboot
    command 3
    reboot
    
  • Clacers
    Clacers almost 7 years
    Would it be possible to achieve this within one single file?
  • Leo Chapiro
    Leo Chapiro almost 7 years
    Not this way, maybe there is another one possibility.
  • ferventcoder
    ferventcoder almost 7 years
    Not sure it's actually that complicated in the user interface - but if the script above works, use it. If it has issues and you start needing something more resilient with pending reboot checks and the like, you will end up writing something much more complex than what you get out of the box with Boxstarter. Something to keep in mind. HTH
  • HackSlash
    HackSlash almost 3 years
    Couldn't this result in an endless boot loop if a problem occurs?
  • Stephan
    Stephan almost 3 years
    Hm. That looks more like a comment asking for clarification than an answer. The idea is very good, though. Turn the questions into statements and maybe even show how it's done (I guess you're thinking of schtasks /create ...) and I'll happily upvote.