Results 1 to 6 of 6

Thread: Strange Recursive Directory Copy Issue

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Strange Recursive Directory Copy Issue

    I am using the following Recursive Directory Copy code, and am having a strange issue.

    I am using this to copy and directory and all subdirectories and files to removable media (flash drive). Everything copies perfectly fine except for 1 file. this occurs on the same file every time and sometimes the file copies successfully, but other times it does not. The problem file is a 7-zip archive with a .7z file extension. When it does not successfully copy, the file is only 8 bytes on the flash drive. This is very strange. Any ideas? Thanks for any help troubleshooting this issue.

    Code:
    ' Usage: 
        ' Copy Recursive with Overwrite if exists. 
        ' RecursiveDirectoryCopy("C:\Data", "D:\Data", True, True) 
        ' Copy Recursive without Overwriting. 
        ' RecursiveDirectoryCopy("C:\Data", "D:\Data", True, False) 
        ' Copy this directory Only. Overwrite if exists. 
        ' RecursiveDirectoryCopy("C:\Data", "D:\Data", False, True) 
        ' Copy this directory only without overwriting. 
        ' RecursiveDirectoryCopy("C:\Data", "D:\Data", False, False) 
    
        ' Recursively copy all files and subdirectories from the specified source to the specified 
        ' destination. 
        Public 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
            ' 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)
                    ' Create the directory if it does not exist. 
                    If dDirInfo.Exists = False Then dDirInfo.Create()
                    ' Since we are in recursive mode, copy the children also 
                    RecursiveDirectoryCopy(sDirInfo.FullName, dDirInfo.FullName, fRecursive, overWrite)
                    sDirInfo = Nothing
                    dDirInfo = Nothing
                Next
            End If
            ' Get the 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 File does not exist. Copy. 
                If dFileInfo.Exists = False Then
                    sFileInfo.CopyTo(dFileInfo.FullName, overWrite)
                Else
                    'If file exists and is the same length (size). Skip. 
                    'If file exists and is of different Length (size) and overwrite = True. Copy 
                    If sFileInfo.Length <> dFileInfo.Length AndAlso overWrite Then
                        sFileInfo.CopyTo(dFileInfo.FullName, overWrite)
                        'If file exists and is of different Length (size) and overwrite = False. Skip 
                    ElseIf sFileInfo.Length <> dFileInfo.Length AndAlso Not overWrite Then
                        Debug.WriteLine(sFileInfo.FullName & " Not copied.")
                    End If
                End If
                sFileInfo = Nothing
                dFileInfo = Nothing
            Next
        End Sub

  2. #2
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Strange Recursive Directory Copy Issue

    Well as a start I would use this to copy a directory:

    Code:
    Private Sub CopyDirectory()
            Microsoft.VisualBasic.Interaction.Shell("xCopy " & """C:\Data"" ""D:\Data""" & " /E /Y /C")
    End Sub
    But, my first question is can you copy the file manually to the drive?

  3. #3
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Strange Recursive Directory Copy Issue

    why not write a new, short, program that just copies the problem file over and over again.

    Code:
    Public Class Form1
        Dim rndm As New Random
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Timer1.Stop()
            Timer1.Interval = rndm.Next(10, 501) 'set a new timer interval
            '
            'place file copy / debugging aids here
            'IO.File.Copy(sourceFileName:="", destFileName:="", overwrite:=False)
            '
            Timer1.Start()
        End Sub
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Timer1.Start()
        End Sub
    End Class
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Strange Recursive Directory Copy Issue

    Ok, this is very strange. I tried using the Xcopy suggestion rather than the RecursiveDirectoryCopy but am still getting the same results - a .7z file that is only 8 bytes when copied to the flash drive. I can probably remedy this by simply copying the single file after the RecursiveDirectoryCopy, but I would like to find out why this file is being such a problem. Thanks.

  5. #5
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: Strange Recursive Directory Copy Issue

    Is it just that one .7z file? Or is it any file with an extension .7z?

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Jul 2001
    Location
    Maumelle, AR
    Posts
    624

    Re: Strange Recursive Directory Copy Issue

    I tested again last night and it appears to be any .7z file. Very strange.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width