Then, when any subsequent subprocesses open a command session, they will run into the second line and immediately exit the script! This is because subprocesses automatically inherit the environment variables from the parent process. How does this avoid those unexpected side effects?Īll we're doing is creating an environment variable called AUTOEXEC. Rem remap exit command to save a copy of the command line history to a log before exiting.ĭOSKEY exit=(echo/ ^& echo **** %date% %time% ****) $g$g %USERPROFILE%\commands.log ^& doskey /history $g$g %USERPROFILE%\commands.log ^& ECHO Command history saved, exiting ^& exit $* Here is what you put in the autoexec.bat file: off So yes, this batch file will run every time a CMD session is opened, but we'll do a trick to make sure it doesn't run in any subprocesses. Then change the value of HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun to this: IF EXIST "%USERPROFILE%\autoexec.bat" (CALL "%USERPROFILE%\autoexec.bat") While that will work, it will definitely create unexpected side effects when doing any kind of scripting*.Ĭreate a file called autoexec.bat and store it in your profile folder (e.g., C:\Users\yourname\autoexec.bat). Anything you put in this entry will be run when you start a CMD session. The short and dangerous answer is to just add it to the REGISTRY in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor\AutoRun. The tricky part is autoloading that doskey macro when you start a new command session. Unfortunately, you won't be able to easily go back to any of these entries by pressing the up arrow, but you can examine the file at any point to see what you did in the past. This creates a macro that remaps the EXIT command to copy the command line history into your user profile folder (e.g., C:\Users\yourname\commands.log). This simple macro will save the command line history when you exit a CMD session: doskey exit=(echo/ ^& echo **** %date% %time% ****) $g$g %USERPROFILE%\commands.log ^& doskey /history $g$g %USERPROFILE%\commands.log ^& ECHO Command history saved, exiting ^& exit $* If all you want to do is save your command line history at the end of every session, here's a simple way to do it:Īs other answers indicated, doskey allows you to list the command line history for the current session.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |