[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 628
  • Last Modified:

moiving duplicate files from a folder, vb.net

Hi Experts,

I need a vb.net program, when button click should move all the duplicate files from both c:\PA and c:\PE into c:\archive folder
Program should check the below things

                           1) check same filename(should omit the datetime stamp) exist or not   ex:   PE8252AD3L6, PE8323AE424
                            2) If the filename are of the same then should check the date
                     
                             3)If filename and date are equal then, need to check the time(24 hr format)

                 All the old files should move into archive and new will remains in c:\PE & c:\PA folder.
                       
       I have elucidated in detail below.........................

My first folder filename format is PE8252AD3L6_13-Mar-06_0213.txt
                                   PE8323AE424_14-May-06_1415.txt   ----------- C:\PE folder


My Second folder filename formart is PA1523-5144_07-Feb_0828.txt
                                     PA1654-2424_09-May_1325.txt

So if i put a file in c:\PE folder PE8252AD3L6_13-May-07_0513.txt, earlier date PE8252AD3L6_13-Mar-06_0213.txt
should go to the c:\arcive folder
0
nyee84
Asked:
nyee84
  • 5
  • 5
1 Solution
 
arif_eqbalCommented:
Use FileSystemWatcher in the Created event check the FileName and process accordingly
If you want I can try and post a sample
0
 
nyee84Author Commented:
How should i check date & time if i  just use file system watch??

Pls provide the full code for the above scenario since i am very new to vb.net
0
 
nyee84Author Commented:
Anxiouslu waiting for the input!!!!
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
arif_eqbalCommented:
Sorry, was a bit busy
Ok so here's a small eg from what I understood of your logic
This is what the example Code does.

It will keep checking a Folder ( C:\PE ), now this folder would always have files in the format "SomeText_DD-MMM-YY_HHmm.Ext" i.e 3 parts Text, Date and Time for eg PE8323AE424_14-May-06_1415.txt
Now as soon as any File is added into this folder our code would check if the folder already contains a file starting with the Text Part, if no we do not do anything
if yes we see which of the two files is older based on the Date and Time part of the file name, the older file gets transferred to another Folder called c:\Archive
The same repeats for the other folder c:\PA (I hope this is what you want if not we can modify the logic)
So here's the code

Drop a FileSystemWatcher object on the Form
Go to its Properties and set the "Path" Property to C:\PE
Now go to the Code, put Imports System.IO and put this Code into the FileSystemWatcher1_Created event

