|
Answer» How Can I Delete All the .BAK Files in a Folder That Are More Than 7 Days Old?There are third party DOS utilities that can do date handling, but left alone, batch code is somewhat lacking in many features...date handling among them.
This little script should help you out:
Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder("your foldergoeshere") Set colSubFolders = f.SubFolders
For Each objFolder in colSubFolders ShowFiles objFolder Next
Sub ShowFiles(Fld) Set k = fso.GetFolder(Fld) Set s = k.SubFolders Set kf = k.Files
For Each objFile In kf If objFile.DateCreated < date - 7 Then If objFile.Extension = "bak" Then Wscript.Echo objFile & " " & objFile.DateCreated End If Next End Sub
As written the script only lists files. Change the highlighted line to fso.DeleteFile(objFile). Be sure to change yourfoldergoeshere with something appropriate (ie: C:\Backup). Save the script with a vbs extension and run as cscript scriptname.vbs
I tried to modify your script as below but failed, COULD you help me to check. Thank you very much.
Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder("F:\test\backup") Set colSubFolders = f.SubFolders
For Each objFolder in colSubFolders ShowFiles objFolder Next
Sub ShowFiles(Fld) Set k = fso.GetFolder(Fld) Set s = k.SubFolders Set kf = k.Files
For Each objFile In kf If objFile.DateCreated < date - 7 Then If objFile.Extension = "bak" Then fso.DeleteFile(objFile) End If End If Next End Sub whosyourdaddy
whosyourbuddy............. I may have inadvertently introducied a property that does not exist. On the other hand it would have been helpful had you pointed out the source of the error. In any case the ORIGINAL script was overwritten and used recursion to delete all bak files in the directory and it's subdirectories.
Code: [Select]Set fso = CreateObject("Scripting.FileSystemObject") Set f = fso.GetFolder("F:\test\backup") Set colFiles = f.Files
For Each objFile in colFiles If objFile.DateCreated < date - 7 Then If Right(objFile.Name, 4) = ".bak" Then fso.DeleteFile(objFile) End If End If Next
Note : if "f:\test\backup" does not exist, the script will fail
Good luck. Does vbs is a freeware? Need to purchase license or not?No commercial license is needed. VBScript is installed on all WINDOWS MACHINES except the earlier versions of Win95. There are commercial editors available for VBScript but Notepad works just fine. The current version of the Windows Script Host (as it is officially known) is 5.6
There is a treasure of information about VBScript at the Script Center including free scripts, free downloads, online lessons and sample scripts for doing just about anything.
Good luck. I get the answer from website as below:
Set ObjFSO = CreateObject ("Scripting.FileSystemObject")
'Pull current date DateInfo = Now
'Delete files older than X number of days OlderThan = "7"
'Remove number of days from date DateInfo = DateInfo - OlderThan
WScript.Echo "Deleting files older than " & DateInfo
'Folder we're interested in strFolder = "F:\test"
'WScript.Echo DateInfo 'Testing
'Get folder for collection
NumberOfFiles = "0" DeletedFiles = "0"
Set objFolder = objFSO.GetFolder(strFolder) Set colFiles = objFolder.Files For each objFile in colFiles 'WScript.Echo objFile.DateCreated 'Testing 'WScript.Echo objFile.Name 'Testing NumberOfFiles = NumberOfFiles + 1 If objFile.DateCreated < DateInfo Then WScript.Echo "Deleting " & objFile.Name objFSO.DeleteFile(objFile.Path) DeletedFiles = DeletedFiles + 1 End If Next WScript.Echo "Complete" WScript. Echo NumberOfFiles & " file(s) in directory" WScript.Echo DeletedFiles & " file(s) deleted"I'm glad the Scripting Guys were able to help. One nice thing is if you don't find a script to do exactly want you want, you can always borrow pieces of multiple scripts to satisfy your inner scriptwriter.
Unfortunately folks have been known to take this a bit too literally and built a frankenscript which turned their PC into a BRICK.
|