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
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.
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 .
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With