1. ホーム
  2. スクリプト・コラム
  3. DOS/BAT

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