0

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
tshepang
  • 12,111
  • 21
  • 91
  • 136
bmac423
  • 15
  • 1
  • 4
  • 1
    There is the windows builtin robocopy utility with /nocopy /purge http://ss64.com/nt/robocopy.html – Alex K. Nov 14 '12 at 14:14
  • This will be running on Server 2003. Is robocopy compatible or available for download? – bmac423 Nov 14 '12 at 14:17
  • Yep; http://www.microsoft.com/en-us/download/details.aspx?id=17657 – Alex K. Nov 14 '12 at 14:20
  • In VBScript comparisons are done with `=`, not `==`. – Zev Spitz Nov 14 '12 at 14:27
  • Also, a `For Each` loop iterates over some collection. The `Folder` object returned from `GetFolder` is not a collection; you need to iterate over the collection returned by the `Files` property of the `Folder`. – Zev Spitz Nov 14 '12 at 14:28

1 Answers1

1
'creates file system object
Set fso = CreateObject("Scripting.FileSystemObject")

'sets the folder object to redundant backup folder
Set redundantFolder = fso.GetFolder("C:\temp\redundant")

'sets folder object to primary folder
Set primaryFolder = fso.GetFolder("C:\temp\primary")

'deletes files that aren't found in primary folder
For Each redundantFile In redundantFolder.Files
    If Not fso.FileExists(fso.BuildPath(primaryFolder.Path,redundantFile.Name)) Then
       redundantFile.Delete
    End If
Next
paul
  • 21,653
  • 1
  • 53
  • 54
  • The condition should be `If Not fso.FileExists...` – Zev Spitz Nov 14 '12 at 14:25
  • Also, you can use the `fso.BuildPath` method to combine the directory and the filename. – Zev Spitz Nov 14 '12 at 14:26
  • Thanks for the answer, Paul. I just tried it out and I got an "Object doesn't support this property or method" error on redundantFolder.Files... – bmac423 Nov 14 '12 at 14:29
  • Does `Files` need parentheses? It's a property, not a method. – Zev Spitz Nov 14 '12 at 14:29
  • Thanks for your help Paul and Zev. I think it's working now, and my small brain just got the tiniest bit bigger. – bmac423 Nov 14 '12 at 14:36
  • @bmac423 - Too bad more people don't have brains the size of your 'small' one :). Although, if I can take the liberty, this question is more about the FileSystemObejct, the File object, and the Folder object, than specifically about VBScript. You could write code that uses the same COM objects in [Javascript](http://stackoverflow.com/questions/8513213/use-scripting-filesystemobject-to-create-file-in-path-that-doesnt-already-exist), Python, C#, or even C++ (if you're feeling bold and adventurous). – Zev Spitz Nov 14 '12 at 18:14