Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If A Portion Of A Filename Exists

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.

like image 942
Muhnamana Avatar asked Feb 04 '13 19:02

Muhnamana


People also ask

How to check if a file exists python?

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.

How do I check if a file exists in C++?

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.

How do you check if any file exists in a directory in VB net?

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.


1 Answers

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
like image 74
gunakkoc Avatar answered Oct 11 '22 17:10

gunakkoc