我想检查文件是否存在,如果存在,我想打开它并阅读第1行,
如果文件不存在或者文件没有内容,那么我希望无声地失败而不让任何人知道发生了错误。
答案 0 :(得分:61)
从这开始:
Set fso = CreateObject("Scripting.FileSystemObject")
If (fso.FileExists(path)) Then
msg = path & " exists."
Else
msg = path & " doesn't exist."
End If
答案 1 :(得分:0)
对于想要观看VBS中存在的特定文件的人:
Function bIsFileDownloaded(strPath, timeout)
Dim FSO, fileIsDownloaded
set FSO = CreateObject("Scripting.FileSystemObject")
fileIsDownloaded = false
limit = DateAdd("s", timeout, Now)
Do While Now < limit
If FSO.FileExists(strPath) Then : fileIsDownloaded = True : Exit Do : End If
WScript.Sleep 1000
Loop
Set FSO = Nothing
bIsFileDownloaded = fileIsDownloaded
End Function
用法:
FileName = "C:\test.txt"
fileIsDownloaded = bIsFileDownloaded(FileName, 5) ' keep watching for 5 seconds
If fileIsDownloaded Then
WScript.Echo Now & " File is Downloaded: " & FileName
Else
WScript.Echo Now & " Timeout, file not found: " & FileName
End If
答案 2 :(得分:-3)
现有文件夹将使用FileExists
失败Function FileExists(strFileName)
' Check if a file exists - returns True or False
代替或另外使用:
Function FolderExists(strFolderPath)
' Check if a path exists