Computing.Net > Forums > Programming > VBScript to delete duplicate files

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

VBScript to delete duplicate files

Reply to Message Icon

Name: Shootace
Date: February 18, 2009 at 06:12:59 Pacific
OS: Windows XP
Subcategory: General
Comment:

Hi,

I was hoping someone could help me with my request? I'm looking for a VBScript that would perform the following:

Scan files (with exact full file name) in "FirstFolder" and if duplicate is found in archive folder "SecondFolder" then a
window pops up with a message dictating so and with a "created on" field of when (date and time) the file was originally created in archive (SecondFolder) folder. Does anyone have any scripts already created for something like this? Any help would be greatly appreciated.

Thank you!



Sponsored Link
Ads by Google

Response Number 1
Name: Shootace
Date: February 18, 2009 at 08:00:52 Pacific
Reply:

This is what I have, it's close, but doesn't quite do what I need above:


Dim fso, rootfolder, subfolder, subfolders
Dim objRegEx

' Create FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")

'Get the Starting Folder
Set rootfolder= fso.GetFolder("c:\temp") 'Change the file location

'Get All the SubFolders in Root
Set subfolders = rootfolder.SubFolders


Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "^\D+\d+$" 'I'm not sure the correct syntax for all characters in filename
objRegEx.IgnoreCase = True
objRegEx.Global = False

'Loop through all the folders
For Each subfolder in subfolders
if objRegEx.Test(subfolder.Name) then
WScript.Echo "Code would delete " & subfolder.Name
'subfolder.Delete
end if
Next

'Clean up
Set objRegEx = Nothing
Set fso = Nothing


0

Response Number 2
Name: Razor2.3
Date: February 18, 2009 at 09:07:09 Pacific
Reply:

Well, it's a nice first attempt.

const fromDir = "c:\FirstFolder"
const toDir = "c:\SecondFolder"

WScript.Quit Main()

Sub FindFiles(ByRef list, fso, path, basePathLen)
  If basePathLen = 0 Then
    basePathLen = Len(path) + 1
    If Right(path, 1) <> "\" Then _
      basePathLen = basePathLen + 1
  End If

  For Each f In fso.GetFolder(path).Files
    list.Add Mid(f.Path, basePathLen), f.DateCreated
  Next 'f

  For Each d In fso.GetFolder(path).SubFolders
    FindFiles list, fso, d.Path, basePathLen
  Next 'd
End Sub

Function Main
  Main = 1
  Set fso = CreateObject("Scripting.FileSystemObject")
  Set fromList = CreateObject("Scripting.Dictionary")
  Set toList = CreateObject("Scripting.Dictionary")

  FindFiles fromList, fso, fromDir, 0
  FindFiles toList, fso, toDir, 0
  
  For Each f In fromList
    If toList.Exists(f) Then _
      WScript.Echo "Code would delete " & f & " (Created: " & toList(f) & ")"
  Next 'f
  
  Main = 0
End Function


0

Response Number 3
Name: Shootace
Date: February 20, 2009 at 14:15:52 Pacific
Reply:

This works perfectly Razor! One last thing, what if the files in "firstfolder" do not match any files in "secondfolder" and because they don't match I want to move the files from "firstfolder" to let's say "thirdfolder"? I was trying to do an "else" or "elseif" after "If toList.Exists(f) Then _" but I'm thinking that didn't work because I'm inside a Function?


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: VBScript to delete duplicate files

Need batch file to delete old files www.computing.net/answers/programming/need-batch-file-to-delete-old-files/14092.html

batch file to delete folders/files www.computing.net/answers/programming/batch-file-to-delete-foldersfiles/16164.html

How To delete a file uninstalled www.computing.net/answers/programming/how-to-delete-a-file-uninstalled/18405.html