VB.NET iterations through files and directories

I am working on a VB.NET program that will automatically back up my work to my FTP server. For now, I can upload one 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 call the function recursively above. I have a problem storing files in a directory.

My question is: how can I execute a loop and send each file to the upload function above? Can I just send the file name to an array or is there some kind 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 am trying to replicate the entire directory structure with unprepared subdirectories. My first attempt dumped ALL files into a single directory.

+4
source share
3 answers

You can 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: Using delegates:

 ForEachFileAndFolder("yourPath", AddressOf dirAction, Addressof fileAction) Public Sub dirAction(Byval dirInfo As DirectoryInfo) ' do something here ' End Sub 

Same thing for FileAction.

+8
source

You will have to process the directories and files separately, but it is still pretty simple.

System.IO.DirectoryInfo contains list files and subdirectories in the specified directory. Find directories and property files.

What you want to do is specify a function that will read all files first, and then directories in DirectoryInfo. For files, you do what you want to do with it (download, I think), and as soon as you get into directories, call your function recursively, using the auxiliary directory as an argument. This will cause all subdirectories and files to be saved.

Pseudocode (I call it pseudocode cos "I do not 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 this helps.

+1
source

Take a look at the DirectoryInfo class.

0
source

Source: https://habr.com/ru/post/1301342/


All Articles