Public Sub FileSystemWatcher1_Created(ByVal sender As Object, ByVal e As System.Io.FileSystemEventArgs) Handles FileSystemWatcher1.Created
      Dim CurrFileName As String = e.Name   'The New File name that has just been added into the Folder
      Dim ExistingFileName As String
      Dim CurrFileTimeStamp As DateTime
      Dim ExistingFileTimeStamp As DateTime
      Dim ExistingFile() As String
      Dim CurrFile() As String = CurrFileName.Split("_")  ' Get the 3 components of the File Name

      CurrFile(2) = CurrFile(2).Remove(CurrFile(2).Length -4 , 4)  'Remove the Extension ".txt"
      CurrFileTimeStamp = CDate(CurrFile(1) & " " & CurrFile(2).Insert(2,":"))   ' get the Date time in a DateTime variable so that we can compare

      Dim Fl() As String = Directory.GetFiles("C:\PE", CurrFile(0) & "*")  'Now serach the Folder for any existing file with the same TextPart, this will always return at least one file i.e. the same file just added, if it returns 2 that means another file with same text part exists, so get that existing file's full name
     
     If Fl.Length > 1 Then
           If Fl(0).toUpper <> e.FullPath.ToUpper then
                ExistingFileName = Fl(0).SubString(Fl(0).LastIndexOf("\") + 1)
           ElseIf Fl(1).toUpper <> e.FullPath.ToUpper then
                ExistingFileName = Fl(1).SubString(Fl(1).LastIndexOf("\") + 1)
           Else
                End Sub
           End If
     Else
         End Sub
     End If

     ExistingFile=ExistingFileName.Split("_")  'Just as for Current file get the 3 parts of this existing file
     ExistingFile(2) = ExistingFile(2).Remove(ExistingFile(2).Length -4 , 4)  ' remove Ext
     ExistingFileTimeStamp = CDate(ExistingFile(1) & " " & ExistingFile(2).Insert(2,":")) ' Get the dateTime for this file as well

     Application.DoEvents()  'Just in case the file is still being copied
     Application.DoEvents()

     If CurrFileTimeStamp > ExistingFileTimeStamp Then  'Now move the older file
          File.Move("C:\PE" & ExistingFileName, "C:\Archive" & ExistingFileName)
     Else
          File.Move(e.FullPath, "C:\Archive" & e.Name)
     End If
End Sub

That's it
However, I have taken a few things for granted such as the FileName format would not vary (even the Time portion should always be 1430 and in case it is 830 it should be 0830 i.e. 4 digits)


0
 
nyee84Author Commented:
However, I have taken a few things for granted such as the FileName format would not vary (even the Time portion should always be 1430 and in case it is 830 it should be 0830 i.e. 4 digits)

-No issues with the above mentioned points.

There is minor change in our logic, the program should move the duplicate files from an existing folder, meaning no more new files will copied to the  folder PE&PA , we need to play around with the existing duplicate files in my folder c:\PE and c:\PA. I got a lot of duplicate files in this folder, so basically what i am doing is a housekeeping of my folder. I dont know whether we can user file system watcher for this scenario or not. Sorry for leading to you a wrong direction. Pls advice how do we overcome this obstacle by providing the code.

Thanks in advance
0
 
nyee84Author Commented:
Awating for your reply
0
 
arif_eqbalCommented:
No in such a case FileSystemWatcher would not help because it would start working only when some event like Create/Delete etc. happens in the Folder.
So we can modify the logic, no issues I'd just try the code here and post back
0
 
arif_eqbalCommented:
Hi nyee84
Here goes...
Though the logic has changed because now we need to Loop through the Files in the Folder, I have tried to use the Same Code, so We needed a few extra things like the MoveFileEventArg Structure
Anyway so all the code that I pasted earlier now moves to a Function, (I found out a bug as well in the code that I have corrected in here, did the earlier one in a little hurry you see :-)  )
So here's the full code

Public Structure MoveFileEventArg
    Dim Name As string
    Dim FullPath As String
End Structure


Private Sub MoveDuplicateFiles(ByVal e As MoveFileEventArg, ByVal SourceFolder As String, ByVal DestinationFolder As String)

      Dim CurrFileName As String = e.Name   'The New File name that has just been added into the Folder
      Dim ExistingFileName As String
      Dim CurrFileTimeStamp As DateTime
      Dim ExistingFileTimeStamp As DateTime
      Dim ExistingFile() As String
      Dim CurrFile() As String = CurrFileName.Split("_")  ' Get the 3 components of the File Name

      CurrFile(2) = CurrFile(2).Remove(CurrFile(2).Length -4 , 4)  'Remove the Extension ".txt"
      CurrFileTimeStamp = CDate(CurrFile(1) & " " & CurrFile(2).Insert(2,":"))   ' get the Date time in a DateTime variable so that we can compare

      Dim Fl() As String = Directory.GetFiles(SourceFolder, CurrFile(0) & "_*")  'Now serach the Folder for any existing file with the same TextPart, this will always return at least one file i.e. the same file just added, if it returns 2 that means another file with same text part exists, so get that existing file's full name
     
     If Fl.Length > 1 Then
           If Fl(0).toUpper <> e.FullPath.ToUpper then
                ExistingFileName = Fl(0).SubString(Fl(0).LastIndexOf("\") + 1)
           ElseIf Fl(1).toUpper <> e.FullPath.ToUpper then
                ExistingFileName = Fl(1).SubString(Fl(1).LastIndexOf("\") + 1)
           Else
                Exit Sub
           End If
     Else
         Exit Sub
     End If

     ExistingFile=ExistingFileName.Split("_")  'Just as for Current file get the 3 parts of this existing file
     ExistingFile(2) = ExistingFile(2).Remove(ExistingFile(2).Length -4 , 4)  ' remove Ext
     ExistingFileTimeStamp = CDate(ExistingFile(1) & " " & ExistingFile(2).Insert(2,":")) ' Get the dateTime for this file as well

     Application.DoEvents()  'Just in case the file is still being copied
     Application.DoEvents()

     If CurrFileTimeStamp > ExistingFileTimeStamp Then  'Now move the older file
          File.Move(SourceFolder & "\" & ExistingFileName, DestinationFolder & "\" & ExistingFileName)
     Else
          File.Move(e.FullPath, DestinationFolder & "\" & e.Name)
     End If

End Sub

'Now call this function from say a Button Click

Private Sub Button1_Click (byVal sender as object, byval e as System.EventArgs) Handles Button1.Click
      Dim SrcFolder As String = "C:\PE"
      Dim ArchiveFolder As String ="C:\Archive"
      Dim Arg As MoveFileEventArg
      Dim Files() As String =  Directory.GetFiles(SrcFolder)
     
      For i As Integer=0 To Files.Length - 1
         If(File.Exists(Files(i)) Then
             Arg.Name =  Files(i).SubString(Files(i).LastIndexOf("\") + 1)
             Arg.FullPath= Files(i)
             MoveDuplicateFiles(Arg, SrcFolder, ArchiveFolder)
          End If
      Next
End Sub


So that should be it....
Plz note that I have typed down the code here so there might be some Typo Errors, Plz take care
Thanks
0
 
nyee84Author Commented:
Your code seemed to work out very well and will accept your solution soon...

Keep up the good work.

Finally, Please take a look in to my future posts(file handling) if you are free..

Regards,
nyee
0
 
arif_eqbalCommented:
Sure nyee84 and thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now