bat脚本删除超过7天的文件

发布于 2024-09-04 09:29:51 字数 379 浏览 2 评论 0原文

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

我的问题:

有没有办法添加到此脚本中以同时删除备份文件夹中超过 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"  
) 

I have a bat script in which gets all files in a folder and then converts this folder with its contents into a one RAR file. This script also adds the current date once it makes a copy and moves it this file into a backup folder. I'm planning to have this bat file run by a windows scheduler task every day.

My question:

Is there a way to add into this script to also delete all rar files older than 7 days in the backup folder?

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"  
) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

故事↓在人 2024-09-11 09:29:51

事情就是这样发生的...

我有一个非常相似的脚本(Visual Basic 脚本)可以执行此操作。但是,您需要修改目录路径、文件扩展名 (.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

我希望有所帮助。

It just so happens...

I have a very similar script (Visual Basic Script) that does this. However you will need to modify the directory path, file extension (.RAR) and date length (in this example its >=3 set this to 7):

EDIT 1:
Simply copy & paste this into a new textfile and rename the extension as .vbs.

Sample Solution Script:

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

I hope that helps.

只为守护你 2024-09-11 09:29:51

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

have a look at a utility called forfiles.exe - this should do what you need

半暖夏伤 2024-09-11 09:29:51

尝试机器人复制!您必须首先将所有文件移动到删除文件夹,然后删除该文件夹,在批处理文件中如下所示:

md C:\Delete
robocopy YourFolderToDeleteFrom C:\delete /E /minage:7
rmdir C:\delete /S /Q

Try robocopy! You have to first move all files to a delete-folder and then delte the folder, something like this in a batch file:

md C:\Delete
robocopy YourFolderToDeleteFrom C:\delete /E /minage:7
rmdir C:\delete /S /Q
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文