Is there an autoexit for cmd.exe like there is an autorun?












2














In windows when you create a value autorun in registry key HKEY_CURRENT_USERSoftwareMicrosoftCommand 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.










share|improve this question



























    2














    In windows when you create a value autorun in registry key HKEY_CURRENT_USERSoftwareMicrosoftCommand 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.










    share|improve this question

























      2












      2








      2


      1





      In windows when you create a value autorun in registry key HKEY_CURRENT_USERSoftwareMicrosoftCommand 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.










      share|improve this question













      In windows when you create a value autorun in registry key HKEY_CURRENT_USERSoftwareMicrosoftCommand 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.







      windows batch windows-registry cmd.exe autorun






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 14 at 1:40









      Henrik Jensen

      132




      132






















          1 Answer
          1






          active

          oldest

          votes


















          0














          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:




          • Doskey - SS64.com


          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:




          • Autorun - SS64.com

          • Cmd - SS64.com






          share|improve this answer



















          • 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.
            – Henrik Jensen
            Dec 15 at 2:57








          • 1




            Does not save the full path, only the name of the current directory. So assuming cd = C:USersHenrik cd test_lastdir&&exit works, but cd test_lastdirsubdir&&exit fails.
            – Henrik Jensen
            Dec 21 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.
            – Henrik Jensen
            2 days ago










          • Of course this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work.
            – Henrik Jensen
            2 days ago










          • 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.?
            – Henrik Jensen
            2 days ago











          Your Answer








          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "3"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1383452%2fis-there-an-autoexit-for-cmd-exe-like-there-is-an-autorun%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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:




          • Doskey - SS64.com


          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:




          • Autorun - SS64.com

          • Cmd - SS64.com






          share|improve this answer



















          • 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.
            – Henrik Jensen
            Dec 15 at 2:57








          • 1




            Does not save the full path, only the name of the current directory. So assuming cd = C:USersHenrik cd test_lastdir&&exit works, but cd test_lastdirsubdir&&exit fails.
            – Henrik Jensen
            Dec 21 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.
            – Henrik Jensen
            2 days ago










          • Of course this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work.
            – Henrik Jensen
            2 days ago










          • 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.?
            – Henrik Jensen
            2 days ago
















          0














          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:




          • Doskey - SS64.com


          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:




          • Autorun - SS64.com

          • Cmd - SS64.com






          share|improve this answer



















          • 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.
            – Henrik Jensen
            Dec 15 at 2:57








          • 1




            Does not save the full path, only the name of the current directory. So assuming cd = C:USersHenrik cd test_lastdir&&exit works, but cd test_lastdirsubdir&&exit fails.
            – Henrik Jensen
            Dec 21 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.
            – Henrik Jensen
            2 days ago










          • Of course this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work.
            – Henrik Jensen
            2 days ago










          • 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.?
            – Henrik Jensen
            2 days ago














          0












          0








          0






          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:




          • Doskey - SS64.com


          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:




          • Autorun - SS64.com

          • Cmd - SS64.com






          share|improve this answer














          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:




          • Doskey - SS64.com


          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:




          • Autorun - SS64.com

          • Cmd - SS64.com







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited yesterday

























          answered Dec 14 at 17:21









          Worthwelle

          2,65231125




          2,65231125








          • 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.
            – Henrik Jensen
            Dec 15 at 2:57








          • 1




            Does not save the full path, only the name of the current directory. So assuming cd = C:USersHenrik cd test_lastdir&&exit works, but cd test_lastdirsubdir&&exit fails.
            – Henrik Jensen
            Dec 21 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.
            – Henrik Jensen
            2 days ago










          • Of course this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work.
            – Henrik Jensen
            2 days ago










          • 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.?
            – Henrik Jensen
            2 days ago














          • 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.
            – Henrik Jensen
            Dec 15 at 2:57








          • 1




            Does not save the full path, only the name of the current directory. So assuming cd = C:USersHenrik cd test_lastdir&&exit works, but cd test_lastdirsubdir&&exit fails.
            – Henrik Jensen
            Dec 21 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.
            – Henrik Jensen
            2 days ago










          • Of course this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work.
            – Henrik Jensen
            2 days ago










          • 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.?
            – Henrik Jensen
            2 days ago








          1




          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.
          – Henrik Jensen
          Dec 15 at 2:57






          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.
          – Henrik Jensen
          Dec 15 at 2:57






          1




          1




          Does not save the full path, only the name of the current directory. So assuming cd = C:USersHenrik cd test_lastdir&&exit works, but cd test_lastdirsubdir&&exit fails.
          – Henrik Jensen
          Dec 21 at 8:47






          Does not save the full path, only the name of the current directory. So assuming cd = C:USersHenrik cd test_lastdir&&exit works, but cd test_lastdirsubdir&&exit fails.
          – Henrik Jensen
          Dec 21 at 8:47






          1




          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.
          – Henrik Jensen
          2 days ago




          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.
          – Henrik Jensen
          2 days ago












          Of course this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work.
          – Henrik Jensen
          2 days ago




          Of course this only works when using cd. Other tools that might change directory, like pushd/popd or 3rd. party tools won't work.
          – Henrik Jensen
          2 days ago












          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.?
          – Henrik Jensen
          2 days ago




          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.?
          – Henrik Jensen
          2 days ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Super User!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1383452%2fis-there-an-autoexit-for-cmd-exe-like-there-is-an-autorun%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How do I know what Microsoft account the skydrive app is syncing to?

          When does type information flow backwards in C++?

          Grease: Live!