10

I am aware that one can do

doskey /history 

to save the command history at a particular point in time, but I wonder if there's a way to proactively save command history to a file, as the commands are being issued.

Once a command prompt is closed, the history is lost, so it's easy to accidentally close a command prompt when one is done.

I'd like to be able to say something like:

log Commands.log

and then issue my commands, and have the commands be saved to Commands.log.

merlin2011
  • 71,677
  • 44
  • 195
  • 329

3 Answers3

6

You could create a doskey macro to remap the EXIT command, as follows:

doskey exit=doskey/history$g$gc:\temp\commands.log$texit $1 $2

This would append the contents of your command history into a file named "c:\temp\commands.log" each time you exit the prompt by typing "exit".

CAUTION: I haven't tested potential side effects of using this with EXIT's "/B exitCode" parameters, but there shouldn't be any since you're capturing the parameters with "$1 $2" anyway.

This doesn't do exactly what you're looking for, but it does capture your command history as long as you exit using EXIT (vs. just closing the window).

More info and samples on DOSKEY macros are here: http://technet.microsoft.com/en-us/library/bb490894.aspx

Marc
  • 11,403
  • 2
  • 35
  • 45
  • The command works well, but you have to specify a directory that already exists - it won't create the directories for you. From what I can tell is that this macro only lasts during the current dos session. So you'd have to type this everytime you open the command prompt. – icc97 Oct 29 '14 at 13:37
  • 1
    If you create shortcut with the following Target: `%windir%\system32\cmd.exe /K doskey exit=doskey/history$g$g%USERPROFILE%\commands.log$texit $1 $2` this a) follows the unix concept of saving it to your home directory, b) this command is run when you run this short cut so that you don't have to type it every time – icc97 Oct 29 '14 at 14:02
1

I know it is late but I can see two alternatives.

Firt clink or something more complete that include cmder

Each time you will open cmd it will be clink and it works great

Novy
  • 1,436
  • 1
  • 14
  • 26
1

As Novy has already said, try Clink; but also use the following link so it also works in admin mode. run cmd with clink as administrator in windows to save your command history permanently

enter image description here

Héctor Oses
  • 71
  • 1
  • 10