Problem copying files from one directory to another

mtait

Member
Joined
Jun 12, 2008
Messages
5
Programming Experience
1-3
Hi - I have a document management program, with a file structure stored in a database, with a parent child relationship:

id, parentid, description, mt_type, filename

Where all of the files are stored in my c:\users\mark\documents\filedb\ directory.

if mt_type = 0 then it relates to a folder - if it = 10, then it relates to a file.

I'm trying to create the physical directory structure eg:

c:\users\mark\documents\Newfiles\
c:\users\mark\documents\Newfiles\Summer07
c:\users\mark\documents\Newfiles\Summer07\sunrise
c:\users\mark\documents\Newfiles\Summer07\sunset
c:\users\mark\documents\Newfiles\Winter07
..
..

...and copy the appropriate files from c:\users\mark\documents\filedb\ into the folders as I go.

I can successfully create all of the folders using the recursive loop "getStructure" below - but it fails to copy every file across - but does not increase the error flag "createFileErr".

Do I need to wait for the file system to copy the file, before moving onto the next? If so, what is the best way of introducing a delay into the code, after file.copy.

Or is there a way of checking that the file has been successfully copied, before allowing the code to move on?

Or is there maybe another problem with copying files, that I'm not seeing?

Thank you for any help or suggestions.

Mark

VB.NET:
 Dim createFolderErr As Integer = 0
    Dim createFolderOk As Integer = 0

    Dim createFileErr As Integer = 0
    Dim createFileOk As Integer = 0


    Function getStructure(ByVal mtID As Integer, ByVal divid As Integer, ByVal counter As String)

        Dim folderid As Integer = 0
        Dim foldername As String = ""
        Dim counter2 As String = counter
        divid = divid + 1


        'Create connection to database
        Dim strConn As String = "Data Source=.\SQLExpress;Initial Catalog=filedb;Integrated Security=True"
        Dim sql As String = "SELECT id, parentid, description, mt_type from tblfiles where parentid= " & mtID & " and mt_type=0 order by description asc"
        Dim conn As New SqlConnection(strConn)
        Dim Cmd As New SqlCommand(sql, conn)
        Dim objDR As SqlDataReader

        'open connection and read folders

        conn.Open()
        objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)
        Do While objDR.Read()
            If objDR("mt_type") = 0 Then
                'for recursive loop - keep track of path back to root
                If counter2 = "0" Then
                    counter2 = objDR("description")
                Else
                    counter2 = counter & "\" & objDR("description")
                End If

                'get folder id for grabbing the files
                folderid = objDR("parentid")

                Try
                    Directory.CreateDirectory(tbFolder.Text & "\" & counter2 & createFolderOk)
                    createFolderOk = createFolderOk + 1

                    'now call sub routine to check for any files within this folder
                    createFiles(folderid, foldername)

                Catch
                    createFolderErr = createFolderErr + 1
                End Try
                
                getStructure(objDR("id"), 0, counter2)
            End If
        Loop
        counter = counter2
        objDR.Close()
        conn.Close()
       
    End Function


    Sub createFiles(ByVal folderid As Integer, ByVal foldername As String)

        Dim fname As String = ""

      
        Dim strConn As String = "Data Source=.\SQLExpress;Initial Catalog=filedb;Integrated Security=True"
        Dim sql As String = "SELECT id, parentid, description, mt_type, filename from tblfiles where parentid= " & folderid & " and mt_type=10 order by description asc"
        Dim conn As New SqlConnection(strConn)
        Dim Cmd As New SqlCommand(sql, conn)
        Dim objDR As SqlDataReader

        'open connection and read files

        conn.Open()
        objDR = Cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

        Do While objDR.Read()

            fname = objDR("filename")

            'copy file from based directory, to the new physical directory
            Try
                File.Copy("c:\users\mark\documents\filedb\" & fname, foldername & "\" & fname, True)
                createFileOk = createFileOk + 1
            Catch ex As Exception
                createFileErr = createFileErr + 1
            End Try

        Loop

        objDR.Close()
        conn.Close()

    End Sub
 
File.Copy blocks until the file has copied so you dont need a delay. What is the errpor message youre encountering during the copy?
 
Hi - I'm not getting an error message - it's as if it just passes over some files.

I'l try rewriting in one routine, rather than recursive - I'm sure either I'm confused, or the code is confused about what I'm trying to do.

Thanks, Mark
 

Latest posts

Back
Top