Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VB.NET iterating through files and directories

I'm working on a VB.NET program that will automatically backup my work to my FTP server. So far I am able to upload a single file, by specifying a file name using this:

     'relevant part - above is where FTP object is instantiated
       Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()

            Try
                'Stream to which the file to be upload is written
                Dim _Stream As System.IO.Stream = _FtpWebRequest.GetRequestStream()

                'Read from the file stream 2kb at a time
                Dim contentLen As Integer = _FileStream.Read(buff, 0, buffLength)

                'Till Stream content ends
                Do While contentLen <> 0
                    ' Write Content from the file stream to the FTP Upload Stream
                    _Stream.Write(buff, 0, contentLen)
                    contentLen = _FileStream.Read(buff, 0, buffLength)
                Loop
                _Stream.Close()
                _Stream.Dispose()
                _FileStream.Close()
                _FileStream.Dispose()

                ' Close the file stream and the Request Stream


            Catch ex As Exception
                MessageBox.Show(ex.Message, "Upload Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try

Now I want to be able to iterate through a directory (Which contains subdirectories) and recursively call the function above. I'm having a problem getting a hold of the files in the directory.

My Question is, How can I loop through and send each file to the upload function above? Can I just send the filename to an array, or is there some sort of system object to handle this (e.g. System.IO.Directory)

Pseudocode for what I'm trying to do

                For Each Sub_directory In Source_directory

                     For Each File in Directory
                       'call the above code to transfer the file
                     Next

                'start next subdirectory
                Next

I'm trying to replicate the entire directory structure with sub directories intact. My first attempt dumped ALL files into one directory.

like image 955
tpow Avatar asked Feb 15 '10 14:02

tpow


2 Answers

You could go through each directory and file using recursion like this:

Public Shared Sub ForEachFileAndFolder(ByVal sourceFolder As String, _
                                       ByVal directoryCallBack As Action(Of DirectoryInfo), _
                                       ByVal fileCallBack As Action(Of FileInfo))

    If Directory.Exists(sourceFolder) Then
        Try
            For Each foldername As String In Directory.GetDirectories(sourceFolder)
                If directoryCallBack IsNot Nothing Then
                    directoryCallBack.Invoke(New DirectoryInfo(foldername))
                End If

                ForEachFileAndFolder(foldername, directoryCallBack, fileCallBack)
            Next
        Catch ex As UnauthorizedAccessException
            Trace.TraceWarning(ex.Message)
        End Try

        If fileCallBack IsNot Nothing
            For Each filename As String In Directory.GetFiles(sourceFolder)
                fileCallBack.Invoke(New FileInfo(filename))
            Next
        End If
    End If
End Sub

Edit: Usage of the Delegates:

ForEachFileAndFolder("yourPath", AddressOf dirAction, Addressof fileAction)

Public Sub dirAction(Byval dirInfo As DirectoryInfo)
    ' do something here '
End Sub

Same for the FileAction.

like image 165
Bobby Avatar answered Oct 19 '22 06:10

Bobby


You'll have to handle directories and files separatly, but it's still pretty simple.

System.IO.DirectoryInfo constains the list files and sub-directories within a specified directory. Look for the property directories and files.

What you will want to do is specify a function that will read through all files first and then directories within the DirectoryInfo. For the files, you do what you want to do with it (Upload I think), and once you hit the Directories, call your function recursivly using the sub directory as the argument. This will go thourh all sub-directories and file.

Pseudo-Code (I call it pseudo-code cos' I don't know the exact syntax)


Sub UploadDirectory(oDirInfo as DirectoryInfo)

   For eaco oFile as FileInfo in oDirINfo.Files
     'Do your thing
   Next

   For each oDir as DirectoryInfo as oDirInfo.Directories
     ' Do some stuff if you need to separate directories
     UploadDirectory(odir)
End sub

Hope that helps.

like image 38
David Brunelle Avatar answered Oct 19 '22 08:10

David Brunelle