Public Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal lpszPath As String) As Long? PathFileExists("C:\Command.COM") 1 <==存在
'给你一个模块 Attribute VB_Name = "fileexist" Option ExplicitFunction FileExists%(filename$)' Description ' Checks 'filename$' to find wether the filename given ' exists. ' ' Parameters ' Name Type Value ' ------------------------------------------------------------- ' filename$ String The filename to be checked ' ' Returns ' True if the file exists ' False if the file does not exist ' ' Last updated by Jens Balchen 21.11.95 Dim f% ' Trap any errors that may occur On Error Resume Next ' Get a free file handle to avoid using a file handle already in use f% = FreeFile ' Open the file for reading Open filename$ For Input As #f% ' Close it Close #f% ' If there was an error, Err will be <> 0. In that case, we return False FileExists% = Not (Err <> 0)End Function
Attribute VB_Name = "fileexist"
Option ExplicitFunction FileExists%(filename$)' Description
' Checks 'filename$' to find wether the filename given
' exists.
'
' Parameters
' Name Type Value
' -------------------------------------------------------------
' filename$ String The filename to be checked
'
' Returns
' True if the file exists
' False if the file does not exist
'
' Last updated by Jens Balchen 21.11.95
Dim f% ' Trap any errors that may occur
On Error Resume Next ' Get a free file handle to avoid using a file handle already in use
f% = FreeFile
' Open the file for reading
Open filename$ For Input As #f%
' Close it
Close #f%
' If there was an error, Err will be <> 0. In that case, we return False
FileExists% = Not (Err <> 0)End Function
Dir函数,还可以查目录是否存在,使用方法:Dir(目录名,参数) 参数好像是VBDir.... 忘记了,不过你在输入时VB会自动列出的。
如果dir(文件名)<>"",文件存在
语法
Dir[(pathname[, attributes])]
Dir 函数的语法具有以下几个部分:
部分 描述pathname 可选参数。用来指定文件名的字符串表达式,可能包含目录或文件夹、以及驱动器。如果没有找到 pathname,则会返回零长度字符串 ("")。
attributes 可选参数。常数或数值表达式,其总和用来指定文件属性。如果省略,则会返回匹配 pathname 的所有文件。
设置值
attributes 参数的设置可为:
常数 值 描述
vbNormal 0 常规
vbHidden 2 隐藏
vbSystem 4 系统文件
vbVolume 8 磁盘卷标;如果指定,则其它属性都会忽略
vbDirectory 16 目录或文件夹
注意 这些常数是由 VBA 所指定的,在程序代码中的任何位置,可以使用这些常数来替换真正的数值。
说明
Dir 支持多字符 (*) 和单字符 (?) 的统配符来指定多重文件。
在第一次调用 Dir 函数时,必须指定 pathname,否则会产生错误。如果也指定了文件属性,那么就必须包括 pathname。
Dir 会返回匹配 pathname 的第一个文件名。若想得到其它匹配 pathname 的文件名,再一次调用 Dir,且不要使用参数。如果已没有合乎条件的文件,则 Dir 会返回一个零长度字符串 ("")。一旦返回值为零长度字符串,并要再次调用 Dir 时,就必须指定 pathname,否则会产生错误。不必访问到所有匹配当前 pathname 的文件名,就可以改变到一个新的 pathname 上。但是,不能以递归方式来调用 Dir 函数。以 vbDirectory 属性来调用 Dir 不能连续地返回子目录。
提示 由于文件名并不会以特别的次序来返回,所以可以将文件名存储在一个数组中,然后再对这个数组排序。
示例
Dir 函数示例
本示例使用 Dir 函数来检查某些文件或目录是否存在。
Dim MyFile, MyPath, MyName' 返回“WIN.INI”(如果该文件存在)。
MyFile = Dir("C:\WINDOWS\WIN.ini")
' 返回带指定扩展名的文件名。如果超过一个 *.ini 文件存在,
' 函数将返回按条件第一个找到的文件名。
MyFile = Dir("C:\WINDOWS\*.ini")
' 若第二次调用 Dir 函数,但不带任何参数,则函数将返回同一目录下的下一个 *.ini 文件。
MyFile = Dir
' 返回找到的第一个隐式 *.TXT 文件。
MyFile = Dir("*.TXT", vbHidden)' 显示 C:\ 目录下的名称。
MyPath = "c:\" ' 指定路径。
MyName = Dir(MyPath, vbDirectory) ' 找寻第一项。
Do While MyName <> "" ' 开始循环。
' 跳过当前的目录及上层目录。
If MyName <> "." And MyName <> ".." Then
' 使用位比较来确定 MyName 代表一目录。
If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
Debug.Print MyName ' 如果它是一个目录,将其名称显示出来。
End If
End If
MyName = Dir ' 查找下一个目录。
Loop
在vb5.0里的联机手册里有很多