如何判断文件或文件夹是否存在??

soec 2003-02-27 11:20:48
文件: "C:\Boot.ini"
文件:"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE"
文件夹:"C:\Windows\"
文件夹:"%ProgramFiles%\Windows NT\"

怎样判断上面的文件和文件夹是否存在呢?
...全文
535 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
benxie 2003-07-10
  • 打赏
  • 举报
回复
Dir 函数示例
本示例使用 Dir 函数来检查某些文件或目录是否存在。在 Macintosh 计算机上,默认驱动器名称是 “HD” ,并且路径部分由冒号取代反斜线隔开。而且 Microsoft Windows 的通配符在 Mac 中可以作为有效字符出现在文件名中。也可以使用 MacID 函数来指定文件组。

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

kmzs 2003-07-10
  • 打赏
  • 举报
回复
%ProgramFiles%:先得到Windows路径(帖子有的是。。。)
%ProgramFiles%=left(winpath,3) & "Program Files\"
HanZhu1 2003-07-10
  • 打赏
  • 举报
回复
用dir 命令
if dir("c:\boot.ini")="" then
不存在
else
存在
end if
newwish 2003-07-10
  • 打赏
  • 举报
回复
谢谢 bluelang(多情浪子)
蒋宏伟 2003-02-28
  • 打赏
  • 举报
回复
我不是为了要你给分,真是希望我能帮你什么,下面是我收集和自己写的一些文件处理函数.
Option Explicit

Function FileExists(ByVal Filename As String) As Integer
Dim Temp$

'Set Default
FileExists = True

'Set up error handler
On Error Resume Next

'Attempt to grab date and time
Temp$ = FileDateTime(Filename)

'Process errors
Select Case Err
Case 53, 76, 68 'File Does Not Exist
FileExists = False
Err = 0
Case Else
If Err <> 0 Then
FileExists = False
Err = 0
End If
End Select
End Function

Function AddPathToFile(ByVal sPathIn As String, ByVal sFileNameIn As String) As String
'*******************************************************************
'
' PURPOSE: Takes a path (including Drive letter and any subdirs) and
' concatenates the file name to path. Path may be empty, path
' may or may not have an ending backslash '\'. No validation
' or existance is check on path or file.
'
' INPUTS: sPathIn - Path to use
' sFileNameIn - Filename to use
'
'
' OUTPUTS: N/A
'
' RETURNS: Path concatenated to File.
'
'*******************************************************************
Dim sPath As String
Dim sFileName As String


'Remove any leading or trailing spaces
sPath = Trim$(sPathIn)
sFileName = Trim$(sFileNameIn)

If sPath = "" Then
AddPathToFile = sFileName
Else
If Right$(sPath, 1) = "\" Then
AddPathToFile = sPath & sFileName
Else
AddPathToFile = sPath & "\" & sFileName
End If
End If

End Function

Function ExtractFileName(sFileName As Variant) As String
'*******************************************************************
'
' PURPOSE: This returns just a file name from a full/partial path.
'
' INPUTS: sFileName - String Data to remove path from.
'
' OUTPUTS: N/A
'
' RETURNS: This function returns all the characters from right to the
' first \. Does NOT check validity of the filename....
'
'*******************************************************************
Dim nIdx As Integer

If Right(Trim(sFileName), 1) = "\" Then
ExtractFileName = ""
Else

For nIdx = Len(sFileName) To 1 Step -1
If Mid$(sFileName, nIdx, 1) = "\" Then
ExtractFileName = Mid$(sFileName, nIdx + 1)
Exit Function
End If
Next nIdx

ExtractFileName = sFileName
End If
End Function

Function ExtractPath(sFileName) As String
'*******************************************************************
'
' PURPOSE: This returns just a path name from a full/partial path.
'
' INPUTS: sFileName - String Data to remove file from.
'
' OUTPUTS: N/A
'
' RETURNS: This function returns all the characters from left to the last
' first \. Does NOT check validity of the filename/Path....
'*******************************************************************
Dim nIdx As Integer


