How can I continue a BAT on a new profile after restarting

Asked

Viewed 213 times

0

I have seen many sites teaching you how to continue a BAT, but this occurs in the same profile. In my situation I need it to be started in another profile, include when restarting Windows, a new profile will be created and it is in this Pefil that I need.

I couldn’t find anything about some. I’ll be very grateful if someone can help.

  • 1

    Welcome to Stackoverflow in Portuguese. As the name implies, the Official language used here is English. So, can you Please Translate your Question? If you prefer, you may also Ask this same Question in the English Stackoverflow site.

  • I translated as requested.

2 answers

0

One alternative to make a bat/cmd run on the next boot, to continue the tasks/executions needed, is in exploring the key of the "Runonce" record. It is possible to program a single execution of a bat/cmd for the next Windows boot. This key only serves to have the entry run on the next boot once, and after this execution is deleted. 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 "forced looping". With a few lines in a bat/cmd, we can add the entry in the registry with the reg add command and also use the setx, so that after the boot the entry is executed by Windows and even if deleted, the bat/cmd itself will add another entry in it with another name (name+counter)to remain in an execution cycle until necessary. So by reading and writing values in the Windows registry, we can even maintain a "permanent" execution in the registry, rewriting the values of this key and adding it by running this bat/cmd itself, ensuring a new execution in the next boot:

:: 1) To make the bat/cmd script run on the next boot and only 1 time, add this 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 to realize that it has already run in post-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 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

    )

:: 3) To keep looping by re-adding the entries in the record (re-adding in each run, ie in 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 finish this looping, we can establish 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

    ) 

0

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.