0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

windowsのコマンドプロンプトでhash一覧作成するバッチ例

Last updated at Posted at 2024-09-27

単発小ネタ Windowsバッチ

20240928_windowsのコマンドプロンプトでhash一覧作成するバッチ例

機能

  • 実行すると、バッチの実行ディレクトリ以下再帰的に全てのファイルのhashをログに記録する。
  • certutil -hashfileを利用。SHA1以外も指定できるので任意で変更してください。
  • ファイル名的に無理だったり、オープンされたファイルはエラーになるなどあるので、ログ見て別途個別に確認いただく感じでご了承ください。
get_hashlist_v001.bat
@echo off

rem **** **** **** **** **** **** **** **** **** **** **** 
rem **** **** **** **** **** **** **** **** **** **** **** 
echo.
echo ################################################################
echo.
echo ハッシュ値収集バッチです。実行しますか?
echo 中止する場合はCtl+C
echo 実行する場合は
pause

echo.
echo 実行します
echo.
echo ################################################################
echo.

echo.
echo ************ 処理開始 ************
echo.

rem **** **** **** **** **** **** **** **** **** **** **** 
rem **** **** **** **** **** **** **** **** **** **** **** 
rem 時間取得
set CDATE=%date:~-10,4%-%date:~-5,2%-%date:~-2,2%
set CTIME=%time: =0%
set CTIME=%CTIME:~0,2%-%CTIME:~3,2%-%CTIME:~6,2%

rem ログファイル設定
set CDIR=%~dp0
set LFILE=%CDIR%hashlist_%CDATE%_%CTIME%.txt
set LFILEL=%CDIR%hashlist_%CDATE%_%CTIME%_lite.txt
set LFILELODD=%CDIR%hashlist_%CDATE%_%CTIME%_lite_odd.txt
set LFILELEVN=%CDIR%hashlist_%CDATE%_%CTIME%_lite_evn.txt

echo. >> %LFILE%
echo search : %CDIR% >> %LFILE%
echo start  : %DATE% %TIME% >> %LFILE%
echo ******************************************** >> %LFILE%

rem **** **** **** **** **** **** **** **** **** **** **** 
rem **** **** **** **** **** **** **** **** **** **** **** 
for /f "delims=;" %%i in ('dir /s /b /a-d') do (
  echo. >> %LFILE%
  certutil -hashfile "%%i" >> %LFILE%
  if ERRORLEVEL 0 (
    echo > nul
  ) else (
    echo err : %%i >> %LFILE%
  )
)

echo. >> %LFILE%
echo ******************************************** >> %LFILE%
echo end    : %DATE% %TIME% >> %LFILE%
echo by %0 >> %LFILE%

rem findstr /v "hashfile" %LFILE% | findstr /r /c:"[a-z]" | findstr /v "^search" | findstr /v "^start"  | findstr /v "^end"  | findstr /v "^by" > %LFILEL%
findstr /v "hashfile" %LFILE% | findstr /r /c:"[a-z]" | findstr /v "^search" | findstr /v "^start"  | findstr /v "^end"  | findstr /v "^by"  | findstr /n "SHA Cert" > %LFILELODD%
findstr /v "hashfile" %LFILE% | findstr /r /c:"[a-z]" | findstr /v "^search" | findstr /v "^start"  | findstr /v "^end"  | findstr /v "^by"  | findstr /n /v "SHA Cert" > %LFILELEVN%
goto paste

:end
rem **** **** **** **** **** **** **** **** **** **** **** 
rem **** **** **** **** **** **** **** **** **** **** **** 
echo.
echo ************ 処理終了 ************
echo.

echo 10秒後に閉じます
ping 127.0.0.1 -n 10 2> nul 1>&2
exit

:paste
setlocal
set N=0
set S=
for /F "delims=" %%A in (%LFILELODD%) do call :sub "%%A"
goto end

:sub
for /F "%S% delims=" %%B in (%LFILELEVN%) do >>%LFILEL% echo.%~1 %%B&goto next

:next
set /A N+=1
set S=skip=%N%

その他

  • 1行でファイル名とhashがほしいなと思って編集しています。
  • 最後のhash一覧以外不要とかなら途中ファイルは削除するよう改変してみてください。
  • paste処理のforの/nは偶数奇数チェック用なので不要ならオプション消してください。
  • Linuxに持って行った方が早い感はありますが、Windowsオンリーな環境もあるよね、という時の試作品でした。かなり遅いと思います。
  • 今ならwsl使えるならそれを利用するとか、PowerShellを模索するとか、他の手法も検討しましょう。

以上

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?