winng Posted February 3, 2015 Posted February 3, 2015 On my SIMS servers I am using the following script to delete older backup files however, I now wish to use it on another server to delete the older backup files I have been Robocopying across to SUNSQLBackups. At the moment, it doesn'y appear to do anything and I think this may be due to the backup files existing within subfolders. Anyone help me tweak this script to do what I need? '-------------- ClearOldFiles.vbs -------------------- Option Explicit Dim fso Set fso = CreateObject("Scripting.FileSystemObject") DeleteFilesa fso.GetFolder("D:\SUNSQLBackups") Sub DeleteFilesa(srcFolder) Dim srcFile If srcFolder.Files.Count = 0 Then 'Wscript.Echo "No File to Delete" Exit Sub End If For Each srcFile in srcFolder.Files If DateDiff("d", Now, srcFile.DateLastModified) < -5 Then fso.DeleteFile srcFile, True End If Next 'Wscript.Echo "Files Deleted successful" End Sub 'Wscript.Echo "Files Deleted successful" '-----------------------------------------------
halbaradkenafin Posted February 3, 2015 Posted February 3, 2015 (edited) Sounds like a job for Powershell rather than VBScript as I'm not sure how easily VBScripts handles recursively checking folders. #Path to backup folder $Path = "D:\SUNSQLBackups" #Get every item in the folder and all sub folders foreach ($File in (Get-ChildItem -Path $Path -Recurse)) { #Check if age is older than 5 days if ($File.LastWriteTime -le (Get-Date).AddDays(-5)) { #Remove file Remove-Item $File -WhatIf } } I'd suggest running it once first with the -WhatIf set on Remove-Item to ensure that it's picking up the correct files you want to remove (replace it with -Confirm:$False so you don't have to hit enter each time it finds a file), you can adjust the (Get-Date).AddDays(-5) to scale the amount of days you want to limit it to. Edited February 3, 2015 by halbaradkenafin 1
mac_shinobi Posted February 3, 2015 Posted February 3, 2015 ActivExperts linky ref vbs and folders / sub folders etc : Scripting Files and Folders using VBScript 1
winng Posted February 3, 2015 Author Posted February 3, 2015 Sounds like a job for Powershell rather than VBScript as I'm not sure how easily VBScripts handles recursively checking folders. #Path to backup folder $Path = "D:\SUNSQLBackups" #Get every item in the folder and all sub folders foreach ($File in (Get-ChildItem -Path $Path -Recurse)) { #Check if age is older than 5 days if ($File.LastWriteTime -le (Get-Date).AddDays(-5)) { #Remove file Remove-Item $File -WhatIf } } I'd suggest running it once first with the -WhatIf set on Remove-Item to ensure that it's picking up the correct files you want to remove (replace it with -Confirm:$False so you don't have to hit enter each time it finds a file), you can adjust the (Get-Date).AddDays(-5) to scale the amount of days you want to limit it to. This is the start of the output I get using the above. Any ideas? PS C:\> C:\deloldfiles.ps1 Remove-Item : Cannot find path 'C:\master_backup_2015_01_27_220305_0055439.bak' because it does not exist. At C:\deloldfiles.ps1:9 char:14 + Remove-Item <<<< $File -WhatIf + CategoryInfo : ObjectNotFound: (C:\master_backu...305_0055439.bak:String) [Remove-Item], ItemNotFou ndException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand Remove-Item : Cannot find path 'C:\master_backup_2015_01_28_220253_7928144.bak' because it does not exist.
halbaradkenafin Posted February 3, 2015 Posted February 3, 2015 This is the start of the output I get using the above. Any ideas? PS C:\> C:\deloldfiles.ps1 Remove-Item : Cannot find path 'C:\master_backup_2015_01_27_220305_0055439.bak' because it does not exist. At C:\deloldfiles.ps1:9 char:14 + Remove-Item <<<< $File -WhatIf + CategoryInfo : ObjectNotFound: (C:\master_backu...305_0055439.bak:String) [Remove-Item], ItemNotFou ndException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.RemoveItemCommand Remove-Item : Cannot find path 'C:\master_backup_2015_01_28_220253_7928144.bak' because it does not exist. That was me being an idiot with the script. The line should be: Remove-Item -Path ($Path + $File) -WhatIf And change $Path to include a \ at the end. 1
winng Posted February 3, 2015 Author Posted February 3, 2015 That was me being an idiot with the script. The line should be: Remove-Item -Path ($Path + $File) -WhatIf And change $Path to include a \ at the end. But it still doesn't go deep enough.... Remove-Item : Cannot find path 'D:\SUNSQLBackups\master_backup_2015_01_27_220305_0055439.bak' because it does not e xist.
halbaradkenafin Posted February 3, 2015 Posted February 3, 2015 But it still doesn't go deep enough.... Remove-Item : Cannot find path 'D:\SUNSQLBackups\master_backup_2015_01_27_220305_0055439.bak' because it does not e xist. Remove-Item -Path $File.FullName -WhatIf Recursive searches always get me like that, need to make a big note of it and stick it to my desk to remind me. 1
winng Posted February 3, 2015 Author Posted February 3, 2015 Great, it's working now. Thanks for you help @halbaradkenafin
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now