What you're trying to do doesn't actually require what you're asking and doesn't work with the workaround provided, so I've split this into two parts.
Saving the last used directory to an environment variable
Create a batch file with the following:
@CD /D %*
@>nul setx _LAST_DIR "%CD%"
Set your AutoRun to the following:
CD /D %_LAST_DIR%&doskey cd=@ECHO off$T<FILENAME> $*$T@ECHO on
<FILENAME> is the full path to the batch file you just created. Alternatively, create the batch file in a directory listed in your
PATH variable so you can just use the filename.
- This is multiple commands, separated by the
& character, which allows commands to be run in sequence.
CD /D %_LAST_DIR% will change the directory to the contents of the
_LAST_DIR environment variable. The
/D switch is used to allow changing to a directory on a different drive.
doskey cd=@ECHO off$T<FILENAME> $*$T@ECHO on creates a
DOSKEY macro that does several things.
doskey cd= will create a macro that replaces the default CD command
$T is the
DOSKEY equivalent of
& and is used to separate multiple commands.
$* is the
DOSKEY equivalent to
%* and holds all of the arguments passed to the macro.
- The macro will turn off
ECHO for less clutter, change the directory, set the environment variable and then turn
ECHO back on.
- Since we're using a batch file inside the
DOSKEY macro, we can use the
%CD% variable to give us the full directory path and account for errors.
This portion doesn't do what you want, but others using similar search terms may find it useful.
As far as I can find, this option doesn't exist and isn't mentioned in Microsoft's documentation. Here is a potential work around.
Warning: Do not use this code if you don't understand it as you could end up with an infinite loop of command prompts starting. This should be thoroughly tested before being used in any sort of production environment.
Create a batch file with the following code and set it as your AutoRun:
<exit commands here>
@ character at the start of each line, as you already know, will hide the commands themselves and show only any output. This is to reduce clutter.
cls will clear the header containing the Windows version and copyright information. If you want to clear it from both consoles, you can replace the next command with
cmd /d /k.
@cmd /d will start a new (child) command prompt inside this one and keep it open for you to run whatever commands you choose. The
/D switch tells the command prompt not to use the AutoRun.
- When the child command prompt exits, it will continue running this batch file.
<exit commands here> should be replaced with whatever commands you want to run. This will not be able to access any environment variables or variable changes that are local to the child command prompt. Even
SETX will only apply to future command prompts, but not be passed back to the parent command prompt.
Note: If the command prompt is terminated abnormally (by clicking the red X in the window or by ending it's task in Task Manager), it will not run your exit commands.