Vbscript 使用wshshell执行两个函数

Vbscript 使用wshshell执行两个函数,vbscript,Vbscript,我执行了两个不同的函数(copy和zip)。我可以用一个wshshell脚本来完成吗?我试过了---- 虽然单独的程序,例如g=“xcopy”&h&“D:\y\/e”和g=“cmd/c cd D:\D&D:&winzip32.exe-min-a D:\a”可以工作,但它不能工作。 很抱歉出现了格式问题。非常感谢您提供的任何帮助。我认为您无法让它以这种方式工作:&operator是命令行shell的一部分,而不是CreateProcess或ShellAPI框架的一部分 我认为有几种方法可以解决这个

我执行了两个不同的函数(copy和zip)。我可以用一个wshshell脚本来完成吗?我试过了----

虽然单独的程序,例如g=“xcopy”&h&“D:\y\/e”和g=“cmd/c cd D:\D&D:&winzip32.exe-min-a D:\a”可以工作,但它不能工作。
很抱歉出现了格式问题。非常感谢您提供的任何帮助。

我认为您无法让它以这种方式工作:&operator是命令行shell的一部分,而不是CreateProcess或ShellAPI框架的一部分

我认为有几种方法可以解决这个问题:

  • 调用CMD.exe并将命令行作为参数传递。(即%ComSpec%/c“&Stuff)
  • 动态编写批处理并执行它
  • 只需编写两个对WshShell.Exec的调用,而不是一个。这可能更好,因为您可以检查过程中每个单独部分的结果。您甚至可以在脚本中进行复制,而不用调用xcopy进行额外的错误检查和登录
  • 下面是一个示例代码,介绍如何在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
    

    是否绝对有必要在vbscript中执行此操作?为什么不从批处理中运行它呢?考虑到您仍然有问题,这会更容易。实际上,代码是工具的一部分。这个工具是用vbscript和asp开发的。嘿,谢谢你的回复。但问题是我是个初学者,不熟悉wshshell编码。我从一个网站上获得了这段代码,但我不知道该如何使用它。请你详细说明一下,或者给我几行代码。我添加了一些示例代码。但很难说你真正需要什么。我建议你从周围众多的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