VBA使用记事本检查文件是否存在

时间:2015-03-10 02:32:22

标签: excel vba

我想询问社区是否有人可以将文件目录路径存储到记事本或其他word文档中,让VBA扫描每个文件目录并检查文件是否存在。

1 个答案:

答案 0 :(得分:0)

以下是使用dir来说明上述评论的评论代码。

'This function will check if the file or the directory
'exists and returns a boolean.
Function ExistTest(Path As String) As Boolean
'The function default return is set to false
ExistTest = False
'Remove stream reader appending
Path = Replace(Path, "", "")
'Dir returns a 0-length string if the file or directory doesn't exist
If Len(Dir(Path)) > 0 Or Len(Dir(Path, vbDirectory)) > 0 Then
ExistTest = True
End If
End Function

Sub main()
'Defining the source file
Dim SourceFile As String: SourceFile = "d:/t.txt"
'Open a stream reader
Open SourceFile For Input As #1
'Read all line until the end of file
'Before we define a temporary string the path as we go through
'the sourcefile
Dim TempPath As String
Do Until EOF(1)
Line Input #1, TempPath
'Debug print the results
Debug.Print (TempPath & " " & ExistTest(TempPath))
Loop
Close #1
End Sub

Debug.Print将在即时窗口中打印。要启用它,请转到查看>立即窗口