【问题标题】:Excel VBA and windows command promptExcel VBA 和 windows 命令提示符
【发布时间】:2020-03-11 16:58:40
【问题描述】:

如果我在 Windows 命令提示符中输入:

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

我得到 9009

如果我在 Windows 命令提示符中输入:

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

我得到 0

我想将此命令从 Excel VBA 发送到 Windows 命令提示符并收到回复。这是我尝试过的方法,但它不起作用,但我觉得它很接近:

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

【问题讨论】:

  • 上面的代码始终返回 0
  • 有更好的发帖地方吗?
  • 试试Dim result As VariantSTDOut.ReadAll()?
  • 我无法确认在Find "Foo" "D:\test.txt" | ECHO %ERRORLEVEL% 的情况下获得9009。对于Find "Foo" "D:\test.txt" | ECHO %ERRORLEVEL%Find "End" "D:\test.txt" | ECHO %ERRORLEVEL%,我总是得到0
  • 我不是批处理编程方面的专家,我只是说Find 命令在两种情况下都会在我的计算机上返回0,而且它似乎也在您的计算机上。

标签: excel vba find errorlevel wscript.shell


【解决方案1】:

首先你可能需要等待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

为了查看该命令是否真正有效,您可以删除ECHO %ERRORLEVEL%,您将得到真正的stdoutstderr.

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

更新:如果您只需要在文件中查找特定文本,您可以使用以下方法

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

更新 2:基于 cmets,以下方法可能是您的问题的解决方案

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

【讨论】:

  • 我复制了第一块代码并粘贴了它......没有修改......但它再次返回 0......我将“报告结束”更改为“你好”,它不应该找到...但它仍然返回 0
  • 它只返回一个 0...如果这意味着什么...
  • 是的,正如我在上面的评论中所说,errorlevel 似乎总是0。如果您尝试帖子中的第二个代码,您会发现您确实会得到stdout。所以看来ind ""End of Report"" ""D:\test.csv"" | ECHO %ERRORLEVEL%是麻烦制造者。
  • 我很困惑...我基本上是在尝试获得真/假或 1/0...
  • 是的,这是正确的,但它返回标准输出,对吗?如果您只需要在文件中搜索某个字符串,我将使用不同的方法更新帖子。
猜你喜欢
  • 2013-07-31
  • 1970-01-01
  • 2014-08-21
  • 2018-01-04
  • 1970-01-01
  • 2012-03-15
  • 2022-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多