When I execute the following code it works, but I need to wait for the code to complete; before I can execute the rest of my program. How do I do this?
当我执行以下代码时,它可以工作,但是我需要等待代码完成;在我执行剩下的程序之前。我该怎么做呢?
Works, but doesn't wait:
工作,但并不等:
Set oShell = CreateObject("Wscript.Shell")
oShell.Run "sftp2.exe -B " & strOutputFile
Doesn't Work:
不工作:
Set oShell = CreateObject("Wscript.Shell")
Return = oShell.Run ("sftp2.exe -B " & strOutputFile, 1, true)
Works, but doesn't wait:
工作,但并不等:
Set oShell = CreateObject("Wscript.Shell")
Set oExec = oShell.Exec ("sftp2.exe -B " & strOutputFile)
2 个解决方案
#1
1
In your third example, you can poll the WshScriptExec
object's status until the process has completed:
在第三个示例中,您可以轮询WshScriptExec对象的状态,直到流程完成:
Set oShell = CreateObject("Wscript.Shell")
Set oExec = oShell.Exec ("sftp2.exe -B " & strOutputFile)
Do While oExec.Status = 0
WScript.Sleep 100
Loop
' as you were...
although your examples look much like they're taken from msdn so I'm not sure why they don't work. Perhaps it's the way your program sftp2.exe
operates which is causing the issue? If it's anything like the usual ftp
application, it will run as a kind-of command prompt which may not be compatible. I'm just speculating.
虽然你的例子看起来很像来自msdn所以我不确定为什么它们不起作用。也许这就是你的程序sftp2的方式。exe操作导致了问题?如果它与通常的ftp应用程序类似,那么它将作为一种命令提示符运行,这可能不兼容。我只是猜测。
#2
1
Thanks again for the help. I figured out why it was not working properly. Absolute path vs relative path. I was using relative path and this was causing the below code to fail, since it wasn't sure were to find the files to sftp. To my suprize absolute path is not necessary in the other two examples above that worked.
再次感谢您的帮助。我弄明白了为什么它不能正常工作。绝对路径与相对路径。我使用的是相对路径,这导致下面的代码失败,因为它不确定要查找到sftp的文件。在我看来,绝对路径在上面另外两个成功的例子中是不必要的。
This works with absolute path (ie. put "C:\somefolder\file";
a command in my "strOutputFile"
file).
这适用于绝对路径。把“C:\ somefolder \文件”;我的“strOutputFile”文件中的命令)。
Set oShell = CreateObject("Wscript.Shell")
Return = oShell.Run ("sftp2.exe -B " & strOutputFile, 1, true)
#1
1
In your third example, you can poll the WshScriptExec
object's status until the process has completed:
在第三个示例中,您可以轮询WshScriptExec对象的状态,直到流程完成:
Set oShell = CreateObject("Wscript.Shell")
Set oExec = oShell.Exec ("sftp2.exe -B " & strOutputFile)
Do While oExec.Status = 0
WScript.Sleep 100
Loop
' as you were...
although your examples look much like they're taken from msdn so I'm not sure why they don't work. Perhaps it's the way your program sftp2.exe
operates which is causing the issue? If it's anything like the usual ftp
application, it will run as a kind-of command prompt which may not be compatible. I'm just speculating.
虽然你的例子看起来很像来自msdn所以我不确定为什么它们不起作用。也许这就是你的程序sftp2的方式。exe操作导致了问题?如果它与通常的ftp应用程序类似,那么它将作为一种命令提示符运行,这可能不兼容。我只是猜测。
#2
1
Thanks again for the help. I figured out why it was not working properly. Absolute path vs relative path. I was using relative path and this was causing the below code to fail, since it wasn't sure were to find the files to sftp. To my suprize absolute path is not necessary in the other two examples above that worked.
再次感谢您的帮助。我弄明白了为什么它不能正常工作。绝对路径与相对路径。我使用的是相对路径,这导致下面的代码失败,因为它不确定要查找到sftp的文件。在我看来,绝对路径在上面另外两个成功的例子中是不必要的。
This works with absolute path (ie. put "C:\somefolder\file";
a command in my "strOutputFile"
file).
这适用于绝对路径。把“C:\ somefolder \文件”;我的“strOutputFile”文件中的命令)。
Set oShell = CreateObject("Wscript.Shell")
Return = oShell.Run ("sftp2.exe -B " & strOutputFile, 1, true)