5

したがって、次のコード例では、ファイルが存在するかどうかを確認します(完全なファイル名)...

If My.Computer.FileSystem.FileExists("C:\Temp\Test.cfg") Then
   MsgBox("File found.")
Else
   MsgBox("File not found.")
End If

...しかし、ファイルの一部が存在する場合はどうでしょうか?ファイルには標準の命名規則はありませんが、ファイルには常に.cfg拡張子が付いています。

そこで、C:\Tempに*.cfgファイルが含まれているかどうかを確認し、存在する場合は何かを実行し、そうでない場合は別のことを実行します。

4

3 に答える 3

14

charは、*フィルタリングの単純なパターンを定義するために使用できます。たとえば、を使用*abc*すると、名前に「abc」が含まれているファイルが検索されます。

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
于 2013-02-04T19:08:17.637 に答える
1

FileSystem.Dirをワイルドカードとともに使用して、ファイルが一致するかどうかを確認できます。

MSDNから

Dim MyFile, MyPath, MyName As String 
' Returns "WIN.INI" if it exists.
MyFile = Dir("C:\WINDOWS\WIN.INI")   

' Returns filename with specified extension. If more than one *.INI 
' file exists, the first file found is returned.
MyFile = Dir("C:\WINDOWS\*.INI")

' Call Dir again without arguments to return the next *.INI file in the 
' same directory.
MyFile = Dir()

' Return first *.TXT file, including files with a set hidden attribute.
MyFile = Dir("*.TXT", vbHidden)

' Display the names in C:\ that represent directories.
MyPath = "c:\"   ' Set the path.
MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
Do While MyName <> ""   ' Start the loop.
      ' Use bitwise comparison to make sure MyName is a directory. 
      If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then 
         ' Display entry only if it's a directory.
         MsgBox(MyName)
      End If   
   MyName = Dir()   ' Get next entry.
Loop
于 2013-02-04T19:22:02.500 に答える
1

System.IOからPath.GetExtensionを使用して拡張子を取得し、それが「.cfg」を探しているものであるかどうかをテストできます。拡張子がない場合、Path.GetExtensionは空の文字列を返します。

MSDNから

于 2018-02-13T23:50:16.143 に答える