So I know in the following code example, it checks to see if a file exists (full filename)...
If My.Computer.FileSystem.FileExists("C:\Temp\Test.cfg") Then
MsgBox("File found.")
Else
MsgBox("File not found.")
End If
...But what about if part of the a file exists? There is no standard naming convention to the files but they will always have a .cfg extention.
So I want to check if C:\Temp contains a *.cfg file and if it exists, do something, else do something else.
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 . If the file is in the same folder as the program, the path_to_file is just simply the file name.
The best way to check if a file exists using standard C/C++ The only way to check if a file exist is to try to open the file for reading or writing.
The Exists method returns True only if the specified file exists; otherwise, it returns False . Note that Exists returns False if path describes a directory instead of a folder.
The *
char can be used to define simple patterns of filtering. For example, if you use *abc*
it will look for the files thats name contains "abc" in them.
Dim paths() As String = IO.Directory.GetFiles("C:\Temp\", "*.cfg")
If paths.Length > 0 Then 'if at least one file is found do something
'do something
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