简体   繁体   English

Excel VBA 和 Windows 命令提示符

[英]Excel VBA and windows command prompt

If I type this into windows command prompt:如果我在 Windows 命令提示符中输入:

Find "Foo" "D:\test.txt" | ECHO %ERRORLEVEL%

I get 9009 <--- this is because it was not found我得到 9009 <--- 这是因为没有找到

If I type this into windows command prompt:如果我在 Windows 命令提示符中键入:

Find "End" "D:\test.txt" | ECHO %ERRORLEVEL%

I get 0 <--- this is because it was found我得到 0 <--- 这是因为它被发现

I want to send this command from Excel VBA to the Windows command prompt and receive the answer back.我想将此命令从 Excel VBA 发送到 Windows 命令提示符并收到回复。 Here is what I have tried and it isn't working but I feel like it is close:这是我尝试过的,但它不起作用,但我觉得它很接近:

Sub test()

    Dim cmdLine As Object
    Dim result As String

    Set cmdLine = CreateObject("WScript.Shell")
    result = cmdLine.Exec("%comspec% /C Find ""End of Report"" ""D:\test.csv"" | ECHO %ERRORLEVEL%").STDOut.ReadAll

    Debug.Print (result)

End Sub

First of all you might need to wait for wscript to finsh the execution of the command, so I would try something like this首先,您可能需要等待wscript完成命令的执行,所以我会尝试这样的操作

Sub test()

    Dim cmdLine As Object
    Dim result As Object

    Set cmdLine = CreateObject("WScript.Shell")

    Set result = cmdLine.Exec("%comspec% /C Find ""End of Report"" ""D:\test.csv"" | ECHO %ERRORLEVEL%")

    Do While result.Status = 0
        Application.Wait Now + TimeValue("00:00:01")
    Loop


    Debug.Print result.stderr.readall(), result.stdout.readall()

End Sub

In order to see that the command is really working you could remove ECHO %ERRORLEVEL% and you will get indeed stdout resp.为了查看该命令是否真的有效,您可以删除ECHO %ERRORLEVEL%并且您确实会得到stdout响应。 stderr . stderr

Sub testA()

    Dim cmdLine As Object
    Dim result As Object

    Set cmdLine = CreateObject("WScript.Shell")

    Set result = cmdLine.Exec("%comspec% /C Find ""End of Report"" ""D:\test.csv""")

    Do While result.Status = 0
        Application.Wait Now + TimeValue("00:00:01")
    Loop


    Debug.Print result.stderr.readall(), result.stdout.readall()

End Sub

Update : In case you only need to find a certain text in a file you could use the following approach更新:如果您只需要在文件中查找某个文本,您可以使用以下方法

Sub TestC()
    Debug.Print inFile("D:\Test.csv", "End of Report")
End Sub

Function inFile(fileName As String, searchText As String) As Boolean

    Dim fileContent As String
    Dim fileNo As Long
    fileNo = FreeFile
    Open fileName For Input As #fileNo
    fileContent = Input(LOF(fileNo), fileNo)
    Close #fileNo

    If InStr(1, fileContent, searchText, vbTextCompare) > 0 Then
        inFile = True
    Else
        inFile = False
    End If

End Function

Update 2 : Based on the comments the following approach might be a solution for your problem更新 2 :根据评论,以下方法可能是您问题的解决方案

Sub TestD()
    Dim fileName As String
    fileName = "D:\Test.csv"
    If Not IsFileOpen(fileName) Then
        Debug.Print inFile(fileName, "End of Report")
    End If
End Sub

Function inFile(fileName As String, searchText As String) As Boolean

    Dim fileContent As String
    Dim fileNo As Long
    fileNo = FreeFile
    Open fileName For Input As #fileNo
    fileContent = Input(LOF(fileNo), fileNo)
    Close #fileNo

    If InStr(1, fileContent, searchText, vbTextCompare) > 0 Then
        inFile = True
    Else
        inFile = False
    End If

End Function


Function IsFileOpen(fileName As String)
    Dim filenum As Integer, errnum As Integer

    On Error Resume Next   ' Turn error checking off.
    filenum = FreeFile()   ' Get a free file number.
    ' Attempt to open the file and lock it.
    Open fileName For Input Lock Read As #filenum
    Close filenum          ' Close the file.
    errnum = Err           ' Save the error number that occurred.
    On Error GoTo 0        ' Turn error checking back on.

    ' Check to see which error occurred.
    Select Case errnum

        ' No error occurred.
        ' File is NOT already open by another user.
        Case 0
         IsFileOpen = False

        ' Error number for "Permission Denied."
        ' File is already opened by another user.
        Case 70
            IsFileOpen = True

        ' Another error occurred.
        Case Else
            Error errnum
    End Select

End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM