2

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

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
4

2 回答 2

3

如有疑问,请阅读文档

retVal = Shell.NameSpace(
  vDir
)

参数

vDir [输入]

     类型:变体

     要为其创建Folder对象的文件夹。这可以是指定文件夹路径的字符串或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
于 2013-10-10T20:02:55.270 回答
1

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]
于 2013-10-10T19:54:45.773 回答