VB.NET iteration files and directories

I am working on a VB.NET program that will automatically back up my work to my FTP server. So far, I can upload by specifying the file name using the following method Single file:

'relevant part-above is where FTP object is instantiated
Dim _FileStream As System.IO.FileStream = _FileInfo.OpenRead()< br />
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 traverse the directory (Including subdirectories) and call the above function recursively. I am having trouble getting the files in the directory.

My question is, how do I loop and send each file to the upload function above ? Can I send the file name to an array, or is there some kind of system object to handle this (e.g. System.IO.Directory)

The pseudo code 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 copy the entire directory structure, and the subdirectories are intact. My first attempt to dump all files into one directory.

You can use recursion to traverse each directory and file as follows:

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 I sNot 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: Representative usage:

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

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

The same goes for FileAction.

I am working on a VB.NET program that will automatically back up my work to On my FTP server. So far, I can upload a single file by specifying the file name using the following method:

'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()

'C lose the file stream and the Request Stream


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

Now I want to be able to traverse the directory (including subdirectories) and call the above function recursively. I am having trouble getting the files in the directory.

I The question is, how do I loop and send each file to the upload function above? Can I send the file name to an array, or is there some kind of system object to handle this (e.g. System.IO.Directory)

The pseudo code 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 copy the entire directory structure, and the subdirectories are intact. My first attempt to dump all files into one directory.

You can use recursion to traverse each directory and file, as shown below:

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
directoryCallB ack.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: Representative usage:

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

Public Sub dirAction(Byval dirInfo As DirectoryInfo)
'do something here'
End Sub< /pre>

The same goes for FileAction.

Leave a Comment

Your email address will not be published.