我使用VBscript扫描文件夹、创建压缩文件并向它们添加文件(压缩),但是当我在包含大量文件的文件夹上运行脚本时,我会得到以下错误:“压缩(压缩)不能创建输出文件”
我的邮政编码如下:
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我最初认为我在创建zip之后等待的时间不够长,但是我甚至在每次压缩之后用10秒等待它,但我仍然得到了相同的错误。
我该怎么解决呢?
如果没有解决办法,是否有其他办法使拉链?这个脚本不仅是供我自己使用的,所以我不想在需要安装的软件上安装ro继电器?
发布于 2014-10-13 13:59:53
经过大量的研究,我发现在使用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"表示"add“,-tzip将文件类型设置为zip。
发布于 2014-10-07 16:11:14
虽然Folder.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)足够压缩小文件或启动进度对话框,以防出现巨大的文件-而您的脚本将等待它.
注意:没有'On Error Resume Next。如果不处理错误,请避免调用On Error Resume Next .
使用的标志如下。
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)文件,一些选项标志可能被设计(sic!)忽略。由MSDN(http://msdn.microsoft.com/en-us/library/windows/desktop/bb787866(v=vs.85%29.aspx)!
如果FOF_SILENT标志无效,那么用户可以取消压缩过程.
如果FOF_RENAMEONCOLLISION标志无效,那么新的同名文件就不会被压缩,现有的zip文件将保持以前的版本而不警告;只有现有的文件夹才会产生额外的错误消息.
这些也可以修复,但这是另一个问题.
https://stackoverflow.com/questions/26221018
复制相似问题