Jump to content

Recommended Posts

Posted

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"

 

'-----------------------------------------------

Posted (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 by halbaradkenafin
  • Thanks 1
Posted
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.

Posted
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.

  • Thanks 1
Posted
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.

Posted
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.

  • Thanks 1

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...