Skip to content

Instantly share code, notes, and snippets.

@retorillo
Created October 28, 2015 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save retorillo/ddeeb7086c1251951e4c to your computer and use it in GitHub Desktop.
Save retorillo/ddeeb7086c1251951e4c to your computer and use it in GitHub Desktop.
@echo off
setlocal
for /F %%a IN ('echo %1^|findstr /R ^[0-9][0-9]*$') DO set arg1num=%%a
IF "%1"=="%arg1num%" (
doskey /history | findstr /N /R .* | findstr /B %1: > %TEMP%\__history.tmp
FOR /F "delims=: tokens=1*" %%a IN (%TEMP%\__history.tmp) DO @echo %%b
) ELSE (
IF "%1"=="" (
doskey /history | findstr /N /R .*
) ELSE (
doskey /history | findstr /N /R %1
)
)
@retorillo
Copy link
Author

コマンドプロンプトのコマンド履歴をファイルに保存する際に便利なバッチファイルです (MS-DOS, cmd.exe, Windows)

  • history としてすべての履歴を番号付きで列挙できます
  • history 10 と番号を指定することで、その番号の履歴が取得できます
  • history 10 > output.bat としてファイルに出力ができます
  • history re*ex などと正規表現を指定してフィルタリングができます(findstrに利用できる正規表現のみに対応しています)

コマンドプロンプトに打ち込んだコマンドをそのまま保存できたらなと思いバッチファイルの周作で作ったものです。ご自由にお使いください。

既知の問題

この問題について、もし詳しい方がいらしたらご指摘いただけるとありがたいです

このバッチを次のようにパイピングしないでください。上記の通りフィルタリングは history reg*xが使えます。

history | findstr /R reg*x

原因がよくわからないのですがパイピングを行うと doskey の結果が失われてしまうようです。

たとえば 以下のように doskey /history とだけ記述したbatファイルもパイピングによって doskey /history の出力結果がうまく受け渡せません。

REM doskeyonly.bat
@doskey /history

doskeyonly.bat はただしく表示されますが、doskeyonly.bat | more はなにも表示されません。
同じことを dir などで行うと当然出力されます

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment