怎样判断文件的存在?

ayou 2000-08-15 02:39:00
听说好象要用到file system object对象,file system object对象是什么啊?哪里有?
...全文
159 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lty 2000-08-15
  • 打赏
  • 举报
回复
头大了呀,测一下文件长,出错不就不存在了。
  • 打赏
  • 举报
回复

'/////////////////////////////////////////////////////////////////////
'//名称: IsFileExist
'//功能: judge the file exist or not
'//入口: FilePath, FileName
'//出口:
'//返回值:
' True: Exist
' False:Nonexist
'/////////////////////////////////////////////////////////////////////
Public Function IsFileExist(FilePath As String, Optional ByVal FileName As String) As Boolean
Dim strFile As String
If FileName <> "" Then
strFile = FilePath & "\" & FileName
Else
strFile = FilePath
FileName = GetFileName(FilePath)
End If

If UCase(Dir(strFile)) = UCase(FileName) Then
IsFileExist = True
Else
IsFileExist = False
End If
End Function
lwj3001 2000-08-15
  • 打赏
  • 举报
回复
这段VC代码你换成VB的就行了。应该能看的懂。
BOOL IsThisFileExist( LPCSTR lpszFullPath )
{
BOOL bResult = FALSE;
HANDLE hTestExistFile = NULL;
WIN32_FIND_DATA findData = {0};
hTestExistFile = FindFirstFile( lpszFullPath, &findData );
if( hTestExistFile != INVALID_HANDLE_VALUE )
{
FindClose( hTestExistFile );
bResult = TRUE;
}

return bResult;
}
kingxing 2000-08-15
  • 打赏
  • 举报
回复
用我这里的API:
Set fs = CreateObject("Scripting.FileSystemObject")
if fs.FileExists("Autoexec.bat") then
文件存在
else
不存在
end if
earphone 2000-08-15
  • 打赏
  • 举报
回复
用这个就是你说的那种方法,打开一个文件如果出错哪就是文件不存在
Set Fos = CreateObject("Scripting.FileSystemObject")
Set Fil = Fos.createtextfile("Config.ini", True)
fos为file system object
config.ini为你要打开的文件
shines77 2000-08-15
  • 打赏
  • 举报
回复
Wingsun: 要搞得这么复杂吗?
ayou: 用Dir 函数就可以了。

Dim RetFile As String
RetFile = Dir("C:\Windows\My.Ini",vbNormal)

如果存在返回文件名"My.ini", 如果不存在返回空字符串。
具体的说明请看VB的帮助文件,查找Dir 。
Areslee 2000-08-15
  • 打赏
  • 举报
回复
用API函数CreateFile()可以做到
Wingsun 2000-08-15
  • 打赏
  • 举报
回复
完全可以不用
Public Const gstrQUOTE$ = """"

'-----------------------------------------------------------
' FUNCTION: FileExists
' Determines whether the specified file exists
'
' IN: [strPathName] - file to check for
'
' Returns: True if file exists, False otherwise
'-----------------------------------------------------------
'
Public Function FileExists(ByVal strPathName As String) As Boolean
Dim intFileNum As Integer

On Error Resume Next

'
' If the string is quoted, remove the quotes.
'
strPathName = strUnQuoteString(strPathName)
'
'Remove any trailing directory separator character
'
If Right$(strPathName, 1) = gstrSEP_DIR Then
strPathName = Left$(strPathName, Len(strPathName) - 1)
End If

'
'Attempt to open the file, return value of this function is False
'if an error occurs on open, True otherwise
'
intFileNum = FreeFile
Open strPathName For Input As intFileNum

FileExists = (Err.Number = 0)

Close intFileNum

Err.Clear
End Function
Public Function strUnQuoteString(ByVal strQuotedString As String)
'
' This routine tests to see if strQuotedString is wrapped in quotation
' marks, and, if so, remove them.
'
strQuotedString = Trim$(strQuotedString)

If Mid$(strQuotedString, 1, 1) = gstrQUOTE Then
If Right$(strQuotedString, 1) = gstrQUOTE Then
'
' It's quoted. Get rid of the quotes.
'
strQuotedString = Mid$(strQuotedString, 2, Len(strQuotedString) - 2)
End If
End If
strUnQuoteString = strQuotedString
End Function
huntout 2000-08-15
  • 打赏
  • 举报
回复
直接用Dir Function

Dir(pathname)返回的是"",則文件不存在!
Mike 2000-08-15
  • 打赏
  • 举报
回复
判断文件的存在用 Dir就行了,如果需要对文件操作再用filr system object吧.

7,759

社区成员

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

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