1.

Solve : Cmd.exe history?

Answer»

Came across this while looking for something SIMILAR, and felt horror rise as I read down the replies..

*** DO NOT DO SOMETHING LIKE WAS SUGGESTED BELOW ***
Code: [Select]@echo off

for /f "tokens=*" %%a in (session.txt) do (
%%a
)

pause
In the first place, it will NOT work as intended: Since the commands imported from the file are running from within a SCRIPT, they will NOT be added to the current command histry anyhow. Only the script/batch file itself will be added to the command history, which is useless.

Second, the script/batch file is DANGEROUS, because it tries to EXECUTE each command.. what if there's something like "DEL /F /S /Q *.*" in the history? Whatever directory you're in will suddenly and quietly be deleted.. This would be bad if you were in C:\Windows, for example.. Unless you really want to practice re-installing Windows!

Long story short, you can export the current command history with something like:
Code: [Select]doskey /history >> history.txtBut there's no way to import it back in (and safely!) without resorting to installing some other command shell, such as ConEmu, Console2, or even Bash for Windows, or some such.

And yes, I know this topic is old, but I wanted to make sure the danger of the script given was pointed out, before someone else stumpled upon this as I did, but did not recognise how dangerous the script is.
I like how several users seemed to think that "Just run all the commands again" was some sort of brilliant workaround, myself, completely ignoring that commands tend to have side effects.

Interesting sidebar- the Doskey /History is actually 'merely' using an undocumented Windows function in Kernel32.dll, so it is something any program attached to a console can retrieve. Presumably, it was not intended for USE (thus why it remains undocumented- it isn't even exported by name anymore...) which may be why there is no corresponding function to set the Command History.

EDIT: also, welcome to the forum
It's easy to save the history with
Code: [Select]doskey /history
Restoring it is much more complicated.

With the help of EMBEDDED vb-script code and the Send-Key function this should work.

Code: [Select]set /p dummy=
---
Parallel vb-script to send the keystrokes
The trick is that the input of a SET /p goes also into the doskey history, but unfortunately this doesn't work with pipes.
So this has no effect
Code: [Select]echo MyTestCommand | set /p dummy=
I'm sure there was a discussion (and solution) about this topic at dostips.com, but I can't find it anymoreYes. You could count the lines in the history text file. Then use a FOR /L and redirect the file into the FOR and capture it with SET /P.Oldest necromanced thread of the Month finalist...



Discussion

No Comment Found