For nIdx = Len(sFileName) To 1 Step -1
If Mid$(sFileName, nIdx, 1) = "\" Then
ExtractPath = Mid$(sFileName, 1, nIdx)
Exit Function
End If
Next nIdx

ExtractPath = sFileName

End Function


'提取命令行文件名
Public Function ExtractCommandFiles(sFileName As String, xFilename() As String) As Integer
If sFileName = "" Then
ExtractCommandFiles = 0
Else
ReDim Preserve xFilename(0)
Dim FlagStart As Boolean '是否是"号的开始
Dim Slen As Integer '字符串参数长度
Dim ai As Integer '数组开始序号
Dim CString As String '每次提取的字符
FlagStart = True
Slen = Len(sFileName)
ai = 0
Dim i As Integer
For i = 1 To Slen
CString = Mid(sFileName, i, 1)
Select Case CString
Case Chr(32) '空格
If FlagStart Then
If Not Trim(xFilename(ai)) = "" Then
ai = ai + 1
ReDim Preserve xFilename(ai)
End If
Else
xFilename(ai) = xFilename(ai) + CString
End If
' FlagStart = Not FlagStart

Case Chr(34) ' "号
If FlagStart Then
If Not Trim(xFilename(ai)) = "" Then
ai = ai + 1
ReDim Preserve xFilename(ai)
End If
End If
FlagStart = Not FlagStart

Case Else

xFilename(ai) = xFilename(ai) + CString
End Select
Next i
ExtractCommandFiles = ai + 1
End If
End Function


'判断是文件还是目录


Private Declare Function GetFileAttributes Lib "kernel32" Alias _
"GetFileAttributesA" (ByVal lpFileName As String) As Long

Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_HIDE = &H2
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_NORMAL = &H80

Private Function IsFile(sFile As String) As FileProp '判断文件属性
IsFile.ReadOnly = (GetFileAttributes(sFile) And _
FILE_ATTRIBUTE_READONLY)

IsFile.Hide = (GetFileAttributes(sFile) And _
FILE_ATTRIBUTE_HIDE)

IsFile.Archive = (GetFileAttributes(sFile) And _
FILE_ATTRIBUTE_ARCHIVE)


IsFile.System = (GetFileAttributes(sFile) And _
FILE_ATTRIBUTE_SYSTEM)

IsFile.IsFile = (GetFileAttributes(sFile) And _
FILE_ATTRIBUTE_DIRECTORY)
IsFile.IsFile = Not IsFile.IsFile
End Function
蒋宏伟 2003-02-28
  • 打赏
  • 举报
回复
试试这个,我一直用,也很好呀.一个VB资源盘上的不是我自己的东东
Function FileExists(ByVal Filename As String) As Integer
Dim Temp$

'Set Default
FileExists = True

'Set up error handler
On Error Resume Next

'Attempt to grab date and time
Temp$ = FileDateTime(Filename)

'Process errors
Select Case Err
Case 53, 76, 68 'File Does Not Exist
FileExists = False
Err = 0
Case Else
If Err <> 0 Then
FileExists = False
Err = 0
End If
End Select
End Function
用户 昵称 2003-02-28
  • 打赏
  • 举报
回复
好像只有这样吧。
wuzhiliang 2003-02-28
  • 打赏
  • 举报
回复
Dim oFileSys As FileSystemObject

Set oFileSys = New FileSystemObject

