创建Zip然后将文件夹复制到它

时间:2013-10-10 17:43:16

标签: vbscript zip

我正在尝试创建一个zip文件,然后将三个文件夹复制到其中。我在第33行char 1上得到错误,需要错误状态对象,我搜索并用Google搜索但似乎无法理解我正在阅读或理解我真正需要搜索的内容。无论如何,这是我的代码。

Option Explicit
Dim objFSO, objFolder1, objFolder2, objFolder3, FolderToZip, ziptoFile, FolderGroup
Dim ShellApp, eFile, oNewZip, strZipHeader
Dim ZipName, Folder, i, Zip, Item
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder1 = objFSO.GetFolder("C:\Windows\Temp\SMSTSLog")
Set objFolder2 = objFSO.GetFolder ("C:\Windows\System32\CCM\Logs")
Set objFolder3 = objFSO.GetFolder ("C:\Windows\SysWOW64\CCM\Logs")
'For Each efile In objFolder.Files
'  If DateDiff("d",eFile.DateLastModified,Now) >= 2 Then
'    objFSO.MoveFile eFile, "C:\Documents and Settings\User\Desktop\Test2\"
'  End If
'Next
Wscript.Sleep 2000
Set oNewZip = objFSO.OpenTextFile("C:\win7tools\testing script.zip", 8, True)
strZipHeader = "PK" & Chr(5) & Chr(6)
For i = 0 To 17
  strZipHeader = strZipHeader & Chr(0)
Next
oNewZip.Write strZipHeader
oNewZip.Close
Set oNewZip = Nothing
WScript.Sleep 5000
FolderGroup = Array(objFolder1,objFolder2,objFolder3)

FolderToZip = "FolderGroup"
ZipToFile = "C:\Win7tools\Test Script.zip"
Set ShellApp = CreateObject("Shell.Application")
Set Zip = ShellApp.NameSpace(ZipToFile)
'Set Folder = ShellApp.NameSpace(FolderToZip)
ShellApp.NameSpace(FolderGroup).CopyHere Zip.NameSpace(ZipToFile)
WScript.Sleep 10000

set ShellApp = Nothing
set FolderToZip = Nothing
set ZipToFile = Nothing

2 个答案:

答案 0 :(得分:2)

如有疑问,请阅读documentation

retVal = Shell.NameSpace(
  vDir
)
     

<强>参数

     

vDir [in]

     

输入:变体

     

要为其创建文件夹对象的文件夹。这可以是一个字符串,用于指定文件夹的路径或 ShellSpecialFolderConstants 值之一。请注意, ShellSpecialFolderConstants 中的常量名称在Visual Basic中可用,但在VBScript或JScript中不可用。在这些情况下,必须在其位置使用数值。

NameSpace方法要求带有路径的字符串或ShellSpecialFolderConstants之一的整数值,而不是Folder个对象的数组。你的订单也错了。您调用copyHere方法的对象是zip文件。参数是你要复制到zip文件的内容(路径字符串应该在这里做得很好)。另外,您创建的zip文件的名称与尝试添加文件夹的zip文件的名称不同。

将您的代码更改为:

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

答案 1 :(得分:0)

WinZip有一个命令行界面。您可能需要下载并安装它,具体取决于您的版本:http://www.winzip.com/prodpagecl.htm

以下是一个适用于WinZip 9.0版的测试脚本,如果有帮助的话。

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

命令格式为:

winzip [action] [options] [Zip Path] [Path to file/folder to zip]