问题描述
我正在尝试创建一个 zip 文件,然后将三个文件夹复制到其中.我在第 33 行第 1 行出现错误,需要错误状态对象,我已经搜索过并用谷歌搜索过,但似乎既无法理解我正在阅读的内容,也无法理解我真正需要搜索的内容.无论如何,这是我的代码.
选项显式Dim objFSO、objFolder1、objFolder2、objFolder3、FolderToZip、ziptoFile、FolderGroupDim ShellApp、eFile、oNewZip、strZipHeaderDim ZipName、文件夹、i、Zip、项目Set objFSO = CreateObject("Scripting.FileSystemObject")Set objFolder1 = objFSO.GetFolder("C:\Windows\Temp\SMSTSLog")设置 objFolder2 = objFSO.GetFolder ("C:\Windows\System32\CCM\Logs")设置 objFolder3 = objFSO.GetFolder ("C:\Windows\SysWOW64\CCM\Logs")'对于 objFolder.Files 中的每个电子文件' 如果 DateDiff("d",eFile.DateLastModified,Now) >= 2 那么' objFSO.MoveFile eFile, "C:\Documents and Settings\User\Desktop\Test2\"' 万一'下一个Wscript.Sleep 2000Set oNewZip = objFSO.OpenTextFile("C:\win7tools\testing script.zip", 8, True)strZipHeader = "PK" &Chr(5) &铬(6)对于 i = 0 到 17strZipHeader = strZipHeader &铬(0)下一个oNewZip.Write strZipHeaderoNewZip.Close设置 oNewZip = 无WScript.Sleep 5000FolderGroup = Array(objFolder1,objFolder2,objFolder3)FolderToZip = "文件夹组"ZipToFile = "C:\Win7tools\Test Script.zip"Set ShellApp = CreateObject("Shell.Application")设置 Zip = ShellApp.NameSpace(ZipToFile)'设置文件夹 = ShellApp.NameSpace(FolderToZip)ShellApp.NameSpace(FolderGroup).CopyHere Zip.NameSpace(ZipToFile)WScript.Sleep 10000设置 ShellApp = 无设置 FolderToZip = 无设置 ZipToFile = 无
如有疑问,请阅读 文档:
retVal = Shell.NameSpace(虚拟目录)
参数
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) &铬(6)_&字符串(18, Chr(0))Set ShellApp = CreateObject("Shell.Application")设置 zip = ShellApp.NameSpace(zipfile)zip.CopyHere folder1zip.CopyHere folder2zip.CopyHere folder3WScript.Sleep 10000
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 seem to either understand what I'm reading or understand what I really need to search for. Anyhow, here is my code.
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
When in doubt, read the documentation:
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
这篇关于创建一个 Zip 然后将文件夹复制到它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!