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.
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>
<th>Exists In Target Folder?</th>
<th>Same Created Date?</th>
<th>Same Modified Date?</th>
Dim strSourcePath, strTargetPath
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
Set objSourceFile = objFSO.GetFile(strSourcePath & "\" & objFile.Name)
Set objTargetFile = objFSO.GetFile(strTargetPath & "\" & objFile.Name)
If objSourceFile.Size = objTargetFile.Size Then
If objSourceFile.DateCreated = objTargetFile.DateCreated Then
If objSourceFile.DateLastModified = objTargetFile.DateLastModified Then
Set objSourceFile = Nothing
Set objTargetFile = Nothing
' 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
Response.Write vbCrLf & "</tr>" & vbCrLf
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.
Exists In Target Folder?
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.