简体   繁体   English

使用 cmd.exe 调用 shell 与 Wscript.Shell

[英]Call shell vs Wscript.Shell using cmd.exe

I am using the following code and it is working fine.我正在使用以下代码,它工作正常。

path = "cd C:\Program Files\bin & AppX File.txt"

Call Shell("cmd.exe /k " & path, vbNormalFocus)

However, I have to use the waitOnreturn so I changed to the following code and it is not working anymore.但是,我必须使用waitOnreturn,因此我更改为以下代码,但它不再起作用。

path = "cd C:\Program Files\bin & AppX File.txt"
Dim wsh As Object
Dim waitOnReturn As Boolean: waitOnReturn = True
Dim windowStyle As Integer: windowStyle = 1
Set wsh = VBA.CreateObject("WScript.Shell")
wsh.Run ("cmd.exe /k " & path & ", windowStyle, waitOnReturn") 

The error is " couldn't read file "File.txt,": no such file or directory " but the file is there.错误是“无法读取文件“File.txt,”:没有这样的文件或目录“但文件在那里。 The Filename is File.txt .文件名是File.txt The program's name is AppX.该程序的名称是 AppX。 The program allows me to run the file using cmd.exe if I first change the direction to the AppX path.如果我首先将方向更改为 AppX 路径,该程序允许我使用 cmd.exe 运行该文件。 Then, to run the file using cmd.exe, I need to run "AppX File.txt" Is there something running with the WScript.Shell code然后,要使用 cmd.exe 运行文件,我需要运行“AppX File.txt”是否有使用 WScript.Shell 代码运行的东西

Because the file has spaces in its name, you need to enclose the path in quotation marks.因为文件名中有空格,所以需要用引号将路径括起来。 So something like:所以像:

path = "cd \"C:\Program Files\bin & AppX File.txt\""

I changed我变了

  wsh.Run ("cmd.exe /k " & path & ", windowStyle, waitOnReturn")

to

  wsh.Run "cmd.exe /k " & path, windowStyle, waitOnReturn

and it is working now它现在正在工作

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

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