怎样判断文件的存在?

ayou 2000-08-15 02:39:00
听说好象要用到file system object对象,file system object对象是什么啊?哪里有?
...全文
164 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吧.
Re:  FastDFS 分布式文件系统部署 ============================== FastDFS 分布式存储主要功能有:文件存储,文件同步,文件访问(文件上传/下载),特别适合以文件为载体的在线服务,如图片网站,视频网站等 它的存储特点是对文件体积小或超小,同时文件的数量特别多的存储情况支持较好,(如果是大文件的存储就推荐使用 Glusterfs 分布式网络文件存储),所谓的小文件就是指网站的图片,文档或者小视频等等,体积范围在4K~500MB之间。所谓的大文件指软件的镜像包、电影等等。 除了FastDFS存储文件特点之外,它同时具备自我负载均衡的能力,可以解决了运维自动化的关键问题。 # 块存储、文件存储、对象存储这三者的本质差别是什么? 1)块存储:磁盘阵列,硬盘(块存储主要是将裸磁盘空间整个映射给主机使用的) 2)文件存储:FTP、NFS服务器(解决文件无法共享的问题) 3)对象存储:内置大容量硬盘的分布式服务器(解决容量、性能、可靠性等问题) 多台服务器内置大容量硬盘,再装上对象存储软件, 然后再额外搞几台服务作为管理节点,安装上对象存储管理软件。 # gluterfs主要运行场景: 存储大文件,针对小文件或超小文件的支持比较差 KVM镜像 视频文件 # FastDFS主要运行场景: 存放文件size范围:4K~500MB # 对象存储 - GlusterFS、Ceph、FastDFS(非对象存储) 必备技能:DRBD/NFS/MooseFS/ GlusterFS/ FastDFS/TFS(http://tfs.taobao.org/)/ # 访问客户端:只能专用API访问实现 存放单元文件size范围:4K~500MB(阅读、声音、视频网站) # 典型用户:      ... ... 

7,763

社区成员

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

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