batch-filecharacter-encodingbatch-processingautorun

BATCH file reloading autorun buffer


There are things that we prefer not to understand in order to have an easier life to live. But this is not something I can choose...

I made a batch file (or macro.doskey) to get the charset code. And it worked perfectly for a long time...

Basically it runs chcp:

> chcp
Code page active: 850

and then wraps the return before and after the colon
assigning what comes after to a variable:

FOR /F "tokens=1,* delims=:" %%s in ('CHCP') do (
  @ECHO %%t
  IF NOT "%1" == "" (SET %1=%%t)
)

For example:

> getCHCP.bat myVar
 850
> ECHO %myVar%
 850

However it started to lock, waiting for ENTER or displaying several echo messages. For example:

> getchcp myVar
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
ECHO is off.
 850

I started to mix until I decided to change the ECHO %%t to ECHO %%s, and guess what?

enter image description here

No, is that the Bill Gates skull? Is it an easter egg from Microsoft? A virus? No, none of that, this is just my autorun's welcome message.

This can be configured in <[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]autorun>

In my case I called a batch file which, among other things, gives several echos showing this skull on the screen.


Solution

  • It's obvious, you already point to the problem.

    this is just my autorun's welcome message.

    This can be configured in <[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor]autorun>

    The line FOR /F %%s in ('CHCP') ... start CHCP but that will be done in a NEW child cmd.exe instance.
    And a NEW cmd.exe instance runs the autorun command!
    Just before it starts your chcp.

    You can disable the autorun at all, or add some code to detect the difference between a new cmd.exe instance for the user against a new instance from a FOR /F.

    Put this code at the start of your autorun batch file

    @echo off
    
    setlocal EnableDelayedExpansion
    REM *** ALWAYS make a copy of the complete CMDCMDLINE, else you destroy the originial!!!
    set "_ccl_=!cmdcmdline!"
    
    REM *** %1 contains only data, when the script itself was called from the command line
    if "%~1" NEQ "" (
            goto :direct_call
    )
    
    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 ****
            REM *** Show your skull or something else
    )
    exit /b