2

In windows when you create a value autorun in registry key HKEY_CURRENT_USER\Software\Microsoft\Command Processor and set it to, say, echo Hello from autorun, invoking cmd.exe will, on invocation, first execute that line.

Is there an equivalent autoexit-like value to run on closing cmd.exe?

I have tried autoexit, autoclose, autoquit, exit, onexit, onquit and quit with no luck.

What I want to achieve is to save the current directory on exit of cmd.exe by setting the (imaginary) autoexit registry value to setx _LAST_DIR "%cd%", so that I can recall it on next invocation with cd "%_LAST_DIR%" or in a Command prompt Last Dir.lnk file with the Start in input box set to %_LAST_DIR%.

My current solution is to have a batch file in the PATH, myexit.cmd:

@setx _LAST_DIR "%CD%"
@exit

that I have to remember to invoke instead of exit, if I want to save the directory.

1 Answer 1

1

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

Where <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.

The breakdown:

  • 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.

Further reading:

Autoexit

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:

@cls
@cmd /d
<exit commands here>
@exit

The breakdown:

  • The @ 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.

Further reading:

5
  • 1
    Don't think it works. cmd /d starts a new child shell and on return the parent shells %CD% is restored to where it was before calling cmd /d. Dec 15, 2018 at 2:57
  • 1
    Does not save the full path, only the name of the current directory. So assuming cd = C:\USers\Henrik\ cd test_lastdir&&exit works, but cd test_lastdir\subdir&&exit fails. Dec 21, 2018 at 8:47
  • 1
    Rev. 7 now seems to work except for directories with spaces in them.Just need to enclose %CD% in double quotes in the batch file. Great work Worthwelle. Wasn't allowed to do it myself as the system requires a minimum of 6 characters of change and the change was only 2. Dec 25, 2018 at 3:09
  • Of course this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work. Dec 25, 2018 at 3:14
  • Edit of my previous comment. I would vote your answer up if I could. (less than 15 reputation). Regarding a backdraw of this solution,- this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work. But it's an ok compromise for emulating bash's .bash_logout which actually was what I was searching for. I'll accept the solution when the change s/%CD%/"%CD%"/ is added. Thx a bunch for the work and effort. PS. What is that stupid rule that comment only can be edited for 5 minutes.? Dec 25, 2018 at 3:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.