excel - VBA:如何将此 cmd 命令转换为 VBA Shell 代码
问题描述
我想在 VBA 代码中使用下面的命令行,命令是递归地获取所有 .pdf 和 .docx 文件并在 CMD 中运行得很好
dir /s *.pdf *.docx
现在在 VBA 中,具有以下内容,它以递归方式打印出所有文件,而不是指定的扩展名
Sub Run()
Debug.Print ShellRun("cmd.exe /c dir c:\ /s *.pdf *.docx")
End Sub
Public Function ShellRun(sCmd As String) As String
Dim oShell As Object
Set oShell = CreateObject("WScript.Shell")
Dim oExec As Object
Dim oOutput As Object
Set oExec = oShell.Exec(sCmd)
ShellRun = oExec.StdOut.ReadAll
End Function
解决方案
尝试这个
Debug.Print ShellRun("cmd.exe /c dir /s c:\*.pdf c:\*.docx")
编辑:以下评论
您需要dir /s PATH\*.pdf PATH\*.docx
在\*
. pdf和docx也PATH
应该重复。但是,在您的代码中,您有空格,并且您的位置c:\
和标志也/s
交换了。
推荐阅读
- javascript - 类型 'number' 不可分配给类型 'ReadonlyArray<{}>'
- ruby-on-rails - 如何使用 Ruby on Rails 5.2 编写复杂的连接查询?
- node.js - @azure/cosmos.js - 同步获取数据库
- javascript - 为什么传奇效应没有被调用?
- fortran - 如何创建具有给定度数分布的网络(图)?
- routes - 如何知道我的骆驼路线是否已启动并正在运行以处理来自队列的消息
- c# - 无法调用/加载 .NET Core 程序集,但 .NET Standard 在 PowerShell 中使用 Add-Type 可以正常工作
- perl - 循环遍历当前的哈希数组;构建一个新的哈希数组并忽略具有特定键 = 值的记录
- python - 如何在作为类对象的画布之间切换?
- php - 如何在 CakePHP 中创建一个使用 rest API 从数据库中检索数据的方法?