检查文件或目录是否存在

丝路花雨
发信人: fuchsia (gg), 信区: CGI
标 题: 检查文件或目录是否存在
发信站: 开放空间 (Sun Oct 10 17:34:05 1999), 转信
==================================================================
=========
板主圈点:
这个题目最初由fuchsia(gg)提出来,使用的方法太笨笨了,大家可以在后面看到,
但是不用去看了,看我的就行了。
Set fso = Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(sPath) Then
...
End If
以上是检查文件是否存在,FileExists函数是内置的,不用自己去编了
如果是检查目录是否存在,把函数名改成 FolderExists 就行了。
以下就不用看了。这是 dirty code 的典范,大家可以瞻仰一下,本来是要删除的,
但是确实是一个坏标本。
==================================================================
===========
检查文件在指定的目录中是否存在
FUNCTION FileExists(ByVal strDir, ByVal strFileName)
' Need this to prevent showing runtime errors
ON ERROR RESUME NEXT
' Default to true
FileExists = TRUE
' Create the filename to use, this is a web dir, / = root.
FN = (Server.MapPath(strDir) & "\" & strFileName)
' Create FileSystemObject
SET FS = Server.CreateObject("Scripting.FileSystemObject")
' Open the file, treat as textfile, doesn't matter
SET InStream=FS.OpenTextFile (FN, 1, FALSE, FALSE)
' If anything other than 0 then couldn't open the file
IF (Err.Number<>0) THEN
' Clear error
Err.Clear
' File doesn't exist
FileExists = FALSE
END IF
' Close the input stream
Instream.Close
' Kill the objects
SET InStream = Nothing
SET FS = Nothing
END FUNCTION
--
☆ 来源:.开放空间 bbs.nwpu.dhs.org.[FROM: www-post@bbs.nwpu.dh]

BBS丝路花雨站纪念。。。