0

我有一个 bat 脚本,其中获取文件夹中的所有文件,然后将此文件夹及其内容转换为一个 RAR 文件。此脚本还会在复制后添加当前日期并将其移动到备份文件夹中。我计划每天让这个 bat 文件由 Windows 调度程序任务运行。

我的问题:

有没有办法添加到这个脚本中来删除备份文件夹中所有超过 7 天的 rar 文件?

for /f "delims==" %%D in ('DIR D:\scripts /A /B /S') do (  
"C:\Program Files\WinRAR\WinRAR.EXE"  a -agyyyy-MM-dd -r "c:\backup\scripts.rar" "%%D"  
) 
4

3 回答 3

1

事情就这样发生了……

我有一个非常相似的脚本(Visual Basic Sc​​ript)可以做到这一点。但是,您需要修改目录路径、文件扩展名 (.RAR) 和日期长度(在本例中 >=3 将其设置为 7):

编辑 1: 只需将其复制并粘贴到一个新的文本文件中,并将扩展名重命名为 .vbs。

示例解决方案脚本:

On Error Resume Next   
Dim fso, folder, files, sFolder, sFolderTarget     
Set fso = CreateObject("Scripting.FileSystemObject")   

'location of the database backup files 
sFolder = "X:\Data\SQL_Backup\" 

Set folder = fso.GetFolder(sFolder)   
Set files = folder.Files     

'used for writing to textfile - generate report on database backups deleted 
Const ForAppending = 8 

'you need to create a folder named “scripts” for ease of file management &  
'a file inside it named “LOG.txt” for delete activity logging 
Set objFile = fso.OpenTextFile(sFolder & "\scripts\LOG.txt", ForAppending) 

objFile.Write "================================================================" &   VBCRLF & VBCRLF 
objFile.Write "                     DATABASE BACKUP FILE REPORT                " & VBCRLF 
objFile.Write "                     DATE:  " &    FormatDateTime(Now(),1)   & "" & VBCRLF 
objFile.Write "                     TIME:  " &    FormatDateTime(Now(),3)   & "" & VBCRLF & VBCRLF 
objFile.Write "================================================================" & VBCRLF  

'iterate thru each of the files in the database backup folder 
 For Each itemFiles In files  
'retrieve complete path of file for the DeleteFile method and to extract  
'file extension using the GetExtensionName method 
 a=sFolder & itemFiles.Name 

'retrieve file extension  
b = fso.GetExtensionName(a) 
   'check if the file extension is BAK 
   If uCase(b)="BAK" Then 

       'check if the database backups are older than 3 days 
       If DateDiff("d",itemFiles.DateCreated,Now()) >= 3 Then 

           'Delete any old BACKUP files to cleanup folder 
           fso.DeleteFile a  
           objFile.WriteLine "BACKUP FILE DELETED: " & a 
       End If 
   End If 
Next   

objFile.WriteLine "================================================================" & VBCRLF & VBCRLF 

objFile.Close 

Set objFile = Nothing 
Set fso = Nothing 
Set folder = Nothing 
Set files = Nothing

我希望这会有所帮助。

于 2010-06-08T22:43:24.063 回答
0

看看一个名为 forfiles.exe 的实用程序 - 这应该可以满足您的需要

于 2010-06-08T22:28:05.697 回答
0

试试 robocopy!您必须首先将所有文件移动到删除文件夹,然后删除文件夹,在批处理文件中是这样的:

md C:\Delete
robocopy YourFolderToDeleteFrom C:\delete /E /minage:7
rmdir C:\delete /S /Q
于 2010-06-08T22:28:21.727 回答