Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if file already exists in the folder

Tags:

vb.net

I am trying to copy some files to folder. I am using the following statement to check if the source fie exists

 If My.Computer.FileSystem.FileExists(fileToCopy) Then

But I donot know how to check if file exists in the folder before copying. Please advise.

Thanks and best regards, Furqan

like image 648
Furqan Sehgal Avatar asked Dec 02 '11 06:12

Furqan Sehgal


People also ask

How do you check if a file exists in a folder?

To check for specific files use File. Exists(path) , which will return a boolean indicating wheter the file at path exists. Noe that this answer returns false if the user does not have permission to read the file. So it does more than just checkinf if the file exists in a folder.

How do you check if a file already exists?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .


1 Answers

Dim SourcePath As String = "c:\SomeFolder\SomeFileYouWantToCopy.txt" 'This is just an example string and could be anything, it maps to fileToCopy in your code.
Dim SaveDirectory As string = "c:\DestinationFolder"

Dim Filename As String = System.IO.Path.GetFileName(SourcePath) 'get the filename of the original file without the directory on it
Dim SavePath As String = System.IO.Path.Combine(SaveDirectory, Filename) 'combines the saveDirectory and the filename to get a fully qualified path.

If System.IO.File.Exists(SavePath) Then
   'The file exists
Else
    'the file doesn't exist
End If
like image 149
Bradley Uffner Avatar answered Oct 27 '22 10:10

Bradley Uffner