开发者

bat script to delete file older than 7 days

开发者 https://www.devze.com 2023-01-02 23:43 出处:网络
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 thi

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 F开发者_JAVA百科iles\WinRAR\WinRAR.EXE"  a -agyyyy-MM-dd -r "c:\backup\scripts.rar" "%%D"  
) 


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.


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


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
0

精彩评论

暂无评论...
验证码 换一张
取 消