LoginSignup
32
37

More than 5 years have passed since last update.

Windowsで古いファイルを削除する

Posted at

コマンドプロンプトのforfilesコマンドで、Linux等でよく使われる find /path/to/log -mtime +7 -exec rm {} \; と同様のことができます。

forfilesコマンドはWindows Server 2003、Windows Vista以降で利用できます。

cmd
7日以上前の*.txtを削除
> forfiles /P c:\path\to\log /D -7 /M "*.txt" /c "cmd /c del @file"

30日以上前の*.logを削除
> forfiles /P c:\path\to\log /D -30 /M "*.log" /c "cmd /c del @file"

デフォルトでは指定したフォルダのみが処理対象で、/S オプションを付けるとサブフォルダを再帰処理します。

バッチファイルを作ってタスクスケジューラに登録しておくと定期的に古いファイルを消すことができます。

参考サイト

Windowsのforfilesコマンドで条件に合うファイルを取り出して処理する

32
37
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
32
37