6 Replies - 6507 Views - Last Post: 22 October 2010 - 07:42 PM Rate Topic: -----

#1 user28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-October 10

Recursive File Copy based on file extension.

Posted 20 October 2010 - 02:57 PM

Recursive File Copy based on file extension.

Hello all,
I have a single root folder that contains numerous folders that contain a few files that I want to extract.
For this example, lets state that I want to copy all ‘txt’ extensions and put them into a repository folder.

Example:
\root
\root\Folder1\
\root\Folder1\abc.txt
\root\Folder1\subFolder\123.txt
\root\Folder2\xyz.txt
\root\Folder2\subFolder\456.txt

After the program is executed:
\repository\abc.txt
\repository\xyz.txt
\repository\123.txt
\repository\456.txt

What am I doing wrong?
Thank you in advance,
Drew


 Private Sub RecursiveDirectoryCopy(ByVal sourceDir As String, ByVal destDir As String, ByVal fRecursive As Boolean, ByVal overWrite As Boolean)
        Dim sDir As String
        Dim dDirInfo As IO.DirectoryInfo
        Dim sDirInfo As IO.DirectoryInfo
        Dim sFile As String
        Dim sFileInfo As IO.FileInfo
        Dim dFileInfo As IO.FileInfo

        ' Add trailing separators to the supplied paths if they don't exist.
        If Not sourceDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
            sourceDir &= System.IO.Path.DirectorySeparatorChar
        End If

        If Not destDir.EndsWith(System.IO.Path.DirectorySeparatorChar.ToString()) Then
            destDir &= System.IO.Path.DirectorySeparatorChar
        End If

        'If destination directory does not exist, create it.
        dDirInfo = New System.IO.DirectoryInfo(destDir)
        If dDirInfo.Exists = False Then dDirInfo.Create()
        dDirInfo = Nothing


        Try
            ' Recursive switch to continue drilling down into directory structure.
            If fRecursive Then
                ' Get a list of directories from the current parent.
                For Each sDir In System.IO.Directory.GetDirectories(sourceDir)
                    sDirInfo = New System.IO.DirectoryInfo(sDir)
                    dDirInfo = New System.IO.DirectoryInfo(destDir & sDirInfo.Name)

                    ' Get a list of files from the current parent.
                    For Each sFile In System.IO.Directory.GetFiles(sourceDir)
                        sFileInfo = New System.IO.FileInfo(sFile)
                        dFileInfo = New System.IO.FileInfo(Replace(sFile, sourceDir, destDir))

                        'If the file meets the defined extension, then move it to the repository
                        If String.Compare(sFileInfo.Extension, "txt") Then
                            RecursiveDirectoryCopy(sFileInfo.FullName, Dest_Dir.Text.ToString(), fRecursive, overWrite)
                        End If

                        sDirInfo = Nothing
                        dDirInfo = Nothing
                    Next
                Next
            End If

        Catch ex As Exception
            MessageBox.Show("Message: " & ex.Message)
        End Try
    End Sub



Is This A Good Question/Topic? 0
  • +

Replies To: Recursive File Copy based on file extension.

#2 demausdauth   User is offline

  • D.I.C Addict
  • member icon

Reputation: 190
  • View blog
  • Posts: 692
  • Joined: 03-February 10

Re: Recursive File Copy based on file extension.

Posted 20 October 2010 - 04:48 PM

What do you mean? Although I haven't checked the code yet, you don't state what the issue is? Is it not copying the files?
Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Recursive File Copy based on file extension.

Posted 20 October 2010 - 05:22 PM

Or a simplified version:

Private Sub CopyFiles(ByVal dir As DirectoryInfo, ByVal ext As String, ByVal dest As String)
    For Each fi As FileInfo In dir.GetFiles(ext)
      File.Copy(fi.FullName, dest)
    Next
    For Each di As DirectoryInfo In dir.GetDirectories
      CopyFiles(di, ext, dest)
    Next
End Sub

This post has been edited by hawkvalley1: 20 October 2010 - 05:28 PM

Was This Post Helpful? 0
  • +
  • -

#4 user28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-October 10

Re: Recursive File Copy based on file extension.

Posted 21 October 2010 - 05:25 PM

Hawk,
Thanks for the simplified version; however, I had to add the file name to the end of the copy to get it to work.

The only thing that your section of code does not account for is dupe file names. I have added a case statement to handle the duplicates and simply append a counter to the file name; however, it is not copying the dupe file with the newly added “_COPY<counter” text.
I still get the error that abcFile already exists…

Can you please look at my second case statement?

Thank you kindly,
Andrew


 Private Sub CopyFiles(ByVal dir As DirectoryInfo, ByVal ext As String, ByVal dest As String)

        Dim counter As Integer
        counter = 0
        Try
            For Each fi As FileInfo In dir.GetFiles(ext)
                Dim destFI As New FileInfo(dest)

                Select Case True
                    'Case for File names that are not dupes and do not exist in the destination folder.
                    Case destFI.Exists = False
                        File.Copy(fi.FullName, dest & fi.Name.ToString(), False)

                        'Case for dupe's
                    Case destFI.Exists = True
                        counter += 1
                        File.Copy(fi.FullName, dest & fi.Name.ToString() & "_COPY" & counter.ToString(), False)
                End Select
            Next

            For Each di As DirectoryInfo In dir.GetDirectories
                CopyFiles(di, ext, dest)
            Next

        Catch ex As Exception
            MessageBox.Show("Message1: " & ex.Message)
        End Try

    End Sub


Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Recursive File Copy based on file extension.

Posted 21 October 2010 - 06:28 PM

With a Select Case <boolean> you check for this, otherwise Case Else would handle the opposite value.
Was This Post Helpful? 0
  • +
  • -

#6 user28   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 20-October 10

Re: Recursive File Copy based on file extension.

Posted 22 October 2010 - 09:32 AM

Yes, a case else would also work; however, the syntax you provided throws an exception when there is a dupe entry.

Do you know how to modify your syntax to allow for dupes?
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Recursive File Copy based on file extension.

Posted 22 October 2010 - 07:42 PM

Well wouldn't FileInfo(dest) be different than File.Copy(fi.FullName, dest & fi.Name.ToString(), False)?

This post has been edited by hawkvalley1: 22 October 2010 - 07:53 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1