Creating a Zip then copying folders to it

后端 未结 2 575
我在风中等你
我在风中等你 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
    
    0 讨论(0)
  • 2021-01-21 16:53

    WinZip has a Command Line Interface. You might have to download and install it depending on your version: http://www.winzip.com/prodpagecl.htm

    The below is a test script that works for WinZip version 9.0 if it helps.

    Const WinZip = "C:\Program Files\WinZip9.0\wzzip.exe"  'WinZip Version 9.0
    
    BasePath = "C:\Path\To\Folders\"
    
    strZipFilePath = BasePath & "Test.zip"
    strArchiveMe = BasePath & "Folder_A"
    
    Set objShell = CreateObject("WScript.Shell")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    
    If Not objFSO.FileExists(WinZip) Then
        MsgBox "WinZip (wzzip.exe) Does Not Exist"
        WScript.Quit
    End If
    
    '''// For Below Command - Change "-a" TO "-mu" To Auto Delete The file After Zip Is Created
    '''// For Below Command - Change "-yb" TO "-ybc" To Answer YES To all Promps and not Terminate Operation
    strcommand = Chr(34) & WinZip & Chr(34) & " -a -yb " & Chr(34) & strZipFilePath & Chr(34) & " " & Chr(34) & strArchiveMe & Chr(34)
    objShell.Run strcommand, 1, True
    

    The command format is:

    winzip [action] [options] [Zip Path] [Path to file/folder to zip]
    
    0 讨论(0)
提交回复
热议问题