【问题标题】:Suggestion with 7Z unzip via command line via vba建议通过命令行通过 vba 解压缩 7Z
【发布时间】:2015-02-02 16:28:06
【问题描述】:

我正在尝试通过命令行从 VBA 解压缩 docx,是否可以提取 word 文件的内容,即提取到自定义路径时的文件夹结构。

如果我使用“7z e”命令,我可以解压到自定义路径,但文件夹结构会丢失。

如果我使用“7z x”命令,文件夹结构会保留,但提取是在“用户文件夹”下完成的

任何建议都会很有帮助,

提前致谢。

代码:

Sub UnZip7Zip(strTargetPath As String, Fname As Variant, Fext As Variant)
Dim oApp As Object, FSOobj As Object, fol, objFiles As Object, lngFileCount As Long, Source As String, Destination As String
Dim MyFile As String, Outdir As String, Cmdstr As String
Dim FileNameFolder As Variant
If Right(strTargetPath, 1) <> Application.PathSeparator Then
    strTargetPath = strTargetPath & Application.PathSeparator
End If
FileNameFolder = strTargetPath & Fname
'create destination folder if it does not exist
Set FSOobj = CreateObject("Scripting.FilesystemObject")
If FSOobj.FolderExists(FileNameFolder) = False Then
    FSOobj.CreateFolder FileNameFolder
End If
Set objFiles = FSOobj.GetFolder(strTargetPath & Fname).Files
lngFileCount = objFiles.count
If (lngFileCount = 0) Then
    'Prompts whether to overwrite if files exist ' This extracts all files into a single folder
    Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " e " & """" & MyFile & """" & " -o" & """" & Outdir & """"
    'This command is working wen directly give in cmd prmpt
    'Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " x " & """" & MyFile & """"
ElseIf (lngFileCount > 0) Then
    'Replace files in the destination 
    Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " e " & """" & MyFile & """" & "-aoa" & " -o" & """" & Outdir & """"
    'This command is working wen directly give in cmd prmpt
    'Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " x " & """" & MyFile & """" & "-aoa"
End If
Debug.Print Cmdstr
'MsgBox Cmdstr
'Extract to the specified path
Call Shell(Cmdstr, 1) ' Here i extract the zip file
'CommandLine Cmdstr, True
Set oApp = Nothing
Set FSOobj = Nothing
Set FileNameFolder = Nothing
End Sub

【问题讨论】:

  • ' 这会将所有文件提取到一个文件夹中 Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & " e " & " """ & MyFile & """" & " -o" & """" & Outdir & """" '这个命令是有效的 wen 直接在 cmd prmpt Cmdstr = """" & "C:\Program Files\7-Zip\7z.exe" & """" & "x" & """" & MyFile & """" Call Shell(Cmdstr, 1) ' 这里我解压 zip 文件
  • 抱歉,无法附上整个代码,所以附上几行。 "C:\Program Files\7-Zip\7z.exe" e "C:\Users\Public\AppData\Local\Temp\Sample.zip" -o"C:\Users\Public\AppData\Local\Temp\ Sample\" "C:\Program Files\7-Zip\7z.exe" x "C:\Users\Public\AppData\Local\Temp\Sample.zip"
  • 编辑您的问题以添加代码 - cmets 中的代码太难阅读了。

标签: vba cmd 7zip


【解决方案1】:

在您的代码中:

  • 7-Zip 命令行版本是 7za.exe 而不是 7z.exe
  • Dim MyFile As String, Outdir As String 语句声明 变量MyFileOutdir(并分配存储空间),但我 没有看到这些变量在您的代码中定义(在哪里)
  • ... &amp; "-aoa" &amp; ... 中缺少一个空格,应为... &amp; " -aoa" &amp; ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    • 2011-10-27
    • 2021-06-29
    • 2010-09-27
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多