删除超过指定日期的文件[重复]

时间:2021-12-11 02:28:49

Possible Duplicate:
Batch file to delete files older than N days

可能的复制:批处理文件删除文件超过N天。

How do I create a batch file to delete files older than a specified date?

如何创建批处理文件来删除比指定日期更早的文件?

This does not seem to work;

这似乎行不通;

:: --------DELOLD.BAT----------
@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX

for /f "tokens=2" %%i in ('date /t') do set thedate=%%i
type %1
pause
set mm=%thedate:~0,2%
set dd=%thedate:~3,2%
set yyyy=%thedate:~6,4%

set /A dd=%dd% - %OLDERTHAN%
set /A mm=%mm% + 0

if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31

goto ERROR

:SET31
set /A dd=31 + %dd%
goto DONE

:SET30
set /A dd=30 + %dd%
goto DONE

:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto DONE

:SET29
set /A dd=29 + %dd%

:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
for %%i in (*.*) do (
set FileName=%%i
call :PROCESSFILE %%~ti
)

set mm=
set yyyy=
set dd=
set thedate=
goto EXIT

:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD X
ECHO   Where X is the number of days previous to Today.
ECHO.
ECHO EX: "DELOLD 5" Deletes files older than 5 days.
GOTO EXIT

:PROCESSFILE
set temp=%1
set fyyyy=20%temp:~6%
set fmm=%temp:~0,2%
set fdd=%temp:~3,2%
if /I %fyyyy% GTR 2069 set fyyyy=19%temp:~6%


:: ***************************************
:: * This is where the files are deleted *
:: * Change the ECHO command to DEL to   *
:: * delete. ECHO is used for test.      *
:: ***************************************
if /I %yyyy%/%mm%/%dd% GEQ %fyyyy%/%fmm%/%fdd% (
ECHO %FileName%
)

set temp=
set fyyyy=
set fmm=
set fdd=

:EXIT

:: ----------END-DELOLD.BAT-------------

8 个解决方案

#1


10  

I use this script:

我使用这个脚本:

////////////////////////////////////////////////////////
// Deletes file older than a number of days 
// in the current directory
////////////////////////////////////////////////////////
// Usage: wscript DeleteOlderThan.js [#Days]
// By default, remove files older than 30 days
////////////////////////////////////////////////////////

function removeDays(date, nDays)
{
    var dateRet = date
    return dateRet.setDate(date.getDate() - nDays);
}

function addSlash(strPath)
{
    var c = strPath.substr(-1, 1);
    if( c !== '\\' && c !== '/' )
    {
        strPath += '\\';
    }
    return strPath;
}

// Read arguments
var nDays = WScript.Arguments(0) || 30;

// Create system objects
var fs = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");

// Retrieve current directory
var strDirectoryPath = addSlash(shell.CurrentDirectory);

// Compute date
var dateNow = new Date();
var dateTest = removeDays(dateNow, nDays);

// Iterate on files
var folder = fs.GetFolder(strDirectoryPath);
var files = folder.Files;

for( var it = new Enumerator(files); !it.atEnd(); it.moveNext() )
{
    var file = it.item();

    if( file.DateLastModified < dateTest)
    {
        file.Delete(true);
    }
}

which I invoke every day with:

我每天都用:

wscript "C:\Program Files\Utils\DeletesOlderThan.js" 30

#2


8  

If you're on a Windows Server and/or have the Server Resource Kit installed, forfiles may be exactly what you're looking for.

如果您在Windows服务器上,并且/或安装了服务器资源工具包,那么forfiles可能正是您需要的。

Examples: print all file names, older than 180 days

示例:打印超过180天的所有文件名

forfiles /S /D -180 /C "cmd /C Echo @Path" >olderthan180days.txt

delete all PDF files, older than 365 days

删除所有PDF文件,超过365天

forfiles /S /M *.pdf /D -365 /C "cmd /C Del @Path"

#3


6  

Edit: I figured it out.

编辑:我想出来了。

To delete all files older than a given date:

删除所有大于给定日期的文件:

REM del_old.bat
REM usage: del_old MM-DD-YYY
for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> FILES_TO_KEEP.TXT
for /f "tokens=*" %%a IN ('xcopy *.* /L /I /EXCLUDE:FILES_TO_KEEP.TXT null') do if exist "%%~nxa" del "%%~nxa"

