提取Zip文件。如何使用Zip的部分文件名来执行此操作?

时间:2018-06-12 16:32:54

标签: excel vba excel-vba

所以这就是场景。每天都会创建一个名为" Bundle_06112018063917"的Zip文件。 (数字是创建zip的日期和时间,因此它们每天都会更改)。 下面的代码将所有文件精美地提取到一个单独的文件夹中!

Sub UnzipAFile(zippedFileFullName As Variant, unzipToPath As Variant)
    Dim ShellApp As Object
        'Copy the files & folders from the zip into a folder
    Set ShellApp = CreateObject("Shell.Application")
    ShellApp.Namespace(unzipToPath).CopyHere 
    ShellApp.Namespace(zippedFileFullName).items
End Sub

Sub Dump()
    Call UnzipAFile("G:\DP\Mstanley\history\JUN18\WESTROCK\Bundle_06112018063917.zip", _
    "G:\DP\Mstanley\history\JUN18\WESTROCK\Dump")
End Sub

问题: zip文件的名称每天根据创建zip的日期和时间而变化。因此我需要一种方法来引用只有" Bundle _"的zip文件。

以下是我尝试的内容,但仍然没有运气。

Sub doingstuff()
    Dim pSTR As String
    Dim strFile As String
    Dim WB As Workbook
    Dim dirFile As String

    pSTR = "G:\DP\Mstanley\history\JUN18\WESTROCK\"
    strFile = "Bundle_" & "*" & ".zip"
    dirFile = Dir(pSTR & strFile)

    Call UnzipAFile(dirFile, "G:\DP\Mstanley\history\JUN18\WESTROCK\Dump") 
End Sub

任何想法/帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您需要遍历所有文件并对要处理的文件执行任何操作。

完成后,将文件移动到存档文件夹。

    Dim di As DirectoryInfo = New DirectoryInfo("C:\ExampleDir\")

    For Each fi In di.GetFiles()
        ' Unzip file file
        ' do stuff to the contents
        ' move the file to an archive folder
    Next

答案 1 :(得分:0)

我更改了UnzipAFile子的签名。你真的想接受任何变量类型,还是想要字符串?

这将在文件夹中搜索最新的" Bundles _"文件并解压缩那一个。我无法理解" date"在捆绑文件的末尾,所以我在zip文件本身上使用了修改日期。

此解决方案需要引用Microsoft Scripting Runtime(scrrun.dll文件)

Sub UnzipLatest(bundlesFolder As String, unzipToPath As String)

    Dim fil As File, fol As Folder
    Dim fso As New FileSystemObject
    Dim latestDate As Date, latestFile As String, latestBundleFileFound As Boolean
    If Not fso.FolderExists(bundlesFolder) Then Exit Sub

    Set fol = fso.GetFolder(bundlesFolder)
    For Each fil In fol.Files
        If fil.Name Like "*Bundles_*" Then
            latestBundleFileFound = True
            If fil.DateLastModified > latestDate Then
                latestDate = fil.DateLastModified
                latestFile = fil.path
            End If
        End If
    Next
    If latestBundleFileFound Then
        UnzipAFile latestFile, unzipToPath
    End If
End Sub

Sub UnzipAFile(zippedFileFullName As String, unzipToPath As String)

End Sub