Ok, so I'm really new to VBScript, so go easy on me.
I'm trying to build a VBScript that will delete files from a redundant backup folder that no longer exist in the primary backup folder. I envision something that takes each file in the redundant folder and checks to see if a file with the same name exists in the primary backup folder. If it doesn't, it then deletes the file in the redundant folder. It seems easy enough, but I think my lack of VB knowledge is holding me back.
Edit: This will run on Server 2003.
Here's what I have so far:
'variables
dim fso1
dim fso2
dim redundantFolder
dim primaryFolder
dim redundantFile
dim primaryFile
dim counter
'creates file system object
Set fso1 = CreateObject("Scripting.FileSystemObject")
set fso2 = CreateObject("Scripting.FileSystemObject")
'sets the folder object to redundant backup folder
Set redundantFolder = fso1.GetFolder("C:\Users\bmcwilliams\Desktop\Dev\TestCompleted")
'sets folder object to primary folder
Set primaryFolder = fso2.getFolder("C:\Users\bmcwilliams\Desktop\Dev\TestUnCompleted")
'deletes files that aren't found in primary folder
For Each redundantFile in redundantFolder
For Each primaryFile in primaryFolder
if primaryFile.name == redundantFile.name
flag = false
Next
I'm aware that the logic in that 'For Each' block of code is incomplete and just plain wrong, but that's as far as my small brain has allowed me to go.
Thanks in advance. Any help is greatly appreciated.
Edit: Here's the final code. I hope it will be helpful for another beginner out there:
'creates file system object
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set fso2 = CreateObject("Scripting.FileSystemObject")
'sets the folder object to redundant backup folder
Set redundantFolder = fso1.GetFolder("C:\temp\redundant")
'sets folder object to primary folder
Set primaryFolder = fso2.GetFolder("C:\temp\primary")
'deletes files that aren't found in primary folder
For Each redundantFile In redundantFolder.Files
If Not fso1.FileExists(primaryFolder.Path + "\" + redundantFile.Name) Then
redundantFile.Delete
End If
Next