To delete all files newer than a given date:

删除所有更新的文件:

REM del_new.bat
REM usage: del_new MM-DD-YYYY
for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist "%%~nxa" del "%%~nxa"

#4


6  

eJames: I found your xcopy solution to work very well, indeed! The thing I like most about it is that it should work in Windows language versions other than English as well, since the format of the XCOPY parameters seems to be date-setting independent. Wonderful! ;]

我发现你的xcopy解决方案工作得很好,真的!我最喜欢它的一点是,它应该也适用于Windows语言版本,而不是英语版本,因为XCOPY参数的格式似乎与日期设置无关。太棒了!,)

One improvement would be to pre-create the FILES_TO_KEEP.TXT file so that if no file match the keep criteria, the second XCOPY statement can still go ahead and do the delete (it fails if it cannot find the FILES_TO_KEEP.TXT file). Here's my script (please note in this example I changed it to only delete *.pdf files and I also changed the temp file name to make more sure there are no potential conflicts, as well as cleaning up the temp file afterwards):

一个改进是预创建FILES_TO_KEEP。TXT文件,这样如果没有文件匹配keep条件,第二个XCOPY语句仍然可以继续执行并删除(如果找不到FILES_TO_KEEP,则会失败)。TXT文件)。这是我的脚本(请注意,在这个示例中,我将它修改为仅删除*。pdf文件和我也更改了temp文件名,以确保没有潜在的冲突,以及之后清理temp文件):

@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX

echo >> ~~~FILES_TO_KEEP.TXT~
for /f "tokens=*" %%a IN ('xcopy *.pdf /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> ~~~FILES_TO_KEEP.TXT~
for /f "tokens=*" %%a IN ('xcopy *.pdf /L /I /EXCLUDE:~~~FILES_TO_KEEP.TXT~ null') do if exist "%%~nxa" del "%%~nxa"
del ~~~FILES_TO_KEEP.TXT~

GOTO END

:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD mm-dd-yyyy
ECHO   Where mm-dd-yyyy is the date prior to which you want to delete files.
ECHO.
ECHO EX: "DELOLD 10-17-2008" Deletes files older than October 17, 2008.
ECHO.
ECHO This should work on any language version of Windows, but has only been 
ECHO tested in English-US versions.
GOTO END

:END

#5


1  

My solution works if you

如果你愿意的话,我的解决办法可以。

  • either have a touch utility on your system (which I do)
  • 或者在你的系统上有一个触摸实用程序(我有)
  • or can afford to temporarily change system date using date command (which is probably not the case, but who knows)
  • 或者可以使用date命令临时更改系统日期(可能不是这样,但是谁知道呢)

Here's the idea:

想法是这样的:

  1. In the target directory, create a signal file (with a unique name), and with timestamp equal to your deletion threshold time. It can be done with touch (which accepts desired timestamp as a parameter), or by remembering current date with date /T, changing system date to desired with date <param>, and restoring the original date.

    在目标目录中,创建一个信号文件(具有唯一的名称),并使用与删除阈值时间相等的时间戳。它可以通过touch(它接受所需的时间戳作为参数)完成,或者通过日期/T记住当前日期,使用日期将系统日期更改为所需日期,并恢复原始日期。

  2. Use for /f %%I in ('dir /od') to iterate over all files ordered by date, deleting them one by one. After you encounter (and delete) the signal file, stop deleting.

    在('dir /od')中使用/f %I来遍历按日期排序的所有文件,逐个删除。遇到(并删除)信号文件后,停止删除。

#6


1  

Perhaps you would like this: http://blog.jumlin.com/2010/10/move-files-in-folders-and-subfolders-older-than-one-year/

也许你会喜欢这个:http://blog.jumlin.com/2010/10/move-files in- folder-subfolders-older -than one year/

Thats a script that will move files older than one year, it even supports various date formats and subfolders. You could make a few changes to make it delete instead of move them. Its a very complete script with comments and explanations.

这是一个可以移动超过一年的文件的脚本,它甚至支持各种日期格式和子文件夹。你可以做一些改变使它删除而不是移动它们。这是一个非常完整的脚本,有注释和解释。

#7


0  

I wonder why you have to use the ole DOS shell... Python or Windows Powershell would be the right choices in 2008 :)

