我在vb6中处理文件系统问题。我必须找出,如果给定的目录包含任何jpg文件(* .jpg)。
有人能告诉我如何用vb6函数检查这个吗?
答案 0 :(得分:3)
检查文件存在的快速,简单方法是使用Dir
命令:
Public Function FileExists(strFileSpec As String) As Boolean
FileExists = (Dir(strFileSpec) <> "")
End Function
这适用于通配符,因此您可以:
If Not FileExists("C:\MyFolder\*.JPG") Then
MsgBox "No JPegs found in C:\MyFolder")
End If
答案 1 :(得分:1)
'检查驱动器\文件是否存在,打开它。 'True = OK,False =错误
Private Function ckFile() As Boolean
On Error GoTo Err_XXX
Dim SelectedJPGFile as String
Dim gPath as String 'Specify your given directory
Dim sTemp As String
Dim sDrive As String
Dim fso As New FileSystemObject
Dim fil As File
Dim ts As TextStream
Dim sStream As String
If SelectedJPGFile = "" Then
sTemp = sDrive & ":\*.jpg"
Else
sTemp = gPath & "\" & SelectedJPGFile
End If
On Error GoTo Err_First
Set fil = fso.GetFile(sTemp)
On Error GoTo Err_XXX
sFullName = sTemp
Set ts = fil.OpenAsTextStream(ForReading)
ckFile = True
GoTo Exit_XXX
Exit_XXX:
SelectedJPGFile = ""
Exit Function
If Err.Number = 53 Then
ckFile = False
MsgBox "Drive does not contain the JPG File
." & vbCrLf & _
"Please check the File and try again.", vbCritical, "Check Directory"
Resume Exit_XXX
End If
Err_First:
Err_XXX:
ckFile = False
MsgBox Err.DESCRIPTION
Resume Exit_XXX
End Function