Dir 函数示例
本示例使用 Dir 函数来检查某些文件或目录是否存在。在 Macintosh 计算机上,默认驱动器名称是 “HD” ,并且路径部分由冒号取代反斜线隔开。而且 Microsoft Windows 的通配符在 Mac 中可以作为有效字符出现在文件名中。也可以使用 MacID 函数来指定文件组。
' 显示 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
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
试试这个,我一直用,也很好呀.一个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
这是我写的一个判断文件是否存在的一个函数
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
使用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
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