N日前または指定日時(前後)に作成(または変更)されたファイルをバッチ処理で削除する。
2022-02-10 13:33:22
コアコードです。
@echo off
:: by oicu#lsxk.org
:: 15:17 2011-1-13
:: If you just delete files modified on a specified date or N days ago, use forfiles
FORFILES /P "C:\test directory" /S /M *.log /D -3 /C "cmd /c if @isdir==FALSE echo del @file"
:: The file modified before the specified date (inclusive): /D -yyyy/mm/dd
:: The following is a running list
:: Restrictions on use: Set date format to Chinese (China) in control panel
:: Only dir /tc can see the file creation time, the default dir are dir /tw
:: In order to take out the creation time, you have to use for+find twice, it must be very slow.
cd /d your directory
:: If you want to include subdirectories, use for /r . %%a in (*)
:: Show file modification time
for %%a in (*) do echo "%%~ta"
:: In Chinese format, this is equivalent to
for %%a in (*) do for /f "tokens=1,2* delims= " %%b in (
'dir /tw "%%a" ^| find /i "%%~nxa"'
) do echo "%%b %%c"
:: Delete files that have been modified after a certain time (this is easy)
for %%a in (*) do if "%%~ta" gtr "2008-04-01" echo del "%%a"
:: This is what displays the file creation time
for %%a in (*) do for /f "tokens=1,2* delims= " %%b in (
'dir /tc "%%a" ^| find /i "%%~nxa"'
) do echo "%%b %%c"
:: Delete files created after a certain time, if you want to delete those created before a certain time, change both
:: add the time of judgement in addition to the date, you can remove the else paragraph.
for %%a in (*) do for /f "tokens=1,2* delims= " %%b in (
'dir /tc "%%a" ^| find /i "%%~nxa"') do (
if "%%b" gtr "2010-12-15" (
echo del "%%a"
) else (
if "%%b" equ "2010-12-15" if "%%c" gtr "14:50" echo del "%%a"
)
)
:: Calculating time with batch is limited by the system's date format, which affects taking the value of a field with set.
:: Calculating time with batch is too complicated, either delete the file with vbs script only or use batch
:: Delete with vbs script.
:: Take the date 10 days ago and put it back into the variable OldDate
echo wscript.echo dateadd("d",-10,date)>GetOldDate.vbs
for /f %%a in ('cscript /nologo GetOldDate.vbs') do set OldDate=%%a
echo %OldDate%
del GetOldDate.vbs
:: There is no Format function in VBScript, Year, Month, Day and other functions take the value and then also have to deal with small
:: The only way to organize the date format is to use the following method
echo wscript.echo dateadd("d",-10,date)>GetOldDate.vbs
for /f "tokens=1,2,3* delims=-/. " %%i in ('cscript /nologo GetOldDate.vbs') do (
set y=%%i
set m=%%j
set d=%%k
)
if %m% LSS 10 set m=0%m%
if %d% LSS 10 set d=0%d%
set OldDate=%y%-%m%-%d%
echo %OldDate%
del GetOldDate.vbs
:: Combining the above, we finally get the result we want
:: Delete the old files created 10 days ago (without subdirectories)
echo wscript.echo dateadd("d",-10,date)>GetOldDate.vbs
for /f "tokens=1,2,3* delims=-/. " %%i in ('cscript /nologo GetOldDate.vbs') do (
set y=%%i
set m=%%j
set d=%%k
)
if %m% LSS 10 set m=0%m%
if %d% LSS 10 set d=0%d%
set OldDate=%y%-%m%-%d%
del GetOldDate.vbs
for %%a in (*) do for /f "tokens=1,2* delims= " %%b in (
'dir /tc "%%a" ^| find /i "%%~nxa"') do (
if "%%b" lss "%OldDate%" echo del "%%a"
)
:: Delete old files modified 10 days ago (without subdirectories)
echo wscript.echo dateadd("d",-10,date)>GetOldDate.vbs
for /f "tokens=1,2,3* delims=-/. " %%i in ('cscript /nologo GetOldDate.vbs') do (
set y=%%i
set m=%%j
set d=%%k
)
if %m% LSS 10 set m=0%m%
if %d% LSS 10 set d=0%d%
set OldDate=%y%-%m%-%d%
del GetOldDate.vbs
for %%a in (*) do if "%%~ta" lss "%OldDate%" echo del "%%a"
:: All of this article uses echo del instead of del, which just shows the file to be deleted and does not actually delete it.
pause
goto :eof
関連
最新
-
nginxです。[emerg] 0.0.0.0:80 への bind() に失敗しました (98: アドレスは既に使用中です)
-
htmlページでギリシャ文字を使うには
-
ピュアhtml+cssでの要素読み込み効果
-
純粋なhtml + cssで五輪を実現するサンプルコード
-
ナビゲーションバー・ドロップダウンメニューのHTML+CSSサンプルコード
-
タイピング効果を実現するピュアhtml+css
-
htmlの選択ボックスのプレースホルダー作成に関する質問
-
html css3 伸縮しない 画像表示効果
-
トップナビゲーションバーメニュー作成用HTML+CSS
-
html+css 実装 サイバーパンク風ボタン
おすすめ
-
cmd 環境変数コマンド set 永続的な環境変数の設定 コマンド setx
-
自動コード投入とプロジェクトデプロイメントのためのバッチファイルbatスクリプト
-
バッチ版 chm ファイルデコンパイラ v1.3
-
DOS DEBUG ユーティリティ アプレット集
-
IPアドレス自動設定一括コード共有
-
Windows Server 2008 R2のリモートポート3389を変更するためのバッチコード
-
dosコマンドラインでIEプロキシを設定するコード
-
Batはforfilesを使って期限切れのファイルを自動的に一括削除する
-
FTPファイル一括アップロード
-
bat, vbs, js ネイティブミックス (bat は vbs, js コードを実行可能)