判断文件是否存在

darksinada 2004-08-12 07:44:59
在VB6.0中如何判断一个路径下的文件是否存在。
...全文
343 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhtzz 2004-08-14
  • 打赏
  • 举报
回复
请问楼上的各位!!你们知道哪种速度是最快的吗?
我知道的有很我,但我不知道哪个速度快,如dir,filelen,这些的都可以,但我不知道哪个快呀
BlueBeer 2004-08-13
  • 打赏
  • 举报
回复
if len(dir(filepath)) then 存在 else 不存在
幻影时空 2004-08-13
  • 打赏
  • 举报
回复
if len(dir("C:\wbc.exe")) then
msgbox"在哟!"
else
msgbox "没有吧"
end if
jackyhyoung 2004-08-13
  • 打赏
  • 举报
回复

Dim i As Integer
Dim SavePath As String
On Error GoTo B
SavePath = Dir(sfilePath & "\*.xls")
isSave = True
Do While SavePath <> ""
If UCase(SavePath) = UCase(sfileName & ".xls") Then
i = MsgBox("已經存在一個名為" & sfileName & ".xls 的文件,是否覆蓋?", vbQuestion + vbYesNo + vbDefaultButton2)
If i = vbNo Then
isSave = False
Else
isSave = True
Kill sFilePath & "\" & sFileName & ".xls"
Exit Do
End If
End If
SavePath = Dir
Loop
Exit Sub
B:
If Err = 75 Then
MsgBox sFilePath & "\" & sFileName & ".xls 已經打開,Excel表不能保存", vbCritical
isSave = False
End If
bubu198 2004-08-13
  • 打赏
  • 举报
回复
dir(path+filename)
BlueBeer 2004-08-12
  • 打赏
  • 举报
回复
当然是用dir最方便啦

len(dir(filepath))
Andy__Huang 2004-08-12
  • 打赏
  • 举报
回复
Set fs = CreateObject("Scripting.FileSystemObject")

If (fs.fileexists(filepath)) Then
msgbox"文件存在!"
Else
msgbox"文件不存在!"
End If
dongge2000 2004-08-12
  • 打赏
  • 举报
回复
一楼的好,我的和他差不多。
Option Explicit

Public Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Public Declare Function SetFileAttributes Lib "kernel32" Alias "SetFileAttributesA" (ByVal lpFileName As String, ByVal dwFileAttributes As Long) As Long
Public Const FILE_ATTRIBUTE_READONLY = &H1
Public Const FILE_ATTRIBUTE_HIDDEN = &H2
Public Const FILE_ATTRIBUTE_SYSTEM = &H4

Enum wAtt
ReadOnly = 1
Hidden = 2
Readonly_Hidden = 3
System = 4
Hidden_System = 6
R_H_S = 7
All = 39
End Enum

Public Function File_AttRead(ByVal PathName As String, ByVal OutAtt As wAtt) As Boolean
On Error Resume Next
If GetFileAttributes(PathName) = -1 Then
File_AttRead = False'没有文件

Else
OutAtt = GetFileAttributes(PathName)
End If
End Function

Public Function File_AttWirte(ByVal PathName As String, ByVal inAtt As wAtt) As Boolean
On Error Resume Next
If GetFileAttributes(PathName) = -1 Then
File_AttWirte = False
Else
SetFileAttributes PathName, inAtt
End If
End Function
'wAtt:
' 1=Readonly
' 2=Hidden
' 3=Readonly & Hidden
' 4=System
' 6=Hidden &System
' 7=Readonly & Hidden & System
' 32=Archive
' 39=All
northwolves 2004-08-12
  • 打赏
  • 举报
回复
Function findfile(dirpath As String, filename As String) As Boolean
findfile = True
dirpath = IIf(Right(dirpath, 1) = "\", dirpath, dirpath & "\")
If Dir(dirpath & filename) = "" Then findfile = False
End Function
libingao 2004-08-12
  • 打赏
  • 举报
回复
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim FileNameStr As String

FileNameStr = "c:\winnt\123.txt" '要查询的文件路径及名称
If fso.FileExists(FileNameStr) Then
MsgBox "文件:" + FileNameStr + "存在!"
Else
MsgBox "文件:" + FileNameStr + "不存在!"
End If
iwzw 2004-08-12
  • 打赏
  • 举报
回复
Dir(filename) <>""
leolan 2004-08-12
  • 打赏
  • 举报
回复
Public Function IsFileExist(ByVal strFileName As String) As Boolean
On Error GoTo ErrHandle

IsFileExist = False

If (GetAttr(strFileName) And vbArchive) = vbArchive Then
IsFileExist = True
End If

Exit Function
ErrHandle:
If Err.Number = 53 Then '[File not found]
IsFileExist = False
Err.Clear
End If
End Function

1,485

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