如何从目录中删除旧文件,同时在 Windows 上保留最新文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I delete old files from a directory while keeping the most recent ones on Windows
提问by KiwiNige
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
尼日
回答by subha
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"
回答by Jason Bunting
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 Microsoftin 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
编辑:意识到您需要从 Microsoft 下载东西才能使 FORFILES 工作。我也喜欢公认的解决方案,因为您不必有任何特别之处。唯一的问题是它只每两周发生一次,而不是每天运行一个过程来删除所有超过 14 天的内容。为了什么,这是值得的。:P
回答by Mark Nold
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。
回答by PabloG
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
回答by zigdon
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 :)
实际删除的行被注释掉了,因为房子里可能有孩子:)