the auto exec page

Microsoft Windows
Post Reply
npocmaka
Posts: 3
Joined: 2022-Mar-07, 10:01 am

the auto exec page

Post by npocmaka »

Just saw the new autoexec page -> https://ss64.com/nt/syntax-autoexec.html

There are few things that eventually can be added. The registry keys that can start a program at the start are (here's the doc https://learn.microsoft.com/en-us/windo ... istry-keys):

on machine level:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce

for the current user:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce

All of them are managed by RunOnce.exe. The RunOnce keys are more interesting as the RunOnce.exe deletes the values from the registry once the program is ran. The more interesting thing in RunOnce keys is that you can run them in safe mode!!
The command should be prefixed with asterix.
To prevent deletion you can use exclamation (I don't know if the asterix and exclamation can be combined). For all of them limit is 260 chars.

There are also two start up folders - one for the current user and one for the whole machine:


C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp

%USERPROFILE%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup


And with task scheduler - the command line gives more control (though this is covered in the SCHTASKS page):

SCHTASKS /Create /SC ONEVENT /MO ONLOGON /TN ON_LOGON /tr "c:\some.bat"
SCHTASKS /Create /SC ONEVENT /MO ONSTART/TN ON_START /tr "c:\some.bat"

Its possible also to be added data on how to execute scripts on shutdown or logoff - by handling events with SCHTASKS or through registry entries.
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: the auto exec page

Post by Simon Sheppard »

Thanks have updated the page now.
jeb
Posts: 12
Joined: 2023-May-10, 1:28 pm

Re: the auto exec page

Post by jeb »

There could be some info about the behavior of the autostart registry keys.

It should be noted, that these are used not only when a new cmd window opens.
Also for drag&drop actions to a batch file.
It will also be called in FOR /F loops, this can have negative effects to batch files parsing the FOR/F output.
In that cases any additional output should be avoided.

This is a batch file to detect if the autorun is called in interactive mode or by FOR/F, drag&drop

Code: Select all

@echo off
setlocal EnableDelayedExpansion
REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the original!!!
set "_ccl_=!cmdcmdline!"
echo(>CON

REM *** %1 contains only data, when the script itself was called from the command line
if "%~1" NEQ "" (
        goto :direct_call
)
REM echo #0 '!_ccl_!' > CON
REM *** The check is necessary to distinguish between a new cmd.exe instance for a user or for a "FOR /F" sub-command
if "!_ccl_:~1,-2!" == "!comspec!" (
        REM ***** INTERACTIVE ****
        echo ********************************************************************
        echo * AutoRun executed from "%~f0"
        
        echo %DATE% %TIME% comspec >> "%~dp0\started.log"
        REM *** Load doskey macros, when the cmdMacros.mac file exists
        FOR %%M in ("%~dp0\cmdMacros.mac") DO (
                endlocal        
                if exist "%%~M" (
                        doskey /macrofile="%%~M"
                        echo * Macros loaded from "%%~M"
                ) ELSE (
                        echo * Macrofile missing at "%%~M"
                )
                echo ********************************************************************
        )        
) ELSE (
        REM *** This is a FOR command, a call by an explorer click or a drag & drop operation
        REM *** Leaving now
        endlocal
)

exit /b        
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: the auto exec page

Post by Simon Sheppard »

jeb wrote: 2023-Nov-06, 11:31 am There could be some info about the behavior of the autostart registry keys.

It should be noted, that these are used not only when a new cmd window opens.
Also for drag&drop actions to a batch file.
It will also be called in FOR /F loops, this can have negative effects to batch files parsing the FOR/F output.
In that cases any additional output should be avoided.
I've done a few tests, and I can see that drag and drop triggers the AutoRun, but Im struggling to do the same for the FOR command, do you have a simple example?
jeb
Posts: 12
Joined: 2023-May-10, 1:28 pm

Re: the auto exec page

Post by jeb »

A short example

Autorun.bat

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "cmd=!cmdcmdline!"

if "%~1" NEQ "" goto :install

if "!cmd:~1,-2!" == "!comspec!" (
    echo *********** Autorun: %~f0 ***********
    echo !cmd!#
    endlocal
) ELSE if "!cmd:~,22!" == ""cmd.exe" /s /k pushd " (
  echo ******* Hello - Verb *********
) ELSE (
  echo *** Hello - FOR/F ***
)
exit /b

:install
reg add "HKCU\Software\Microsoft\Command Processor" /v "AutoRun" /t REG_EXPAND_SZ /d "%~f0"
Test.bat

Code: Select all

@echo off
for /F "delims=" %%a in ('echo World') do echo ## %%a
Now the test can be done with

Code: Select all

Autorun.bat install
test.bat
Output wrote:C:\Users\jeb>test.bat
## *** Hello - FOR/F ***
## World
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: the auto exec page

Post by Simon Sheppard »

Thanks Jeb that makes sense.

A strange thing Im finding while testing this is that a command like:

Code: Select all

TYPE test.txt | FIND "Smith"
does not appear to trigger the CMD autorun, even though I have always assumed this would be instantiating a new CMD.exe instance based on the handling of escape characters. Perhaps it is just that passing values over to a new process is behaving in the same way.

Im also noticing that a FOR command which does not read any FOR variables does not trigger the AutoRun
e.g.

Code: Select all

for /F "delims=" %%a in ('echo World') do echo Hello Friends
A FOR command which does read a FOR variables will trigger the AutoRun, but only once even if it loops many times.
jeb
Posts: 12
Joined: 2023-May-10, 1:28 pm

Re: the auto exec page

Post by jeb »

Hi Simon,

the pipe behaves differently because when a pipe starts the sub processes it uses quite different options for cmd.exe
test.bat

Code: Select all

@echo off
setlocal EnableDelayedExpansion
echo cmdcmdline=!cmdcmdline!
C:\Users\jeb>echo Hello | test.bat
cmdcmdline=C:\Windows\system32\cmd.exe /S /D /c" test.bat"
The /D is the option for "disables the execution of AutoRun-Commands of the registry"
Simon Sheppard wrote: 2023-Nov-10, 1:04 pm Im also noticing that a FOR command which does not read any FOR variables does not trigger the AutoRun
Your observation isn't quite right.
You don't see the "## Hello - FOR /F ##" text, but you get two lines with "Hello Friends" instead of one.
That's because the FOR /F still processes internally two lines.
In my first example the text "## *** Hello - FOR/F ***" wasn't printed by the autorun script, instead it's printed by the FOR /F "echo ## %%a" expression, therefore it's prefixed by the two "##"

For better testing you can modify the AutoRun.bat, add a line directly after the line "set cmd=!cmdcmdline!":

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "cmd=!cmdcmdline!"
echo ---- THIS IS FROM %~f0 : cmdcmdline='!cmd!' > NUL 
jeb
User avatar
Simon Sheppard
Posts: 191
Joined: 2021-Jul-10, 7:46 pm
Contact:

Re: the auto exec page

Post by Simon Sheppard »

Thanks again Jeb I think I get it now.

Every line of output echoed within the AutoRun script is treated as part of the FOR loop causing the doubled output.

So I'm thinking the sensible way to use AutoRun would be to either redirect any output to a file, a variable or redirect it to &2 (the error stream) so it appears in the console and doesn't affect the FOR loop.
Post Reply