views:

525

answers:

4

hi guys 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 am planning to have this bat file run by a windows scheduler task every day. My question is there a way to add into this cript 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"
)

A: 

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

phatmanace
can you tell me how can i use forfiles to delete files older than 7 days?
Jessie
A: 

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
Oskar Kjellin
i tried using robocopy but i can't open the link you sent.
Jessie
@Jessie What do you mean? Works fine for me
Oskar Kjellin
@Jessie try my edit
Oskar Kjellin
+1  A: 

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.

Darknight
thanks, how can i make this code to run in windows scheduler? also the name of my files will be c:\backup\scripts2010-06-07.rar, and so on, will this script work for this?
Jessie
I'll give more details in the morning, I need to sleep now!! :)
Darknight
(1) To run in windows scheduler, simply create a .BAT file with the path of this .vbs script inside it, thus when the .BAT file runs it calls this .vbs script.(2) The Name of the files does not matter, you simply point it towards the directory those files exists, and it uses the files creation date to go by. So yes the script will work for those file names.
Darknight
you mean have this whole script inside a file called script.vbs, then create another file called delete.bat which inside it has: c:\scripts\script.vbs, then from windows scheduler run c:\scripts\delete.bat??
Jessie
yes that's correct. Or you can execute that command inside an existing .bat script e.g after you have created the .rars,
Darknight
i tried doing it that one, but it doesn't do anything, i tried doing research and found that putting csscript C:\script\script.vbs inside the bat file might work but still does'nt delete anything.
Jessie
It might be due to permissions, what OS are you using? Vista or higher will certainly have permission's issue. In windows scheduler, there is an option to run with 'Highest Privileges' in order to have 'read/write/delete' access. This script works perfectly fine for me (runs off as a schedule task)
Darknight
are you sure you have inside your .bat file just as c:\script\script.vbs ?or do you need another command to make it run?
Jessie
yes, I'm 100% sure. Here is a test I just did myslelf, try it now:(1) make a .vbs file and put inside it this line:msgbox("Hello World")save it in this location : c:\test.vbsnow go into a dos command prompt and type :c:\test.vbsyou should see a message box saying "Hello World"this is no different from typing it in cmd or having it in a .BAT. I'm sure because I've been using this backup script for more than 6 months without issue.
Darknight
thank you so much it worked :)
Jessie
A: 

hi thanks for your answers, one more question, would this script 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" )

in a bat file, also close winrar, after this scripts runs, in my windows task manager i still have around 10 winrar.exe, does it mean is not closing correctly?

Jessie