Two tasks in Vb Script overlapping?
I am new to VB Script, but since it is straight forward, so I grabbed two fragments of code from the web and combine them into one in order to do my task.
Basically what I want is to run an application called "MapForce.exe" and produce an output file, then create a new directory and copy this file to this new directory too.
The code is straightforward:
'This is the line to call MapForce.exe and produce output file.
createObject("wscript.shell").exec "C:\Program Files\Altova\MapForce2011\MapForce.exe 834toASCII.mfd /BUILTIN /LOG ACS.log"
'These are the lines to copy that output file to the new folder:
sourceDir = "C:\Documents and Settings\Robert\test\result.txt"
destinationDir = "C:\Documents and Settings\Robert\test\"
const OverwriteExisting = True
strDirectory = destinationDir & replace(Month(date),"/","_") & " TOU"
Set fso = CreateObject("Scripting.FileSystemObject")
if not fso.FolderExists(strDirectory) then
Set objFolder = fso.CreateFolder(strDirectory)
end if
fso.CopyFile sourceDir & "*.*", strDirectory & "\", OverwriteExisting
Now it works: Mapforce.exe gets run, output generated, new folder created and a file copied to the new folder. But the question is, since the MapForce will take a longer time to finish and produce the newest output file, so line 1 takes longer time to finish, however, the remaining lines of doing copying task don't wait for it to finish, so the file gets copied in the new folder is always the old one, not the newest one generated by the application.
To put it in another way, the remaining lines hurry to finish the task without caring whether the first line (which runs the Ma开发者_JAVA技巧pForce application) finishes or not.
So I wonder if experts could give me advice on how to force the remaining lines to wait for the first line gets finished and newest output gets generated?
You could use the WshShell.Run method.
- The first parameter (REQUIRED) is the executable (including parameters).
- The second parameter (OPTIONAL) specifies the window style.
- The third parameter (OPTIONAL) specifies whether the scripts waits for the command to complete before continuing.
A sample:
Dim oShell
Set oShell = WScript.CreateObject("WScript.Shell")
returnCode = oShell.Run("Your.exe Param1 Param2", 1, True)
精彩评论