'文件甲
If oFileSys.FolderExists("C:\Windows\") = False Then
'不存在
End If
'文件
If objFileSys.FileExists(FileName) = False Then
'不存在
End If
qiqif 2003-02-28
  • 打赏
  • 举报
回复
这是我写的一个判断文件是否存在的一个函数
Function reportfilestatus(filespec) As Boolean
'Dim fso, msg
Set fso = CreateObject("scripting.filesystemobject")
If (fso.FileExists(filespec)) Then
reportfilestatus = True
Else
reportfilestatus = False
End If
End Function
bluelang 2003-02-28
  • 打赏
  • 举报
回复
Dir[(pathname[, attributes])]

Dir 函数的语法具有以下几个部分:

部分 描述
pathname 可选参数。用来指定文件名的字符串表达式,可能包含目录或文件夹、以及驱动器。如果没有找到 pathname,则会返回零长度字符串 ("")。
attributes 可选参数。常数或数值表达式,其总和用来指定文件属性。如果省略,则会返回匹配 pathname 但不包含属性的文件。


设置值

attributes 参数的设置可为:

常数 值 描述
vbNormal 0 (缺省) 指定没有属性的文件。
vbReadOnly 1 指定无属性的只读文件
vbHidden 2 指定无属性的隐藏文件
VbSystem 4 指定无属性的系统文件
vbVolume 8 指定卷标文件;如果指定了其它属性,则忽略vbVolume
vbDirectory 16 指定无属性文件及其路径和文件夹。
zw_yu 2003-02-28
  • 打赏
  • 举报
回复
"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE"
试试看:
Environ("ProgramFiles") & "\Windows NT\Accessories\WORDPAD.EXE"
zdleek 2003-02-28
  • 打赏
  • 举报
回复
使用Win32API OpenFile(),检查文件是绝对可以,文件夹我没试过,你自己试一下。代码最好放在公共模块中,如下
'================= Win32 API and CONSTANTS for API ==================

'for File operation
Public Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Public Const OF_EXIST = &H4000
Public Const OFS_MAXPATHNAME = 128
Public Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type

Public Function FileExists(ByVal sFilename As String) As Boolean
'----------------------------
'*** function: FileExists()
'*** Abstract: Check if the file exists
'*** Return : True - file exists,False - file not exists
'created on 2002-07-17 ,zdleek
'----------------------------

Dim udtOFStruct As OFSTRUCT

On Error GoTo CheckErr
FileExists = False
If Len(sFilename) > 0 Then
OpenFile sFilename, udtOFStruct, OF_EXIST
FileExists = udtOFStruct.nErrCode <> 2
End If

Exit Function

CheckErr:
FileExists = False

End Function
tollers 2003-02-28
  • 打赏
  • 举报
回复
dir(FilePath)<>"" 存在
chenyu5188 2003-02-28
  • 打赏
  • 举报
回复
用VB提供的dir()函数啊
soec 2003-02-27
  • 打赏
  • 举报
回复
但是好像不能判断
文件:"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE"
这种文件路径.怎么办?

难道要先读取所有系统路径,然后不停的case 吗? 有更好的办法吗?
soec 2003-02-27
  • 打赏
  • 举报
回复
????
soec 2003-02-27
  • 打赏
  • 举报
回复
那么怎样判断
文件:"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE"
是否存在呢?
用户 昵称 2003-02-27
  • 打赏
  • 举报
回复
如果不支持长路径,可以参考这个
Place this code in a .bas module

Option Explicit

Public Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As Long

Public Function GetShortName(sFile As String) As String
Dim sShortFile As String * 67
Dim lResult As Long

'Make a call to the GetShortPathName API
lResult = GetShortPathName(sFile, sShortFile, _
Len(sShortFile))

'Trim out unused characters from the string.
GetShortName = Left$(sShortFile, lResult)

End Function
'--end code block
Now place this code in the Form_Load event

Private Sub Form_Load()

Debug.Print GetShortName(App.Path)

End Sub
cotaxyp 2003-02-27
  • 打赏
  • 举报
回复
判断文件是否存在:if dir(filename,vbNormal)=vbnullstring then '判断文件是否存在;
判断文件是否存在:if dir(filename,vbDirectory)=vbnullstring then '判断文件夹是否存在;
xinshou1979330 2003-02-27
  • 打赏
  • 举报
回复
用DIR函数这个问题我刚问过
http://expert.csdn.net/Expert/topic/1471/1471690.xml?temp=.6334955
这个帖子对你一定有帮助的
加载更多回复(1)

7,765

社区成员

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

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