Download .txt files from web using vb.net - vb.net

I have managed to find a way to download .txt files from a website that i have access to which is great, but i am using a list of filenames i have added to a datagridview.
My next progression is to download any .txt files from the root and subdirectories on the website but if it exists in my download location c:\logs it will skip it.
Any direction is gratefully received. VBVirg

Dim _FilePath As String = "<file path and name here>"
If My.Computer.FileSystem.FileExist(_FilePath) = True Then
'File exists.
End If

In addition to Vincent's answer:
Dim FilePath As String = "YourFile"
If My.Computer.FileSystem.FileExists(FilePath) = True Then
'File exists, delete it
My.Computer.FileSystem.DeleteFile(FilePath)
Else
'The file doesn't exist.
End If

Related

How Do I Get The Full Path Of A Selected Directory And Then Use That Directory Within The Code

I have currently created code which grabs a ceratin file extension and puts it into a certain folder.
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Excel Files")
Dim filePaths31 = IO.Directory.GetFiles("C:\Users\bj\Desktop\", "*.xlsx")
For Each filePath31 In filePaths31
Dim filename31 = IO.Path.GetFileName(filePath31)
Dim newPath31 = IO.Path.Combine("C:\Users\bj\Desktop\Excel Files", filename31)
If IO.File.Exists(newPath31) Then
MessageBox.Show("Error: Please check if the file elready exists")
Return
End If
IO.File.Move(filePath31, newPath31)
Next filePath31
MessageBox.Show("Excel Files Compiled And Cleaned")
This code works well although the directory implemented inside the code and should be different for every user.
I have experimented with this code here which allows a user to select a directory, although now I need help assigning that full directory into IO.Path.Combine(users chosen directory here + \Excel Files, filename31
If FolderBrowserDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim grade As New IO.DirectoryInfo(FolderBrowserDialog1.SelectedPath)
This code I believe grabs the user directory and stores it in the grade variable. I need to figure out how to store that 'grade' variable into my code, and also create a new folder in that directory that is specified in the code.

How to Auto Copy text file from one folder to another folder in vb 2010 console or windows application

I want to create a program that auto copy text file from one folder to another folder . is it possible to make in windows form in vb.net ? if not what about in console apps ? i tried to search but i didn't find an answer for both. please help me i'm new to to this. I want to copy all the text file that is being save to c:folder1\test1.text copy to c:folder2\test1.text then test2.text,test3.text all the text file that are being put in folder1. i want to copy in folder2.
now i only have this code:
it will only copy 1 specific textfile with file name test.txt.
enter code here
My.Computer.FileSystem.CopyFile("C:\CopyTo\test.txt",
"C:\CopyHere\test.txt")
Of course! First of all we need a function that search for files.
Public Sub SearchFiles(ByVal Pattern As String, ByVal Path As String, ByVal FilesFound As ArrayList)
FilesFound.AddRange(Directory.GetFiles(Path, Pattern))
End Sub
But where we should save the list of files? We can use a Array for it. Also we should define our output and input folder
Dim files As New ArrayList
Dim inDir As String = "input path"
Dim outDir As String = "output path"
We can now call this function like this:
SearchFiles("*.txt", inDir, files)
All .txt files in the folder are now saved in our Array List. But how we can work with it? We can now work with it like this:
Try
For Each file As String In files
Dim fName As String = Path.GetFileName(file)
My.Computer.FileSystem.CopyFile(file , outDir & "\" & fName, overwrite:=False)
Next
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
This will copy every .txt file that where found in our inDir to our outDir. If something goes wrong then you will see this in the console. Try it out and understand how it works :)

Read File name and put it into Variable

I am trying to find a way to read a specific file name in a directory and then put the file name into a variable. In my program I have a batch file that zips a folder called logs and then changes the name of zip to username_date_time.zip.
So basically if the filename was jobs_03152015_1315.zip I would want the entire file name but not the path stored into a variable. The file starts off on the user's local machine. It is then uploaded into a network share.
The network path will be uploaded to a database for others to view. I want to just add the unique file name to the end of a pre set path. Here is the code I am using.
Dim filePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(filePath)) Then
For Each file As String In filePath
If file.Contains(".zip") Then
Dim zip As String = file
testbox.Text = zip
Exit For
End If
Next
End If
You can enumerate all files using the GetFiles() method of the static (shared in VB) Directory class, see msdn.
Assuming you have more then 1 file I add the files to a listbox control. But you could change that if you wish. I get the filename using the Path class. You will find a lot other helpful functions in this class.
Dim searchPath = "C:\temp\logs"
Dim files = Directory.GetFiles(path, "*.zip")
For Each file In files
listbox1.Items.Add(path.GetFileName(file))
Next
This should do it.
Your loop doesn't read anything. The string variable filePath is just the name of the directory, not a list of the files in that directory. Calling For Each over a string simply enumerates each character contained in the string
To get a list of files contained in that folder you need Directory.EnumerateFiles() and pass the filePath variable and the extension required.
It seems that you are interested only to find if that folder contains at least one file with zip extension. If this is the case then you could remove the explicit loop and write simply
Dim file = Directory.EnumerateFiles(filePath, "*.zip").FirstOrDefault()
If file IsNot Nothing Then
testbox.Text = Path.GetFileName(file)
End If
Using Path.GetFileName will return just the file without the path part
Try this code :
Dim FilePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(FilePath)) Then
For Each File As String In System.IO.Directory.EnumerateFiles(FilePath)
If File.Contains(".zip") Then
Dim info As New System.IO.FileInfo(File)
zip = info.Name
testbox.Text = zip
Exit For
End If
Next
End If

