Creating a Zip then copying folders to it

后端 未结 2 576
我在风中等你
我在风中等你 2021-01-21 16:11

I\'m trying to create a zip file, then copy three folders into it. I get the error on line 33 char 1, error state object required, I have searched and googled but just can\'t se

2条回答
  •  情话喂你
    2021-01-21 16:47

    When in doubt, read the documentation:

    retVal = Shell.NameSpace(
      vDir
    )
    

    Parameters

    vDir [in]

         Type: Variant

         The folder for which to create the Folder object. This can be a string that specifies the path of the folder or one of the ShellSpecialFolderConstants values. Note that the constant names found in ShellSpecialFolderConstants are available in Visual Basic, but not in VBScript or JScript. In those cases, the numeric values must be used in their place.

    The NameSpace method expects either a string with a path or the integer value of one of the ShellSpecialFolderConstants, not an array of Folder objects. Also you got the order wrong. The object on which you call the copyHere method is the zip file. The argument is what you want to copy to the zip file (a path string should do just fine here). Plus, the name of the zip file you create is different from the name of the zip file you try to add the folders to.

    Change your code to this:

    folder1 = "C:\Windows\Temp\SMSTSLog"
    folder2 = "C:\Windows\System32\CCM\Logs"
    folder3 = "C:\Windows\SysWOW64\CCM\Logs"
    zipfile = "C:\Win7tools\Test Script.zip"
    
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    objFSO.OpenTextFile(zipfile, 2, True).Write "PK" & Chr(5) & Chr(6) _
      & String(18, Chr(0))
    
    Set ShellApp = CreateObject("Shell.Application")
    Set zip = ShellApp.NameSpace(zipfile)
    zip.CopyHere folder1
    zip.CopyHere folder2
    zip.CopyHere folder3
    
    WScript.Sleep 10000
    

提交回复
热议问题