文件: "C:\Boot.ini"
文件:"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE"
文件夹:"C:\Windows\"
文件夹:"%ProgramFiles%\Windows NT\"怎样判断上面的文件和文件夹是否存在呢?
文件:"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE"
文件夹:"C:\Windows\"
文件夹:"%ProgramFiles%\Windows NT\"怎样判断上面的文件和文件夹是否存在呢?
不存在
else
存在
end if
http://expert.csdn.net/Expert/topic/1471/1471690.xml?temp=.6334955
这个帖子对你一定有帮助的
判断文件是否存在:if dir(filename,vbDirectory)=vbnullstring then '判断文件夹是否存在;
Place this code in a .bas moduleOption ExplicitPublic Declare Function GetShortPathName Lib "kernel32" _
Alias "GetShortPathNameA" _
(ByVal lpszLongPath As String, _
ByVal lpszShortPath As String, _
ByVal cchBuffer As Long) As LongPublic 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 eventPrivate Sub Form_Load() Debug.Print GetShortName(App.Path)End Sub
文件:"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE"
是否存在呢?
文件:"%ProgramFiles%\Windows NT\Accessories\WORDPAD.EXE"
这种文件路径.怎么办?难道要先读取所有系统路径,然后不停的case 吗? 有更好的办法吗?
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
Option ExplicitFunction 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 FunctionFunction 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 IfEnd FunctionFunction 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 FunctionFunction 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 = sFileNameEnd 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 LongPrivate 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 = &H80Private 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
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
If oFileSys.FolderExists("C:\Windows\") = False Then
'不存在
End If
'文件
If objFileSys.FileExists(FileName) = False Then
'不存在
End If
'================= 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 TypePublic 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 FunctionCheckErr:
FileExists = False
End Function
试试看:
Environ("ProgramFiles") & "\Windows NT\Accessories\WORDPAD.EXE"
pathname 可选参数。用来指定文件名的字符串表达式,可能包含目录或文件夹、以及驱动器。如果没有找到 pathname,则会返回零长度字符串 ("")。
attributes 可选参数。常数或数值表达式,其总和用来指定文件属性。如果省略,则会返回匹配 pathname 但不包含属性的文件。
设置值attributes 参数的设置可为:常数 值 描述
vbNormal 0 (缺省) 指定没有属性的文件。
vbReadOnly 1 指定无属性的只读文件
vbHidden 2 指定无属性的隐藏文件
VbSystem 4 指定无属性的系统文件
vbVolume 8 指定卷标文件;如果指定了其它属性,则忽略vbVolume
vbDirectory 16 指定无属性文件及其路径和文件夹。
if dir("c:\boot.ini")="" then
不存在
else
存在
end if
%ProgramFiles%=left(winpath,3) & "Program Files\"
本示例使用 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