How to delete all text in a text file on ftp server without deleting file itself in vb.net

Hi I am doing a chat orientated program in vb.net. I have everything working nicely but there is a feature I am trying to add where if the user types /clear it will then delete the chat text from the file, (the chat is an ftp connection and it writes to a text file)
i'm hoping to only delete text from the file not the whole file itself, i have tried appending but that as you probably know already just writes extra text to the file.
If anyone can help it would be great cheers :D
The way to go should be upload a new empty file overwriting the one on the FTP, with the same filename and path.
Dim wcFTP As New WebClient()
'wcFTP.Proxy = ...
wcFTP.BaseAddress = "ftp://foobar.domain.com"
wcFTP.Credentials = New NetworkCredential("user", "pass")
Dim sFilePath As String '= ...
Dim sFtpPath As String = wcFTP.BaseAddress & sFolder & Path.GetFileName(sFilePath)
wcFTP.UploadFile(sFtpPath, sFilePath)
Where sFilePath is the path of the empty file with the same name as the one in the server.
To get an empty file you can donwload and empty the file on the ftp, or simply create a new file with the same name and no data. For example:
Dim sFilePath As String = "C:\Folder\fileName.xml"
File.Create(sFilePath).Dispose()

Visual Basic: File is said to be in the wrong folder

Ok, here is my story:
I am building a fileviewer, and i am trying to delete the selected file in the listview.
when i try to delete it, it gave me an error saying the file wasnt found. I looked at my desktop and the file was there. here is the original code:
dim f as string = lv1.focuseditem.text
my.computer.filesystem.deletfile(f)
lv1.update()
this gave me that error. My updated code is supposed to show me where the computer thinks my file is:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
this shows a messagebox that shows the path of:
C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt
but the real file location is:
C:\Users\tgs266\Desktop\test.txt
please help
How are you getting the filenames for the ListView? Is it just the filename and no path?
If, for example, lv1.FocusedItem.Text is "test.txt", and that is the value you use (without the path), by default the program will look in the directory it's executing in. This is most likely why you're seeing C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt as the location, instead of what you expected.
If all the files are on your desktop, you can use Environment.GetFolderPath in conjunction with the Environment.SpecialFolder Enumeration to get the file, like this:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
However, if you're going to have files scattered throughout your system, you'd be better off storing the full path as #Plutonix indicates in his comment.
It looks like your code is looking in your applications path on the server while you want to look at the users desktop location.