ftp download stopping before end of list - vb.net

I'm using the following code to download files from an FTP server. Before entering this code, I have created a list of filenames from files that are in the directory on the FTP server. There are over 2000 files in the list.
As I iterate through the list, the files download properly until I reach exactly 121 files downloaded. Then it starts giving me an error of "file not found, access denied." for every file after that. However the files are there. If I start the process over again it will pick up from where it left off and download another 121 files and continue until it errors again after the next 121 files.
Here is the code:
For Each file As String In dirlist
DownloadFile(local_path + "\" + filename, new_path + "/" + Trim(filename), client)
Next
Private Sub DownloadFile(ByVal localpath As String, ByVal ftpuri As String, client As String)
Dim request As New WebClient()
request.Credentials = New NetworkCredential(user_name, password)
Dim bytes() As Byte = request.DownloadData(ftpuri)
Try
Dim DownloadStream As FileStream = IO.File.Create(localpath)
DownloadStream.Write(bytes, 0, bytes.Length)
DownloadStream.Close()
Catch ex As Exception
add_to_log(log_window, ex.Message)
End Try
End Sub
I do not understand why it is stopping before completing the list.

Related

vb.net search directory for files containing *.G(num) but NOT *.GP(num)

I'm fairly familiar with bash, but I'm very, ***very**** new to vb.net. I'm searching for an easy way to find files in a folder that end with .G1, .G2, .G3, etc. but NOT .GP1, .GP2, .GP3, etc. Then for each file I need to copy it to another folder using a different file name but the same extension. I've managed to figure this out for the unique files, but there will be an undefined number of these depending on the project and I need to make sure that I get them all. Hard coding is possible, but very, very ugly. Any suggestions?
Here's the remnants of a failed attempt:
Public Sub FindGFiles()
FileList = IO.Directory.GetFiles(searchDir, ".G[1-99]" + , IO.SearchOption.AllDirectories)
For Each foundfile As String In FileList
If foundfile.Contains(".G#") Then
'copy file somehow and retain file extension
Else
MsgBox("No match")
End If
Next
End Sub
The GetFiles-method does only support * and ? wildcard characters.
So you have to get all files with a *.G*-extension first.
In the For Each-loop one can then use the Like-operator to check the desired pattern:
Public Sub CopyGFiles(searchDir As String, destDir As String)
Dim fileList As String() = IO.Directory.GetFiles(searchDir, "*.G*", IO.SearchOption.AllDirectories)
Dim fileName As String
Dim extension As String
For Each foundfile As String In fileList
fileName = IO.Path.GetFileNameWithoutExtension(foundfile)
extension = IO.Path.GetExtension(foundfile)
If extension Like ".G#" OrElse
extension Like ".G##" Then
'copy file to destination, append "_new" to the filename and retain file extension
IO.File.Copy(foundfile, IO.Path.Combine(destDir, fileName & "_new" & extension))
Else
'pattern not matched
End If
Next
End Sub
The method-call would then be as follows:
CopyGFiles("C:\Temp", "C:\Temp\Dest")
This should be done inside a Try/Catch as different exceptions can occur when working with files.
Try
CopyGFiles("C:\Temp", "C:\Temp\Dest")
Catch ex As Exception
MessageBox.Show("An error occured" + vbCrLf + ex.Message)
End Try

Uploadfile method to overwrite file on webserver

What I am trying to do is create a small "shoutbox" where everyone will have a log on their computer that'll store everything that is typed over a textbox and upload it on a file that is located on a webserver. What I want it to do now is to overwrite existing content inside the file when file already exists.
The webserver is local.
What I've tried so far:
Private Sub sendmsg_Click(sender As Object, e As EventArgs) Handles sendmsg.Click
Try
Dim path As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\chatlog.txt"
Dim writer As StreamWriter = New StreamWriter(path, True)
Dim address As String = "http://localhost/tonakis2108/shoutbox.txt"
'Grabs text from textboxes and hides logfile
writer.WriteLine(nickname.Text + ": " + msg.Text)
writer.Close()
File.SetAttributes(path, FileAttributes.Hidden) 'Hides file
'Uploads file
My.Computer.Network.UploadFile(path, address, "", "", True, 50)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The problem:
The problem I face is when I change or delete the file on the server I get an error 404 that the destination doesn't exist and when the file is already there and empty it doesn't do anything when I upload.

Check the file whether is it by another process

I have some ask to you to check and advice if my code is enough and correct. My application works on files sometimes its downloading multiple files by windows service and then by other windows service parser process those files are parsing. Unfortunately before process file has to be finished downloading before process parser should open the file and play with it therefore i prepared some code to check firstly whether the file is free - means not open by another process or some person, whatever. I use most cases streamreader to open the file and play with it but as i said i would like firstly check file if its ready.
This is my code, what do you think:
Public Shared Function FileInUse(ByVal sFile As String, ByVal log As String) As Boolean
Dim thisFileInUse As Boolean = True
Dim counter As Integer = 0
For i = 0 To 30
counter += 1
If System.IO.File.Exists(sFile) Then
Try
Using f As New IO.FileStream(sFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)
thisFileInUse = False
Exit For
End Using
Catch
System.Threading.Thread.Sleep(10000) '10 sec
End Try
End If
Next
Return thisFileInUse
End Function

File is a directory not a file

I have a folder called test which has subfolders: A, B, and C at the root. I am trying to copy a file to these three folders.
Not sure why the I am getting the error:
The target file "c:\test\A" is a directory and not a file. Please help.
Dim OPUSINI As New FileInfo("C:\Program Files (x86)\OPUS_4.5\OPUS32.INI")
'Where is will be going
'Dim Win7DestLocation As String = "C:\Users"
Dim Win7DestLocation As String = "C:\test"
Dim WinXPDestLocation As String = "C:\Documents and Settings"
'Get a list of all the Subfolders within the Destination location
Dim Win7Destdir As New DirectoryInfo(Win7DestLocation)
Dim WinXPDestdir As New DirectoryInfo(WinXPDestLocation)
'Checks if Destination Exists for Windows 7
Dim Win7CheckExistDestLocation As New IO.DirectoryInfo(Win7DestLocation)
'Checks if Destination Exists for Windows XP
Dim WinXPCheckExistDestLocation As New IO.DirectoryInfo(WinXPDestLocation)
If Win7CheckExistDestLocation.Exists Then
Try
For Each subfolder As DirectoryInfo In Win7Destdir.GetDirectories
OPUSINI.CopyTo(subfolder.FullName, True)
Next
Catch ex As Exception
MessageBox.Show("Unable to backup Pump data files." + ex.ToString, "Backup Error:", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
You are passing a directory name to CopyTo.
That method wants a filename not a directory name.
Thus the exception received.
If I understand your code well, you need to change that line to
Dim destFile = Path.Combine(subfolder.FullName, OPUSINI.Name))
OPUSINI.CopyTo(destFile, True)
Also using DirectoryInfo objects is not really necessary here.
The simple Directory class could do the same things with less overhead

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.
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.
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.
Have a look at the DirectoryInfo class.