简体   繁体   English

在文件夹中解压缩 cab 文件,仅某些文件类型并重命名

[英]Unzip cab files in a folder, only certain filetypes and rename

I am trying to solve a unzipping a lot of cab files problem and I managed to create a frankenstein script below from different scripts that does what i need but I am having a lot of problems figuring out the very last step and I need a lil nudge to make it perfect.我正在尝试解决解压缩大量 cab 文件的问题,并且我设法从不同的脚本创建了下面的科学怪人脚本,它可以满足我的需要,但是我在弄清楚最后一步时遇到了很多问题,我需要一点微调使其完美。

I get an archive of a lot of CAB files and each CAB is an archive of one xml and one txt file that has the exact same name in every archive.我得到了许多 CAB 文件的存档,每个 CAB 都是一个 xml 和一个 txt 文件的存档,每个存档中的名称完全相同。 Zip files have serial number as part of the filename. Zip 文件将序列号作为文件名的一部分。

I managed to:我设法:

  1. loop through all cab files in a single folder;循环遍历单个文件夹中的所有 cab 文件;
  2. unzip only xml files from cab;仅从 cab 解压缩 xml 文件;
  3. put the output into a hardcoded destination I wanted将输出放入我想要的硬编码目的地

I need help with appending the serial number I extract in variable NameAdd to the extracted xml file so they dont overwrite one over another as the macro loops through the cab files.我需要帮助将我在变量 NameAdd 中提取的序列号附加到提取的 xml 文件中,这样它们就不会在宏通过 cab 文件循环时覆盖另一个。

Sub UnarchiveCabs()

Dim str_FILENAME As String, str_DIRECTORY As String, str_DESTINATION As String

'cab archives folder
str_DIRECTORY = "C:\Users\vp1\Desktop\input\"

'Loop through all cab files in a given directory
str_FILENAME = Dir(str_DIRECTORY & "*.cab")

Do While Len(str_FILENAME) > 0
Call UnarchiveCabsSub(str_DIRECTORY & str_FILENAME)
Debug.Print str_FILENAME
str_FILENAME = Dir
Loop

End Sub

Sub UnarchiveCabsSub(str_FILENAME As String)
Dim FSO As Object
Dim oApp As Object
Dim Fname As Variant
Dim FileNameFolder As Variant
Dim DefPath As String
Dim strDate As String
Dim FileNameZip As Variant
Dim NameAdd As String

'Fname = Application.GetOpenFilename(filefilter:="Zip Files (*.zip), *.zip", _
                                    MultiSelect:=False)
Fname = str_FILENAME
NameAdd = Left(Right(Fname, 19), 12)

If Fname = False Then
    'Do nothing
Else
    'Root folder for the new folder.
    DefPath = "C:\Users\vp1\Desktop\input\xml\"
    If Right(DefPath, 1) <> "\" Then
        DefPath = DefPath & "\"
    End If


    FileNameFolder = DefPath    'I want them in hardcoded folder

    Set oApp = CreateObject("Shell.Application")

    'if you want to extract all without conditions
    'oApp.Namespace(FileNameFolder).CopyHere oApp.Namespace(Fname).items

    'If you want to extract only one file you can use this:
    'oApp.Namespace(FileNameFolder).CopyHere _
     'oApp.Namespace(Fname).items.Item("filename.txt")

    'Change this "*.xml" to extract the type of files you want
    For Each FileNameZip In oApp.Namespace(Fname).items
        If LCase(FileNameZip) Like LCase("*.xml") Then
            oApp.Namespace(FileNameFolder).CopyHere _
                    oApp.Namespace(Fname).items.Item(CStr(FileNameZip))
        End If
    Next


    'MsgBox "You find the files here: " & FileNameFolder
    Debug.Print "You find the files here: " & FileNameFolder

    On Error Resume Next
    Set FSO = CreateObject("scripting.filesystemobject")
    FSO.deletefolder Environ("Temp") & "\Temporary Directory*", True
End If
End Sub

I tried adding &NameAdd after line "oApp.Namespace(Fname).items.Item(CStr(FileNameZip))" in the following snippet and script runs without errors but nothing happens - cabs dont get extracted.我尝试在以下代码段中的“oApp.Namespace(Fname).items.Item(CStr(FileNameZip))”行之后添加 &NameAdd 并且脚本运行没有错误但没有任何反应 - 出租车没有被提取。 I tried adding it after line, after last parenthesis and after FileNameZip我尝试在行之后、最后一个括号之后和 FileNameZip 之后添加它

For Each FileNameZip In oApp.Namespace(Fname).items
    If LCase(FileNameZip) Like LCase("*.xml") Then
        oApp.Namespace(FileNameFolder).CopyHere _
                oApp.Namespace(Fname).items.Item(CStr(FileNameZip))
    End If
Next

Is there a way to append that NameAdd variable so that files come out of archives as OriginalName-NameAdd.xml有没有办法附加该 NameAdd 变量,以便文件作为 OriginalName-NameAdd.xml 从档案中出来

Any assistance would be amazing任何帮助都会很棒

Thanks to a suggestion by @Comintern the issue has been resolved.感谢@Comintern 的建议,问题已经解决。

I renamed by NameAdd variable to FileNameNew我通过NameAdd变量重命名为FileNameNew
Chanaged it to =Left(Right(Fname, 19), 12) & ".xml"将其更改为=Left(Right(Fname, 19), 12) & ".xml"

And added another line in the snippet above并在上面的代码段中添加了另一行

Name FileNameFolder & FileNameZip As FileNameFolder & FileNameNew 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM