ASP 101 - Active Server Pages 101 - Web01
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
Getting Scripts to Run on a Schedule
The Top 10 ASP Links @
What is and Why Do I Need It?

Connection Pooling lightens the load
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

ASP Duplicate File Finder

by John Peterson

The Scenario

Sometimes I come up with a cool topic to write about and then have to figure out how to write the script to go along with it. Other times I have a cool script then have to find a way to tie it into an interesting topic. This time I messed things up royally and had to write a script to save my butt!

Let me give you a little background on this one. I was recently migrating an old section of the site into a different section. In the process, once I moved the files I was also updating them to use the latest version of our template. Don't ask me why, but I wasn't doing them in any particular order and then in the middle of the process I got interrupted and lost track of where I was. Worse yet, I couldn't just empty the destination and start over because it had originally contained some other things that didn't exist in the source folder. Well instead of opening every file and looking at them to see which ones I had already updated, I whipped up this little script to help get me back on track and thought some of you might find it useful.

The Script

I've neatened up the output some and put it in a table, but aside from that it's basically unchanged from the version I was using. Which means there are some problems or at least things you should know:

  • It only lists files found in the source directory. Files that exist only in the target directory are not listed.
  • It never checks to see if the files are really the same or not. It only looks at the file attributes... not the content of the file!
  • It could easily be modified to perform actions based on what it finds, but it currently does not. If you attempt this you should be sure to have a backup of the files to be manipulated and test carefully. It's quite easy for one typo in your script to wipe out or overwrite a whole folder... I've done it before!
  • I was only using date last modified. I've added the created date, but it can be weird... it's value is often after the last modified date... especially if the file was copied.
<%@ Language = "VBScript" %>
<% Option Explicit %>
  <title>Duplicate File Finder</title>
<table border="1">
  <th>Exists In Target Folder?</th>
  <th>Same Size?</th>
  <th>Same Created Date?</th>
  <th>Same Modified Date?</th>
Dim objFSO
Dim strSourcePath, strTargetPath
Dim objSourceFolder
Dim objFile
Dim objSourceFile, objTargetFile
strSourcePath = Server.MapPath("folder1")
strTargetPath = Server.MapPath("folder2")
Set objFSO = Server.CreateObject("Scripting.FileSystemObject")
Set objSourceFolder = objFSO.GetFolder(strSourcePath)
For Each objFile In objSourceFolder.Files
  Response.Write "<tr>" & vbCrLf
  Response.Write "<td>" & objFile.Name & "</td>"
  If objFSO.FileExists(strTargetPath & "\" & objFile.Name) Then
    Response.Write "<td>Yes</td>"
    Set objSourceFile = objFSO.GetFile(strSourcePath & "\" & objFile.Name)
    Set objTargetFile = objFSO.GetFile(strTargetPath & "\" & objFile.Name)
    If objSourceFile.Size = objTargetFile.Size Then
      Response.Write "<td>Yes</td>"
      Response.Write "<td>No</td>"
    End If
    If objSourceFile.DateCreated = objTargetFile.DateCreated Then
      Response.Write "<td>Yes</td>"
      Response.Write "<td>No</td>"
    End If
    If objSourceFile.DateLastModified = objTargetFile.DateLastModified Then
      Response.Write "<td>Yes</td>"
      Response.Write "<td>No</td>"
    End If
    Set objSourceFile = Nothing
    Set objTargetFile = Nothing
    Response.Write "<td>No</td>"
    ' You can add the blank <td>s if desired
    Response.Write "<td> </td><td> </td><td> </td>"
    ' For a while I was moving files that didn't exist to the target.
    ' Left as comment in case anyone cares:
    'objFSO.MoveFile strSourcePath & "\" & objFile.Name,
    '  strTargetPath & "\" & objFile.Name
  End If
  Response.Write vbCrLf & "</tr>" & vbCrLf
Next 'objFile
Set objSourceFolder = Nothing
Set objFSO = Nothing

Here's an example of what the script's output looks like. You could obviously make it pretty if you want, but I'll leave that to you.

Filename Exists In Target Folder? Same Size? Same Created Date? Same Modified Date?

Download the Code

For those of you who want to download the code instead of cutting and pasting it from above, I've put together a zip file (4 KB) containing the script and two sample folders for you to play with.

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers