开发者

executing two functions with wshshell

i have two different functions (copy and zip) to b executed. can i do it with with a single wshshell script.i tried----

Dim WshShell, oExec,g,h
h="D:\d"

g="xcopy " & h & " " & "D:\y\ /E & cmd /c cd D:\c & D: & winzip32.exe -min -a D:\a"
Set WshShell = CreateObject("WScript.Shell")

Set oExec = WshShell.Exec(g)

Do While oExec.Status = 0
     WScript.Sleep 100
Loop

WScript.Echo oExec.Status

it dint work.though separate programs i.e g="xcopy " & h & " " & "D:\y\ /E" and g="cmd /c cd D:\d & D: & winzip32.ex开发者_JAVA技巧e -min -a D:\a" works. i am sorry for the formatting problem. any help is appreciated.


I don't think you can make it work that way: the & operator is part of the command-line shell, not of the CreateProcess or ShellAPI frameworks.

I see several ways to work around this:

  1. call CMD.exe and pass your command-line as parameter. (i.e. "%ComSpec% /c " & Stuff)
  2. Write a batch on the fly and execute it.
  3. Just write two calls to WshShell.Exec instead of one. It's probably better since you can check the result of each individual part of the process anyway. You can even do the copy in script instead of calling xcopy for extra error checking and loging.

here is a sample code on how to copy a folder and then tzip another one in VBScript:

Dim WshShell, oExec, oFS, oF

szSourcePath="c:\tmp\testfolderSrc"
szDestinationPath="c:\tmp\testfolderDest"
szDestinationZip="c:\tmp\final.zip"
bOverwrite=true

' Create objects
Set oFS = CreateObject("Scripting.FileSystemObject")
Set WshShell = CreateObject("WScript.Shell")

' cleanup target folder
if  oFS.FolderExists(szDestinationPath) then
  oFS.DeleteFolder szDestinationPath, true
end if
' Create target folder
set oF = oFS.CreateFolder(szDestinationPath)


' Copy content of the source folder to the target folder
oFS.CopyFolder szSourcePath & "\*", szDestinationPath, bOverwrite

' delete old zip
if oFS.FileExists("c:\myzip.zip") then
  oFS.deleteFile("c:\myzip.zip")
end if

wshShell.CurrentDirectory = "c:\tmp\" ' set current directory to this
Set oExec = WshShell.Exec("c:\program files\winzip\winzip32.exe -min -a c:\myzip.zip" )
Do While oExec.Status = 0
  WScript.Sleep 100
Loop
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