我想知道你为什么要用ole DOS外壳……Python或Windows Powershell将是2008年的正确选择:)

#8


-1  

I don't know if you can do that with .BAT files and what few tools Windows comes with, but you sure can with .js (JScript) or .vbs (VBScript) files. You are doing this under Windows, right? If so, then Windows can process .js and .vbs files just as well as .bat files by default. They are far more powerful.

我不知道你是否可以用。bat文件和Windows附带的工具,但是你肯定可以用。js (JScript)或。vbs (VBScript)文件。你是在Windows下做的,对吧?如果是,那么Windows在默认情况下可以处理.js和.vbs文件以及.bat文件。他们要强大得多。

And, if you happen to be targeting only Windows Server 2008, there's PowerShell which is even better (downloadable for other Windows versions).

而且,如果您碰巧只针对Windows Server 2008,还有PowerShell,它甚至更好(可为其他Windows版本下载)。

#1


10  

I use this script:

我使用这个脚本:

////////////////////////////////////////////////////////
// Deletes file older than a number of days 
// in the current directory
////////////////////////////////////////////////////////
// Usage: wscript DeleteOlderThan.js [#Days]
// By default, remove files older than 30 days
////////////////////////////////////////////////////////

function removeDays(date, nDays)
{
    var dateRet = date
    return dateRet.setDate(date.getDate() - nDays);
}

function addSlash(strPath)
{
    var c = strPath.substr(-1, 1);
    if( c !== '\\' && c !== '/' )
    {
        strPath += '\\';
    }
    return strPath;
}

// Read arguments
var nDays = WScript.Arguments(0) || 30;

// Create system objects
var fs = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");

// Retrieve current directory
var strDirectoryPath = addSlash(shell.CurrentDirectory);

// Compute date
var dateNow = new Date();
var dateTest = removeDays(dateNow, nDays);

// Iterate on files
var folder = fs.GetFolder(strDirectoryPath);
var files = folder.Files;

for( var it = new Enumerator(files); !it.atEnd(); it.moveNext() )
{
    var file = it.item();

    if( file.DateLastModified < dateTest)
    {
        file.Delete(true);
    }
}

which I invoke every day with:

我每天都用:

wscript "C:\Program Files\Utils\DeletesOlderThan.js" 30

#2


8  

If you're on a Windows Server and/or have the Server Resource Kit installed, forfiles may be exactly what you're looking for.

如果您在Windows服务器上,并且/或安装了服务器资源工具包,那么forfiles可能正是您需要的。

Examples: print all file names, older than 180 days

示例:打印超过180天的所有文件名

forfiles /S /D -180 /C "cmd /C Echo @Path" >olderthan180days.txt

delete all PDF files, older than 365 days

删除所有PDF文件,超过365天

forfiles /S /M *.pdf /D -365 /C "cmd /C Del @Path"

#3


6  

Edit: I figured it out.

编辑:我想出来了。

To delete all files older than a given date:

删除所有大于给定日期的文件:

REM del_old.bat
REM usage: del_old MM-DD-YYY
for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> FILES_TO_KEEP.TXT
for /f "tokens=*" %%a IN ('xcopy *.* /L /I /EXCLUDE:FILES_TO_KEEP.TXT null') do if exist "%%~nxa" del "%%~nxa"

To delete all files newer than a given date:

删除所有更新的文件:

REM del_new.bat
REM usage: del_new MM-DD-YYYY
for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist "%%~nxa" del "%%~nxa"

#4


6  

eJames: I found your xcopy solution to work very well, indeed! The thing I like most about it is that it should work in Windows language versions other than English as well, since the format of the XCOPY parameters seems to be date-setting independent. Wonderful! ;]

我发现你的xcopy解决方案工作得很好,真的!我最喜欢它的一点是,它应该也适用于Windows语言版本,而不是英语版本,因为XCOPY参数的格式似乎与日期设置无关。太棒了!,)

One improvement would be to pre-create the FILES_TO_KEEP.TXT file so that if no file match the keep criteria, the second XCOPY statement can still go ahead and do the delete (it fails if it cannot find the FILES_TO_KEEP.TXT file). Here's my script (please note in this example I changed it to only delete *.pdf files and I also changed the temp file name to make more sure there are no potential conflicts, as well as cleaning up the temp file afterwards):

