学習帳

VBS(またはAHK)でWindowsイベントログを一括削除

最終更新:


Windowsの不要なイベントログが溜まっているとストレージ容量を圧迫してしまう。

Xの部分には年月日時分秒などが入る。

これを手動で消すのは面倒なので、下記手順を実施して自動化する。

結果、HDDの容量を数十GB節約できた。

## 手順(VBSスクリプトを作成)

Logフォルダ内の特定名称のファイルをすべて削除する。

```vb
Option explicit
Dim wshell
Set wshell = CreateObject("WScript.Shell")
Dim fso,fd
Set fso = CreateObject("Scripting.FileSystemObject")
Dim regexp
Set regexp = CreateObject("VBScript.RegExp")
On Error Resume Next

For Each fd In fso.GetFolder("C:\Windows\System32\winevt\Logs").Files
    regexp.Pattern = "^Archive-Security-....-..-..-..-..-..-...\.evtx$"
    if regexp.Test(fd.Name) Then
       fso.DeleteFile(fd)
    end if
next
msgbox "完了"

よりシンプルに以下の書き方でもOKの模様。

Option explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
fso.DeleteFile "C:\Windows\System32\winevt\Logs\Archive-Security-*.evtx", True 

2018.1.11 追記

上記のスクリプトがいつのまにか上手く動かなくなった。
システムフォルダの内部を書き換えるスクリプトなので、管理者権限に関するセキュリティ設定が何か変わったためではないかと考えている。

応急処置としてahk(Autohotkey)で同様のスクリプトを作成した。

FileDelete, C:\Windows\System32\winevt\Logs\Archive-Security-*.evtx


関連記事


スポンサーリンク