判断文件是否存在,可以使用下面的函数:Public Function FileExists%(FileName$)Dim f% ' Trap any errors that may occur On Error Resume Next ' Get a free file handle to avoid using a file handle already in use f% = FreeFile ' Open the file for reading Open FileName$ For Input As #f% ' Close it Close #f% ' If there was an error, Err will be <> 0. In that case, we return False FileExists% = Not (Err <> 0)End Function使用: if Function FileExists("c:\abc.txt")<>0 then msgbox "文件存在" endif
On Error Resume Next ' Get a free file handle to avoid using a file handle already in use
f% = FreeFile
' Open the file for reading
Open FileName$ For Input As #f%
' Close it
Close #f%
' If there was an error, Err will be <> 0. In that case, we return False
FileExists% = Not (Err <> 0)End Function使用:
if Function FileExists("c:\abc.txt")<>0 then
msgbox "文件存在"
endif