使用VBA从Zip删除一些特定文件 [英] Deleting some specific files from a Zip using VBA
问题描述
在一个完整的宏过程中,我正在创建一个 Folder
的 Zip
文件.该文件夹包含多个子文件夹和文件.使用此代码:
During a complete macro process I am creating a Zip
file of a Folder
. That folder have multiple sub-folders and files. Using This code:
Dim oApp As Object
NewZip (s_path & "\" & acc_name & ".zip")
Set oApp = CreateObject("Shell.Application")
oApp.Namespace(s_path & "\" & acc_name & ".zip").CopyHere oApp.Namespace(s_path & "\" & acc_name & "\").items
On Error Resume Next
Do Until oApp.Namespace(s_path & "\" & acc_name & ".zip").items.Count = _
oApp.Namespace(s_path & "\" & acc_name & "\").items.Count
Application.Wait (Now + TimeValue("0:00:01"))
Loop
On Error GoTo 0
Set oApp = Nothing
现在,我需要检查邮政编码是否小于 20mb ,以便可以通过邮件发送.我发现可以使用以下行来完成:
Now What i need to is to check that the Zip is less than 20mb, so that it can be sent via mail. Which I found can be done using line:
FileLen(path)
现在,如果文件大小超过 20mb ,我想从该邮编的一个特定子文件夹中删除所有文件.我不知道该怎么做.我是否应该像原始文件那样创建另一个zip文件,然后尝试跳过该子文件夹中的文件,或者是否可以通过某种方式删除Zip文件中的特定文件?
Now if the file size exceeds 20mb, i want to delete all the files from one specific subfolder of that Zip. I don't have any idea how to do that. Should I just create another zip like the original and try skipping files in that subfolder or there is some way to delete specific files in a Zip ?
我正在尝试使用以下方法查看Zip内部的内容:
I was trying to look inside the Zip using:
Dim FSO As Object
Dim sh As Object, fld As Object, n As Object
Set FSO = CreateObject("Scripting.FileSystemObject")
Set sh = CreateObject("Shell.Application")
Set ZipFile = sh.Namespace("C:\Users\mohit.bansal\Desktop\Test\Test.zip")
For Each fileInZip In ZipFile.Items
Debug.Print (fileInZip)
Next
仍然无法进入Zip的子文件夹.
Still not able to get inside the Subfolders of the Zip.
推荐答案
要从zip文件中删除文件,请尝试此操作.我正在演示如何删除一个文件.随时对其进行修改以满足您的需求
To delete a file from a zip file, try this. I am demonstrating on how to delete one file. Feel free to amend it to suit your needs
逻辑:
- 使用
.MoveHere
将文件移动到用户的临时目录.这将从zip文件中删除该文件 - 从临时目录中删除文件
- Use
.MoveHere
to move the file to user's temp directory. This will remove the file from the zip file - Delete the file from the temp directory
代码:(经过测试)
Option Explicit
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" _
(ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long
Private Const MAX_PATH As Long = 260
Sub Sample()
Dim zipFile, oShellApp, fileToDelete, fl
zipFile = "C:\Users\routs\Desktop\Desktop.zip"
fileToDelete = "Tester.xlsm"
Set oShellApp = CreateObject("Shell.Application")
For Each fl In oShellApp.Namespace(zipFile).Items
If fl.Name = fileToDelete Then
oShellApp.Namespace(TempPath).MoveHere (fl)
End If
Next fl
Kill TempPath & fileToDelete
End Sub
'~~> Function to get the user's temp path
Function TempPath() As Variant
TempPath = String$(MAX_PATH, Chr$(0))
GetTempPath MAX_PATH, TempPath
TempPath = Replace(TempPath, Chr$(0), "")
End Function
替代
- 将所有相关文件添加到zip
- 之后,请循环检查文件大小,如果在可接受的范围内,请一一添加可选文件.
这篇关于使用VBA从Zip删除一些特定文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!