如何从目录中删除旧文件,同时在Windows上保留最新文件[重复]

时间:2021-11-23 07:32:43

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

可能重复:批处理文件,用于删除超过N天的文件

I want to run a scheduled windows task that deletes all files from a directory that are older than 2 weeks.

我想运行一个计划的Windows任务,删除目录中超过2周的所有文件。

The reason is that these are IIS and Tomcat logs that fill up my server, but I want to keep the most recent logs in case I need to investigate a problem.

原因是这些是填充我的服务器的IIS和Tomcat日志,但我想保留最新的日志以防我需要调查问题。

Does any one know an easy way to do this?

有没有人知道这么做的简单方法?

Cheers

Nige

6 个解决方案

#1


6  

exact syntax: FORFILES /p d:\new /d -30 /m * /c "cmd /c del @file"

确切的语法:FORFILES / p d:\ new / d -30 / m * / c“cmd / c del @file”

#2


4  

The simplest way would be a .bat run file run weekly or monthly.

最简单的方法是每周或每月运行一次.bat运行文件。

cd \mylog\dir
mkdir archive
del /Q .\archive\*.log
move *.log .\archive

If you want something more complex look into downloading the cygwin tools to use un*x like commands, or possibly look into Powershell.

如果你想要更复杂的东西,可以下载cygwin工具来使用un * x之类的命令,或者看看Powershell。

#3


4  

With VBScript, adapted from ScriptingAnswers

使用VBScript,改编自ScriptingAnswers

Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")

startFolder = "E:\temp"           ' folder to start deleting (subfolders will also be cleaned)

OlderThanDate = DateAdd("d", -07, Date)  ' 07 days (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
    Dim folder, file, fileCollection, folderCollection, subFolder

    Set folder = fso.GetFolder(folderName)

    Set fileCollection = folder.Files

    For Each file In fileCollection
        If file.DateLastModified < BeforeDate Then
            ' fso.DeleteFile(file.Path)    # Modify this to delete after testing
            WScript.StdOut.WriteLine (file.Path)
        End If
    Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
        DeleteOldFiles subFolder.Path, BeforeDate
    Next

End Function

You can run this script with CScript

您可以使用CScript运行此脚本

@Jason: nice utility, FORFILES from the Resource Kit

@Jason:很好的实用程序,来自Resource Kit的FORFILES

#4


4  

Schedule a batch file to handle this.

安排批处理文件来处理这个问题。

This line will delete all files (*.*) in c:\mydirectory that are older than 14 days:

此行将删除c:\ mydirectory中超过14天的所有文件(*。*):

FORFILES -pc:\mydirectory -s -m*.* -d-14 -c"DEL @FILE"

Put that in a text file, rename it to something like "deletefiles.bat" and schedule it.

将它放在一个文本文件中,将其重命名为“deletefiles.bat”并安排它。

I haven't tested this, but should be easy enough to try.

我没有测试过这个,但应该很容易尝试。


EDIT: If you use this, make sure you understand what is happening - the -s flag tells it to recurse the subdirectories, and that may not be what you want to happen. Also, you may need to specify some flags for the DEL command too. :)

编辑:如果你使用它,请确保你了解发生了什么 - -s标志告诉它递归子目录,这可能不是你想要发生的。此外,您可能还需要为DEL命令指定一些标志。 :)


EDIT: Realized that you need to download stuff from Microsoft in order for FORFILES to work. I like the accepted solution too, since you don't have to have anything special. The only problem is that it only happens every two weeks instead of running a process daily to remove all things older than 14 days. For what that's worth. :P

编辑:意识到你需要从微软下载东西才能使FORFILES正常工作。我也喜欢被接受的解决方案,因为你不必有任何特别的东西。唯一的问题是它只会每两周发生一次,而不是每天运行一个进程来删除超过14天的所有内容。这是值得的。 :P

#5


1  

Why dont you write a batch file or a powershell script and schedule it?

为什么不编写批处理文件或PowerShell脚本并安排它?

Script for deleting files older than a specified date.

用于删除早于指定日期的文件的脚本。

#6


1  

It's fairly trivial to do if you have a perl (or similar) installed on the server:

如果您在服务器上安装了perl(或类似的),那么这很简单:

#!perl
foreach my $file (</path/to/logs/*.log>) {
  next unless -M $file > 14;
  print "Deleting $file...\n";
  # unlink $file or die "Failed to remove $file: $!";
}

The line that actually does the delete is commented out, since there might be kids in the house :)

实际执行删除的行被注释掉了,因为房子里可能有孩子:)

#1


6  

exact syntax: FORFILES /p d:\new /d -30 /m * /c "cmd /c del @file"

确切的语法:FORFILES / p d:\ new / d -30 / m * / c“cmd / c del @file”

#2


4  

The simplest way would be a .bat run file run weekly or monthly.

最简单的方法是每周或每月运行一次.bat运行文件。

cd \mylog\dir
mkdir archive
del /Q .\archive\*.log
move *.log .\archive

If you want something more complex look into downloading the cygwin tools to use un*x like commands, or possibly look into Powershell.

如果你想要更复杂的东西,可以下载cygwin工具来使用un * x之类的命令,或者看看Powershell。

#3


4  

With VBScript, adapted from ScriptingAnswers

使用VBScript,改编自ScriptingAnswers

Dim fso, startFolder, OlderThanDate
Set fso = CreateObject("Scripting.FileSystemObject")

startFolder = "E:\temp"           ' folder to start deleting (subfolders will also be cleaned)

OlderThanDate = DateAdd("d", -07, Date)  ' 07 days (adjust as necessary)

DeleteOldFiles startFolder, OlderThanDate

Function DeleteOldFiles(folderName, BeforeDate)
    Dim folder, file, fileCollection, folderCollection, subFolder

    Set folder = fso.GetFolder(folderName)

    Set fileCollection = folder.Files

    For Each file In fileCollection
        If file.DateLastModified < BeforeDate Then
            ' fso.DeleteFile(file.Path)    # Modify this to delete after testing
            WScript.StdOut.WriteLine (file.Path)
        End If
    Next

    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
        DeleteOldFiles subFolder.Path, BeforeDate
    Next

End Function

You can run this script with CScript

您可以使用CScript运行此脚本

@Jason: nice utility, FORFILES from the Resource Kit

@Jason:很好的实用程序,来自Resource Kit的FORFILES

#4


4  

Schedule a batch file to handle this.

安排批处理文件来处理这个问题。

This line will delete all files (*.*) in c:\mydirectory that are older than 14 days:

此行将删除c:\ mydirectory中超过14天的所有文件(*。*):

FORFILES -pc:\mydirectory -s -m*.* -d-14 -c"DEL @FILE"

Put that in a text file, rename it to something like "deletefiles.bat" and schedule it.

将它放在一个文本文件中,将其重命名为“deletefiles.bat”并安排它。

I haven't tested this, but should be easy enough to try.

我没有测试过这个,但应该很容易尝试。


EDIT: If you use this, make sure you understand what is happening - the -s flag tells it to recurse the subdirectories, and that may not be what you want to happen. Also, you may need to specify some flags for the DEL command too. :)

编辑:如果你使用它,请确保你了解发生了什么 - -s标志告诉它递归子目录,这可能不是你想要发生的。此外,您可能还需要为DEL命令指定一些标志。 :)


EDIT: Realized that you need to download stuff from Microsoft in order for FORFILES to work. I like the accepted solution too, since you don't have to have anything special. The only problem is that it only happens every two weeks instead of running a process daily to remove all things older than 14 days. For what that's worth. :P

编辑:意识到你需要从微软下载东西才能使FORFILES正常工作。我也喜欢被接受的解决方案,因为你不必有任何特别的东西。唯一的问题是它只会每两周发生一次,而不是每天运行一个进程来删除超过14天的所有内容。这是值得的。 :P

#5


1  

Why dont you write a batch file or a powershell script and schedule it?

为什么不编写批处理文件或PowerShell脚本并安排它?

Script for deleting files older than a specified date.

用于删除早于指定日期的文件的脚本。

#6


1  

It's fairly trivial to do if you have a perl (or similar) installed on the server:

如果您在服务器上安装了perl(或类似的),那么这很简单:

#!perl
foreach my $file (</path/to/logs/*.log>) {
  next unless -M $file > 14;
  print "Deleting $file...\n";
  # unlink $file or die "Failed to remove $file: $!";
}

The line that actually does the delete is commented out, since there might be kids in the house :)

实际执行删除的行被注释掉了,因为房子里可能有孩子:)