1

I have this one batch file that runs:

for /R C:\some\Directory\target\ %%f in (*.bat) do (call "%%f")

It eventually loops over a bat file and has to reboot is there a way for it keeps looping after the reboot? I was reading on SuperUser something like resume however I do not see it implemented so quickly here. Is there any way to implement it? The script is meant to be called and install software on a device, but it stops after the reboot. Is there a way to keep the batch file executing the other software?

3
  • 1
    If you have a Pro Version of Windows you can try putting the script in C:\Windows\System32\GroupPolicy\User\Scripts\Logon and use Group Policy to activate the script.
    – anon
    Commented Jan 5, 2023 at 0:10
  • superuser.com/a/1215057/1737567 Break the script down to 2 or more different (.bat) files (depending on how many loops you want it to have), and then you can make a batch file edit a registry entry so that it includes the second batch file name and path (which needs to be executed when the computer restarts). the registry entry that needs to be altered is this : HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce and the value of that registry entry should be changed to the second batch file location (so on applies to the second batch file too)
    – tabby.sl
    Commented Jan 5, 2023 at 5:52
  • Cool question bro!
    – Io-oI
    Commented Jan 6, 2023 at 2:29

1 Answer 1

2
@echo off

setlocal enabledelayedexpansion
set "_batch_dir=C:\some\Directory\target"
set "_KeyRunOnce=HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce"

if not "%~1" == "" (set "_skp=skip=%~1" && set "_count=%~1")else set "_count=1"

for /f %%i in ('dir /b /s /aa "%_batch_dir%\*.bat" ^| find /v /c ""')do set /a "_total=%%~i"

for /f "%_skp% delims=" %%i in ('2^>nul where.exe /r "%_batch_dir%" "*.bat"')do if !_count! lss !_total! =;(
     reg add "%_KeyRunOnce%" /v "%~n0" /d "\"%~f0\" \"!_count!\"" /f && set /a "_count+=1" );= 1>nul 2>&1 & call "%%~i"

1>nul 2>&1 reg delete "%_KeyRunOnce%" /v "%~n0" /f & endlocal

1. Start by counting the total of your bat files to run

2. Obtained the total, check if the current execution is the initial, interval or last

3. Add a counter and through that, execute the value of the counter and use that indicator for the relevant continuity execution.

4. Define which bat to start, continue and end with the argument and RunOnce key suggested by @tabby.sl

5. Replace For /R to For /F and use counter as argument in RunOnce, and use argument as skip in your For /F skip=Argument

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .