Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

Compare last 10 digits of a file

I would like to write a program that compares the last 10 digits of the files contained in a folder.
If there are duplicate files i would like to copy the duplicate in another folder.
Thanks you in advance.
[220 byte] By [mkebe] at [2007-11-11 8:14:32]
# 1 Re: Compare last 10 digits of a file
OK. What, specifically, do you need help with? What have you done so far?
Phil Weber at 2007-11-11 17:25:49 >
# 2 Re: Compare last 10 digits of a file
I am trying to catch the files that have the same last 10 bits inside a folder. The files in that folder do not have the same length but many of them do have the same last 10 bits.
Now i want to loop through the folder and see the files (.tif) that have the same last 10 bits.
My program consists of a simple GUI that has 2 buttons: 1 for selecting the file and 1 for clearing duplicates.
I have completed the part that allows me to choose the file and have the
file name displayed in a text box. Now i need to compare the file name with
the other files in the folder and see if the last 10 bits are the same.
If that's the case, i will move the duplicate in another folder.

I hope this will make sense.

Thank you
mkebe at 2007-11-11 17:26:50 >
# 3 Re: Compare last 10 digits of a file
OK, first use the Dir function to loop through the .tif files in a directory (d:\path in this example):

FileName = Dir("d:\path\*.tif")
Do While Len(FileName)
' Compare filenames here
' Get next filename
FileName = Dir()
Loop

Next, you want to strip off the file extension, so you have just the file name:

' Remove file extension
FileName = Left(FileName, Len(FileName) - 4)

Finally, compare the last 10 characters of the file name with the name the user selected and move the file if the names match:

If Right(FileName, 10) = Right(UserSelection, 10) Then
Name FileName As "d:\duplicates\" & FileName
End If

Note that the above code performs a case-sensitive comparison of the file names (e.g., filename <> FileName). If you want to perform a case-insensitive comparison, you can use the LCase function to make both file names lower-case before comparing them.

Also, VB's Name statement will move a file from one folder to another on the same drive. If you want to move the file to another drive, you can use VB's FileCopy to copy the file, and Kill to delete it. Hope that helps!
Phil Weber at 2007-11-11 17:27:47 >
# 4 Re: Compare last 10 digits of a file
Thank you i will give that a shot and let you how that turned out.

Again thank you.
mkebe at 2007-11-11 17:28:53 >
# 5 Re: Compare last 10 digits of a file
The issue that i have is how to give the first file and the second file some index so i can loop through the directory and make my test.
The filename were declared as being strings not arrays.

A quick response will be appreciated.

Thanks.
M. Kebe.
mkebe at 2007-11-11 17:29:54 >
# 6 Re: Compare last 10 digits of a file
Please post the code you have so far.
Phil Weber at 2007-11-11 17:30:53 >
# 7 Re: Compare last 10 digits of a file
Here is the code.

Option Explicit

Public FileName As String
Public fileName1 As String
Public fileName2 As String
Public I As Integer
Public j As Integer
Public directory As String
Public Modified_Date1 As Date
Public Modified_Date2 As Date
Public NumFiles As Integer ' Number of files
Public Sub AddFile_Click()
Dim File As stcFileStruct
'// fill values (not required)
File.strDialogtitle = "Select Directory to Open"
'File.strFilter = "TIF Files *.tif|*.tif" '// use same format as CommonDialog Control
'// pass stcFileStruct
ShowOpenDialog File

list_File.AddItem File.strFileName
MsgBox File.strFileName
End Sub

Public Sub cmdEliminate_Dups_Click()
FileName = Dir("U:\test\*.tif")
Do While Len(FileName)
If fileName1 <> "" And fileName2 <> "" Then
if fileName2



'For i = Len(File.strFileName) To 1
' If Mid(File.strFileName, i, 1) = "\" Then
' directory = Dir$(Left(File.strFileName, Len(File.strFileName) - (Len(File.strFileName) - i)), vbDirectory)
' End If
' Next



' For i = 1 To Len(NumFiles)
' fileName1 = list_File(i)
' For j = 1 To Len(NumFiles)
' fileName2 = list_File(j)
' If i <> j Then
' If Right(fileName1, 10) = Right(fileName2, 10) Then
' If Modified_Date1 Then
' FileCopy
'Else
' fileName1.Copy (fileName2)
' End If
'End If
' End If
'Next

'Loop






End Sub

' Function that checks if the file exists
Function DoesFileExist(FilePath As String, Optional FileAttr As VbFileAttribute) As Boolean
If Len(Dir$(FilePath, FileAttr)) > 0 Then
DoesFileExist = True
Else
DoesFileExist = False

End Function

' Function that checks if the directory exists
Function DirExists(ByVal DName As String) As Boolean

Dim sDummy As String

On Error Resume Next

If Right(DName, 1) <> "\" Then DName = DName & "\"
sDummy = Dir$(DName & "*.*", vbDirectory)
DirExists = Not (sDummy = "")

End Function
mkebe at 2007-11-11 17:31:57 >
# 8 Re: Compare last 10 digits of a file
Ah, OK, you didn't mention that you're comparing the files in a directory against a list of files. To access the files in a listbox, use the .List property:

For I = 0 To list_File.ListCount - 1
fileName = list_File.List(I)
' etc.
Next
Phil Weber at 2007-11-11 17:32:59 >
# 9 Re: Compare last 10 digits of a file
I am going to rephrase the issue that i have.

I have a drive called U:
In the U: drive i have a test folder that has may .tif files. Inside the
test folder i also have a PreRev folder.
The test folder has many .tif files that do have the same last 10 bits.

What i am trying to do is to loop through the test folder and see the files
that have the same last bits.
If i find two or more files that have the same last 10 bits, i would like to
send a copy of that same .tif file in the PreRev Folder.

This PreRev folder is going to be the clean folder that wont have any file
that share the same last 10 bits with any other file in this folder.

So fare i have created a simple user interface that has 2 buttons and a
text box. The one button is called "Choose File" and allows me to pick
any file in the test folder. The other button is called "Eliminate Dups"
and will be used to eliminate the duplicates.
I can so fare click on the "Choose File" button and open the file i want.
Also when i select the file its name shows up on the text box fine.

What i want now is a code that allows me to click on "Eliminate Dups"
and test if that selected file has any other file within the test folder
that has the same last 10 bits. If this is true i want to move a copy of that file
in the PreRev folder; if not display a message saying no duplicates.

Thank for.
mkebe at 2007-11-11 17:33:57 >