Vbscript 压缩(zip)无法创建输出文件-错误
我正在使用VBscript扫描文件夹,创建zip文件并向其中添加文件(压缩),但当我在包含大量文件的文件夹上运行脚本时,出现以下错误:“压缩(zip)无法创建输出文件” 我的邮政编码如下:Vbscript 压缩(zip)无法创建输出文件-错误,vbscript,zip,Vbscript,Zip,我正在使用VBscript扫描文件夹,创建zip文件并向其中添加文件(压缩),但当我在包含大量文件的文件夹上运行脚本时,出现以下错误:“压缩(zip)无法创建输出文件” 我的邮政编码如下: Dim objFSO Set objFSO= CreateObject("Scripting.FileSystemObject" Function PreformZip(objFile,target,zip_name, number_of_file) Set shell = CreateObject(
Dim objFSO
Set objFSO= CreateObject("Scripting.FileSystemObject"
Function PreformZip(objFile,target,zip_name, number_of_file)
Set shell = CreateObject("WScript.Shell")
zip_target = target + "\" + zip_name +".zip"
If Not objFSO.FileExists(zip_target) Then
MakePathIfNotExist(target)
NewZip(zip_target)
Else
If number_of_file=0 Then
objFSO.DeleteFile(zip_target)
NewZip(zip_target)
End if
End If
Set zipApp = CreateObject("Shell.Application")
aSourceName = Split(objFile, "\")
sSourceName = (aSourceName(Ubound(aSourceName)))
zip_file_count = zipApp.NameSpace(zip_target).items.Count
zipApp.NameSpace(zip_target).Copyhere objFile, 16
On Error Resume Next
sLoop = 0
Do Until zip_file_count < zipApp.NameSpace(zip_target).Items.Count
Wscript.Sleep(100)
sLoop = sLoop + 1
Loop
On Error GoTo 0
End Function
Sub NewZip(zip)
Set new_zip = objFSO.CreateTextFile(zip)
new_zip.Write Chr(80) & Chr(75) & Chr(5) & Chr(6) & String(18, 0)
new_zip.Close
Set new_zip = Nothing
WScript.Sleep(5000)
End Sub
Function MakePathIfNotExist(DirPath)
Dim FSO, aDirectories, sCreateDirectory, iDirectory
Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(DirPath) Then
Exit Function
End If
aDirectories = Split(DirPath, "\")
sCreateDirectory = aDirectories(0)
For iDirectory = 1 To UBound(aDirectories)
sCreateDirectory = sCreateDirectory & "\" & aDirectories(iDirectory)
If Not FSO.FolderExists(sCreateDirectory) Then
FSO.CreateFolder(sCreateDirectory)
End If
Next
End Function
Function Recursion(DirectoryPath)
Dim FSO : Set FSO = CreateObject("Scripting.FileSystemObject")
If FSO.FolderExists(DirectoryPath) Then Exit Function
Call Recursion(FSO.GetParentFolderName(DirectoryPath))
FSO.CreateFolder(DirectoryPath)
End Function
Dim objFSO
设置objFSO=CreateObject(“Scripting.FileSystemObject”
函数PreformZip(objFile、target、zip\u名称、\u文件的\u编号)
Set shell=CreateObject(“WScript.shell”)
zip\u target=target+“\”+zip\u name+“.zip”
如果不存在objFSO.FileExists(zip_目标),则
MakePathIfNotExist(目标)
新邮政编码(邮政编码目标)
其他的
如果_文件的编号为0,则
objFSO.DeleteFile(zip_目标)
新邮政编码(邮政编码目标)
如果结束
如果结束
Set zipApp=CreateObject(“Shell.Application”)
aSourceName=Split(objFile,“\”)
sSourceName=(aSourceName(Ubound(aSourceName)))
zip_file_count=zipApp.NameSpace(zip_target).items.count
NameSpace(zip_target).Copyhere objFile,16
出错时继续下一步
单桅帆船=0
直到zip_file_count
我最初认为我在创建zip后等待的时间不够长,但我甚至在每次zip后都尝试了10秒等待,但仍然会出现相同的错误
我怎样才能解决它
如果没有解决方案,是否有其他方法制作zip?脚本不仅供我自己使用,因此我不希望ro中继到需要安装的软件上?尽管
文件夹。CopyHere
方法不会返回值,也不会通知调用程序复制已完成,但是您可以等待下一个代码片段,我希望您可以在脚本中看到正确的(重新)位置:
On Error GoTo 0
zipApp.NameSpace(zip_target).Copyhere objFile _
, 4 +8 +16 +256 +512 +1024
Wscript.Sleep( 100)
On Error GoTo 0
注意:无需等待。Do..Loop
,此Wscript.Sleep(100)
足以压缩小文件或在出现大文件时启动进度对话框-脚本将等待它
注意:下一次错误恢复时没有。如果不处理错误,请避免调用下一次错误恢复时的
使用的标志如下
Const FOF_SILENT = &h0004 'ineffective?
Const FOF_RENAMEONCOLLISION = &h0008 'ineffective?
Const FOF_NOCONFIRMATION = &h0010 '
Const FOF_SIMPLEPROGRESS = &h0100 'ineffective?
Const FOF_NOCONFIRMMKDIR = &h0200 '
Const FOF_NOERRORUI = &h0400 '
不幸的是,在某些情况下,例如压缩(.zip
)文件,一些选项标志可能会被设计忽略
如果FOF_SILENT
标志无效,则用户可以取消压缩过程
如果FOF_RENAMEONCOLLISION
标志无效,则相同名称的较新文件将不会压缩,现有压缩文件将保留以前的版本而不小心;只有现有文件夹会显示额外的错误消息
这些问题也可以解决,但这是另一个问题……好吧,经过大量研究,我发现在使用shell执行zip时,没有可能解决这个问题
我通过以下方式使用za7.exe(7-zip)解决了这个问题:
Dim zipParams
zipParams = "a -tzip"
Dim objShell: Set objShell = CreateObject("WScript.Shell")
command = zip_exe_location + " " + zipParams + " " + zip_target + " " + SourceFile
objShell.Run Command, 0 ,true
zip参数中的“a”表示“添加到文件”,并且-tzip将文件类型设置为zip。请制作代码示例,以便我们可以重现问题。当我删除对未定义函数的调用MakePathIfNotExist()时,代码对我有效
并添加了缺少的objFSO
定义和对PreformZip()
的实际调用。我已经添加了缺少的函数和定义。正如我所说,代码是有效的,但只有当文件夹和子文件夹有几个文件时,如果我需要制作很多zip文件,我就会得到错误。我认为zipApp.NameSpace(zip_目标)。复制此处
不会等待复制过程完成。睡眠
可能需要更长时间。可能还有其他方法。什么是“大量文件”可能需要超过10秒的等待时间,但是等待超过10秒的文件不是一个可靠的解决方案,它花费了太多的时间。同意。你应该考虑使用另一个zip解决方案,比如7zip,首先感谢回放,我不明白为什么你说你的脚本会在我压缩大文件的时候等待,就像我一样。请看,我将启动压缩过程,100毫秒后,即使传输未完成,代码也将继续。其次,它如何解决文件未复制到zip的问题?似乎另一个标志与问题无关,并且不太可能是循环导致的。这对我来说是任意的组合:压缩文件,例如aa=PreformZip(“C:\Temp\debug.txt”、“C:\Temp”、“mof”,1)
或压缩文件夹,例如aa=PreformZip(“C:\rsit\”、“C:\Temp”、“mof”,1)
;现在我绕过了FOF_RENAMEONCOLLISION
标志无效性…但请您放心,听从Matt的建议-7-zip中有命令行版本。。。