2

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
5

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.

2
  • Would it be possible to achieve this within one single file?
    – Clacers
    Jun 1 '17 at 14:03
  • Not this way, maybe there is another one possibility. Jun 1 '17 at 14:04
3

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
)
1
  • Couldn't this result in an endless boot loop if a problem occurs?
    – HackSlash
    Jun 8 '21 at 16:03
1

Look at BoxStarter. http://boxstarter.org

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

1
  • 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 Jun 14 '17 at 12:58
1

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 ?

1
  • 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.
    – Stephan
    Jun 8 '21 at 18:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.