一个改进是预创建FILES_TO_KEEP。TXT文件,这样如果没有文件匹配keep条件,第二个XCOPY语句仍然可以继续执行并删除(如果找不到FILES_TO_KEEP,则会失败)。TXT文件)。这是我的脚本(请注意,在这个示例中,我将它修改为仅删除*。pdf文件和我也更改了temp文件名,以确保没有潜在的冲突,以及之后清理temp文件):

@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX

echo >> ~~~FILES_TO_KEEP.TXT~
for /f "tokens=*" %%a IN ('xcopy *.pdf /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> ~~~FILES_TO_KEEP.TXT~
for /f "tokens=*" %%a IN ('xcopy *.pdf /L /I /EXCLUDE:~~~FILES_TO_KEEP.TXT~ null') do if exist "%%~nxa" del "%%~nxa"
del ~~~FILES_TO_KEEP.TXT~

GOTO END

:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD mm-dd-yyyy
ECHO   Where mm-dd-yyyy is the date prior to which you want to delete files.
ECHO.
ECHO EX: "DELOLD 10-17-2008" Deletes files older than October 17, 2008.
ECHO.
ECHO This should work on any language version of Windows, but has only been 
ECHO tested in English-US versions.
GOTO END

:END

#5


1  

My solution works if you

如果你愿意的话,我的解决办法可以。

  • either have a touch utility on your system (which I do)
  • 或者在你的系统上有一个触摸实用程序(我有)
  • or can afford to temporarily change system date using date command (which is probably not the case, but who knows)
  • 或者可以使用date命令临时更改系统日期(可能不是这样,但是谁知道呢)

Here's the idea:

想法是这样的:

  1. In the target directory, create a signal file (with a unique name), and with timestamp equal to your deletion threshold time. It can be done with touch (which accepts desired timestamp as a parameter), or by remembering current date with date /T, changing system date to desired with date <param>, and restoring the original date.

    在目标目录中,创建一个信号文件(具有唯一的名称),并使用与删除阈值时间相等的时间戳。它可以通过touch(它接受所需的时间戳作为参数)完成,或者通过日期/T记住当前日期,使用日期将系统日期更改为所需日期,并恢复原始日期。

  2. Use for /f %%I in ('dir /od') to iterate over all files ordered by date, deleting them one by one. After you encounter (and delete) the signal file, stop deleting.

    在('dir /od')中使用/f %I来遍历按日期排序的所有文件,逐个删除。遇到(并删除)信号文件后,停止删除。

#6


1  

Perhaps you would like this: http://blog.jumlin.com/2010/10/move-files-in-folders-and-subfolders-older-than-one-year/

也许你会喜欢这个:http://blog.jumlin.com/2010/10/move-files in- folder-subfolders-older -than one year/

Thats a script that will move files older than one year, it even supports various date formats and subfolders. You could make a few changes to make it delete instead of move them. Its a very complete script with comments and explanations.

这是一个可以移动超过一年的文件的脚本,它甚至支持各种日期格式和子文件夹。你可以做一些改变使它删除而不是移动它们。这是一个非常完整的脚本,有注释和解释。

#7


0  

I wonder why you have to use the ole DOS shell... Python or Windows Powershell would be the right choices in 2008 :)

我想知道你为什么要用ole DOS外壳……Python或Windows Powershell将是2008年的正确选择:)

#8


-1  

I don't know if you can do that with .BAT files and what few tools Windows comes with, but you sure can with .js (JScript) or .vbs (VBScript) files. You are doing this under Windows, right? If so, then Windows can process .js and .vbs files just as well as .bat files by default. They are far more powerful.

我不知道你是否可以用。bat文件和Windows附带的工具,但是你肯定可以用。js (JScript)或。vbs (VBScript)文件。你是在Windows下做的,对吧?如果是,那么Windows在默认情况下可以处理.js和.vbs文件以及.bat文件。他们要强大得多。

And, if you happen to be targeting only Windows Server 2008, there's PowerShell which is even better (downloadable for other Windows versions).

而且,如果您碰巧只针对Windows Server 2008,还有PowerShell,它甚至更好(可为其他Windows版本下载)。