How can I continue a BAT in a new profile after rebooting

0

I've seen many websites teaching how to continue a BAT, but this occurs in the same profile. In my situation I need to be started in another profile, including restarting Windows, a new profile will be created and it is in this profile that I need.

I could not find anything about it. I will be very grateful if anyone can help.

    
asked by anonymous 25.05.2017 / 14:03

2 answers

0

I was able to resolve using the% allusersprofile% \ Microsoft \ Windows \ Start Menu \ Programs \ Startup folder

    
26.05.2017 / 14:48
0

An alternative to make a bat / cmd run at the next boot, in order to continue the necessary tasks / executions, you are exploring the "RunOnce" registry key. In it it is possible to program a single execution of a bat / cmd to the next boot of Windows. This key is used only for the entry to be executed at the next boot once, and after this execution is erased. However, it is possible to create a flow structure, which will use the reading of global variables with the reading of Windows registry keys, and according to the values obtained, we have a path to write a new entry in each execution, maintaining a "looping forced". With a few lines in a bat / cmd, we can add the registry entry with the reg add command and also use the setx, so that after boot the entry is executed by Windows and even if it is deleted, bat / cmd itself will add another one entry in it with another name (name + counter), to keep in a run cycle until needed. So by reading and writing values in the Windows registry, we can even maintain a "permanent" execution in the registry, rewriting the values of that key and adding it through the execution of that bat / cmd, guaranteeing a new execution at the next boot: p>

:: 1) To make the bat / cmd script run at the next boot and only once, add that line to it:

 Reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v Rode_Uma_Vez /d "drive:\caminho\completoe\para\-o-bat\-ou-exec\nome-bat-ou-exe"
 setx _loop_ 1

:: 2) For the bat / cmd script notice that it has already run in boot post add these lines:

 for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i /c:"_loop_"') do set _loop_setx_=%%i

 if defined _loop_setx set /a _loop_setx_=!_loop_setx_! + 1 

 if "!_loop_setx_!" geq "1" ( 

     echo/ Já rodou 1 e nao vai rodar mais uma...

     REG delete HKCU\Environment /F /V _loop_

    ) else (

     Reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v Rode /d "drive:\caminho\completoe\para\-o-bat\-ou-exec\nome-bat-ou-exe"
     setx _loop_ 1

    )
To keep looping re-adding the entries in the registry (re-adding in each run, that is, on each boot), add these lines:

 for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i /c:"_loop_"') do set _loop_setx_=%%i

 if defined _loop_setx set /a _loop_setx_=!_loop_setx_! + 1 

 if "!_loop_setx_!" geq "1" ( 

     echo/ Já rodou 1 e vai rodar mais uma...

     Reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v Rode!_loop_setx_! /d "drive:\caminho\completoe\para\-o-bat\-ou-exec\nome-bat-ou-exe"
     setx _loop_ !_loop_setx_!


    ) else (

     echo/ Nao rodou nem 1 e vai entrar no loop no próximo boot...
     setx _loop_ 1
     Reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v Rode!_loop_setx_! /d "drive:\caminho\completoe\para\-o-bat\-ou-exec\nome-bat-ou-exe"

    )

:: 4) To end this looping, we can set a limit, taking advantage of the counter:

for /f "tokens=3 delims=^ " %%i in ('reg query HKCU\Environment ^| findstr /i /c:"_loop_"') do set _loop_setx_=%%i

 if defined _loop_setx set /a _loop_setx_=!_loop_setx_! + 1 


     if "!_loop_setx_!" equ "10" ( 

     echo/ Já rodou 10 e nao vai rodar mais, rodar mais uma...

     REG delete HKCU\Environment /F /V _loop_

    ) else if "!_loop_setx_!" geq "1" ( 

     echo/ Já rodou 1 e vai rodar mais uma...

     Reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v Rode!_loop_setx_! /d "drive:\caminho\completoe\para\-o-bat\-ou-exec\nome-bat-ou-exe"
     setx _loop_ !_loop_setx_!


    ) else (

     echo/ Nao rodou nem 1 e vai entrar no loop no próximo boot...

     Reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\RunOnce" /v Rode!_loop_setx_! /d "drive:\caminho\completoe\para\-o-bat\-ou-exec\nome-bat-ou-exe"
     setx _loop_ 1

    ) 
    
03.12.2018 / 01:27