tags:

views:

94

answers:

1

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.exe -min -a D:\a" works. i am sorry for the formatting problem. any help is appreciated.

A: 

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
Stephane
hey, thanks for the reply.but the problem is that i am a beginner and not familiar with wshshell coding. i got this code from a site, but i dont know much to play with it. could you please elaborate it or may be give me few lines of code.
sushant
I added some sample code. Hard to say what you really need, though. I suggest you grab one of the many VBScript books around and lookup MS's scripting site
Stephane
thanks a lot for ur help. i appreciate u making such effort for me. i will try this code
sushant