VB中检测文件是否存在
2011-09-03 23:14
288 查看
问题:
在VB中检测文件是否存在?
方法:
1.用Dir函数,如果函数返回为空字符串则文件不存在。
例:If Dir(C:\Win95\My.txt") = "" Then
MsgBox "文件不存在。"
End If
注意: 文件名要写完整,包括扩展名.
使用Dir("文件名")判断,但是当主调函数也正在用dir并且后续使用没有结束时就会出错。
如果还要对目录或者驱动器进行其他操作:
Public Function FileIsExist(Filename As String, Optional Attribus As VbFileAttribute = vbArchive) As Boolean
Dim cName As String
cName = Dir(Filename, Attribus)
FileIsExist = (Format(cName, ">") = Format(StripFile(Filename), ">"))
End Function
Public Function StripFile(Name As String) As String
Dim pos As Integer, pos1 As Integer
pos = InStr(Name, "\")
While pos <> 0
If pos <> 0 Then pos1 = pos
pos = InStr(pos + 1, Name, "\")
Wend
StripFile = Right(Name, Len(Name) - pos1)
End Function
2.用FileLen(pathname)函数,该函数返回一个 Long,代表一个文件的长度,单位是字节。
pathname 参数是用来指定一个文件名的字符串表达式。pathname 可以包含目录或文件夹、以及驱动器。
例:
If FileLen("TESTFILE") then
MsgBox "文件存在。"
End if
注意: 文件名要写完整,包括扩展名.
如果参数为目录名,则运行时刻出错.
3.用FileExists 方法
先在“引用”对话框中选中Microsoft Scripting Runtime
声明
例:
Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(App.path & "\Template.dat") = False Then
MsgBox "缺少Template.dat文件!", vbInformation
End If
注意:使用FileSystemObject(fso).程序发布时要带runtime文件,防止客户机器没有这个文件这样程序变成多个文件,很多操作系统。
4.最好的方法:
Public Function FileExists(ByVal File As String) As Boolean
On Error Resume Next
If (GetAttr(File) And vbDirectory) = False Then FileExists = True
If err Then FileExists = False: err.Clear
End Function
Function FolderExists(ByVal Folder As String) As Boolean
On Error Resume Next
If GetAttr(Folder) And vbDirectory Then FolderExists = True
If err Then FolderExists = False: err.Clear
End Function
上面都是用的vbDirectory=16 不要认为写错了
5.还有一个
'*************************************************************************
'**模 块 名:mod_GetFileExists
'**说 明:丹心软件在线设计 版权所有2007 - 2008(C)
'**创 建 人:丹心
'**日 期:2007-11-14 21:23:17
'**版 本:V1.0.0
'**博客地址:http://hi.baidu.com/starwork/
'**QQ 号码:121877114
'**E - mail:cnstarwork@126.com
'*************************************************************************
Option Explicit
'api_GetPATH:vb中检测文件是否存在
Private Declare Function SHFileExists Lib "shell32" Alias "#45" (ByVal szPath As String) As Long
'这个函数除了能判断文件是否存在外,还可以判断本地或远程文件夹
'"\\192.168.0.2\d$\javatools\somefile.txt"
'"\\workstation\javatools\somefile.txt"
'"http://www.microsoft.com/ms.htm"
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
'自定义全部属性,以检测隐藏或系统文件
Private Const vbAllFileAttrib = vbNormal + vbReadOnly + vbHidden + vbSystem + vbVolume + vbDirectory
Private Enum SelectCheckFile
DIRCheck = 0 '方法1,Dir检测文件是否存在
OPENCheck = 1 '方法2,Open检测文件是否存在
GetFileCheck = 2 '判断指定文件的属性
PathFileCheck = 3 '判断文件是否存在外
SHFileExistsCheck = 4
End Enum
Public Function GetFileExists(filename As String, Index As Integer) As Boolean
Dim fileIN As String, apiRet As Long
On Error GoTo FileDoesNotExist
If filename = "" Then GetFileExists = False: Exit Function
GetFileExists = False
Select Case Index
Case DIRCheck
fileIN = Dir$(filename, vbAllFileAttrib)
If fileIN <> vbNullString Then GetFileExists = True
Case OPENCheck
Open filename For Input As #1
Close #1
GetFileExists = True
Case GetFileCheck
apiRet& = Str$(GetFileAttributes(filename)) '返回负一则文件不存在
If Not apiRet < 0 Then GetFileExists = True
Case PathFileCheck '返回0则文件不存在
GetFileExists = CBool(PathFileExists(filename))
Case SHFileExistsCheck
If SHFileExists(filename) <> 0 Then GetFileExists = True
Case Else
GetFileExists = False
End Select
Exit Function
FileDoesNotExist:
GetFileExists = False '文件不存在跳转
End Function
''test
'Private Sub Command1_Click()
'MsgBox GetFileExists(App.Path & "\1.exe", 1)
'MsgBox GetFileExists(App.Path & "\1.exe", 2)
'MsgBox GetFileExists(App.Path & "\1.exe", 3)
'MsgBox GetFileExists(App.Path & "\1.exe", 4)
'MsgBox GetFileExists(App.Path & "\1.exe", 0)
'End Sub
http://hi.baidu.com/starwork/blog/item/b04d3c465760160a6a63e56e.html
在VB中检测文件是否存在?
方法:
1.用Dir函数,如果函数返回为空字符串则文件不存在。
例:If Dir(C:\Win95\My.txt") = "" Then
MsgBox "文件不存在。"
End If
注意: 文件名要写完整,包括扩展名.
使用Dir("文件名")判断,但是当主调函数也正在用dir并且后续使用没有结束时就会出错。
如果还要对目录或者驱动器进行其他操作:
Public Function FileIsExist(Filename As String, Optional Attribus As VbFileAttribute = vbArchive) As Boolean
Dim cName As String
cName = Dir(Filename, Attribus)
FileIsExist = (Format(cName, ">") = Format(StripFile(Filename), ">"))
End Function
Public Function StripFile(Name As String) As String
Dim pos As Integer, pos1 As Integer
pos = InStr(Name, "\")
While pos <> 0
If pos <> 0 Then pos1 = pos
pos = InStr(pos + 1, Name, "\")
Wend
StripFile = Right(Name, Len(Name) - pos1)
End Function
2.用FileLen(pathname)函数,该函数返回一个 Long,代表一个文件的长度,单位是字节。
pathname 参数是用来指定一个文件名的字符串表达式。pathname 可以包含目录或文件夹、以及驱动器。
例:
If FileLen("TESTFILE") then
MsgBox "文件存在。"
End if
注意: 文件名要写完整,包括扩展名.
如果参数为目录名,则运行时刻出错.
3.用FileExists 方法
先在“引用”对话框中选中Microsoft Scripting Runtime
声明
例:
Dim fso As FileSystemObject
Set fso = CreateObject("Scripting.FileSystemObject")
If fso.FileExists(App.path & "\Template.dat") = False Then
MsgBox "缺少Template.dat文件!", vbInformation
End If
注意:使用FileSystemObject(fso).程序发布时要带runtime文件,防止客户机器没有这个文件这样程序变成多个文件,很多操作系统。
4.最好的方法:
Public Function FileExists(ByVal File As String) As Boolean
On Error Resume Next
If (GetAttr(File) And vbDirectory) = False Then FileExists = True
If err Then FileExists = False: err.Clear
End Function
Function FolderExists(ByVal Folder As String) As Boolean
On Error Resume Next
If GetAttr(Folder) And vbDirectory Then FolderExists = True
If err Then FolderExists = False: err.Clear
End Function
上面都是用的vbDirectory=16 不要认为写错了
5.还有一个
'*************************************************************************
'**模 块 名:mod_GetFileExists
'**说 明:丹心软件在线设计 版权所有2007 - 2008(C)
'**创 建 人:丹心
'**日 期:2007-11-14 21:23:17
'**版 本:V1.0.0
'**博客地址:http://hi.baidu.com/starwork/
'**QQ 号码:121877114
'**E - mail:cnstarwork@126.com
'*************************************************************************
Option Explicit
'api_GetPATH:vb中检测文件是否存在
Private Declare Function SHFileExists Lib "shell32" Alias "#45" (ByVal szPath As String) As Long
'这个函数除了能判断文件是否存在外,还可以判断本地或远程文件夹
'"\\192.168.0.2\d$\javatools\somefile.txt"
'"\\workstation\javatools\somefile.txt"
'"http://www.microsoft.com/ms.htm"
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long
'自定义全部属性,以检测隐藏或系统文件
Private Const vbAllFileAttrib = vbNormal + vbReadOnly + vbHidden + vbSystem + vbVolume + vbDirectory
Private Enum SelectCheckFile
DIRCheck = 0 '方法1,Dir检测文件是否存在
OPENCheck = 1 '方法2,Open检测文件是否存在
GetFileCheck = 2 '判断指定文件的属性
PathFileCheck = 3 '判断文件是否存在外
SHFileExistsCheck = 4
End Enum
Public Function GetFileExists(filename As String, Index As Integer) As Boolean
Dim fileIN As String, apiRet As Long
On Error GoTo FileDoesNotExist
If filename = "" Then GetFileExists = False: Exit Function
GetFileExists = False
Select Case Index
Case DIRCheck
fileIN = Dir$(filename, vbAllFileAttrib)
If fileIN <> vbNullString Then GetFileExists = True
Case OPENCheck
Open filename For Input As #1
Close #1
GetFileExists = True
Case GetFileCheck
apiRet& = Str$(GetFileAttributes(filename)) '返回负一则文件不存在
If Not apiRet < 0 Then GetFileExists = True
Case PathFileCheck '返回0则文件不存在
GetFileExists = CBool(PathFileExists(filename))
Case SHFileExistsCheck
If SHFileExists(filename) <> 0 Then GetFileExists = True
Case Else
GetFileExists = False
End Select
Exit Function
FileDoesNotExist:
GetFileExists = False '文件不存在跳转
End Function
''test
'Private Sub Command1_Click()
'MsgBox GetFileExists(App.Path & "\1.exe", 1)
'MsgBox GetFileExists(App.Path & "\1.exe", 2)
'MsgBox GetFileExists(App.Path & "\1.exe", 3)
'MsgBox GetFileExists(App.Path & "\1.exe", 4)
'MsgBox GetFileExists(App.Path & "\1.exe", 0)
'End Sub
http://hi.baidu.com/starwork/blog/item/b04d3c465760160a6a63e56e.html
相关文章推荐
- vb中检测文件是否存在
- 检测文件是否存在
- 中望CAD VBA检测文件是否存在
- 检测文件夹&文件是否存在
- C++检测文件或者目录是否存在
- C# 判断 检测 access 建库 建表 文件是否存在等 数据操作 使用 ADOX
- 使用jQuery来检测远程图片文件是否存在
- Symbian S60 如何检测文件是否存在
- VB-检测指定的进程是否存在
- C# 判断 检测 access 建库 建表 文件是否存在等 数据操作 使用 ADOX
- ASP检测远程文件是否存在
- 检测文件是否存在的三函数对比
- 自动化测试:k8s环境下,通过检测文件是否存在来自动启停tomcatapp的方法_20160316_七侠镇莫尛貝
- VB中如何判断文件、文件夹是否存在
- 在VB中如何判断文件、文件夹是否存在和生成文件夹
- VB.Net判断目录或文件是否存在的实例
- 原来php的dirname不检测文件是否存在
- vb 检测进程是否存在
- 自动化测试:k8s环境下,通过检测文件是否存在来自动启停tomcatapp的方法_20160316_七侠镇莫尛貝
- 检测文件是否存在于沙盒中