How do I check if file exists before downloading?

Checking to see if a file is already downloaded

Page 1 of 1

1 Replies - 7233 Views - Last Post: 12 August 2008 - 11:40 AM Rate Topic: -----

#1 EvolutionMedia  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 125
  • Joined: 11-August 08

How do I check if file exists before downloading?

Post icon  Posted 12 August 2008 - 09:54 AM

Hey!

Okay, so far so good! I'm able to create a temp and to make the directory more unique I'm able to randomize a directory in the temp directory. I ran into a couple of problems because some files already exist in the direcotry.

I have the code within the Do_Download function
 My.Computer.Network.DownloadFile(main_form.WebBrowser1.document.Domdocument.images(i).href, random_dir & "\" & splitted_file(i))
					check_file = My.Computer.FileSystem.FileExists(splitted_file(i))
					If check_file = True Then
						Kill(splitted_file(i))
					Else
						Return

					End If



I did a run on it it gave me some exception error.

Inside the Do_Download function I have:

	  Dim check_file As String



Which I may have to change to boolean, right?

Well, thanks for the help guys!

P.S. How would I be able to append a number the file? So for example: image1.jpg
this is only if the file exists of course!
then if it tries downloading it again it will put it into image1-1.jpg and so on.

Is This A Good Question/Topic? 0
  • +

Replies To: How do I check if file exists before downloading?

#2 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 160
  • Joined: 01-August 08

Re: How do I check if file exists before downloading?

Posted 12 August 2008 - 11:40 AM

I am somewhat new to VB programming, but what I do to check if a file exists is I use an IF statement to check for a condition (in this case, if the file exists)

IF File.Exists(file_path_with_file_name) Then
	Do something
End If



As for appending a number to a file, that is something I am working on myself. In my case, I know that all the files I am working with have a .pdf extension. So I use InStr(string1, string2) with string1 being the file name and string2 being what I want to search for (.pdf) in the string. InStr() returns the index of my search as an integer so I assign a variable (as an integer) to handle this.

position = InStr(string1, string2)



The variable position now holds the index. Then I perform an insert on the file (string1) to insert " - 1", or whatever number happens to be next, before position, which holds the index of the search.

string1.Insert(position -1, " - 1")



There maybe an alternative to this, but so far, this is what I have been using. Like I said, I am still somewhat new to VB.Net.
Was This Post Helpful? 1

Page 1 of 1