从 VBA 使用 7-zip 命令行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18983304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:28:58  来源:igfitidea点击:

Using 7-zip command line from VBA

vbaexcel-vbacommand-line7zipexcel

提问by user2811214

As a result of running a macro I've one folder containing various file types: .err, .txt, .sh, .fat ..

作为运行宏的结果,我有一个包含各种文件类型的文件夹:.err、.txt、.sh、.fat ..

If for instance I want that macro to compress only the .fat files this works pretty well:

例如,如果我希望该宏仅压缩 .fat 文件,则效果很好:

ShellStr = PathZipProgram & "7z.exe a " & " " & Chr(34) & NameZipFile & Chr(34) & " " & Chr(34) & FolderName & Chr(34) & "*.fat"

where (I'm trying to keep this as neat as possible):

哪里(我尽量保持整洁):

  1. PathZipProgram -> 7zip directory
  2. NameZipFile -> Output.zip file
  3. FolderName-> Input.zip file
  1. PathZipProgram -> 7zip 目录
  2. NameZipFile -> Output.zip 文件
  3. 文件夹名称-> Input.zip 文件

Instead, if I want to zip only some of those files (e.g. only *.fat and *.sh) reading the 7-zip command line manual it seems that :

相反,如果我只想压缩其中的一些文件(例如,只有 *.fat 和 *.sh)阅读 7-zip 命令行手册,似乎:

ShellStr = PathZipProgram & "7z.exe a " & " " & Chr(34) & NameZipFile & Chr(34) & " " & Chr(34) & FolderName & Chr(34) & "*.fat -i*.sh"

should also work but it doesn't. Could anyone with more experience help me sorting this out?

也应该工作,但它没有。有更多经验的人可以帮我解决这个问题吗?

回答by LS_???

As you may find in 7-Zip command line syntax:

正如您在 7-Zip 命令行语法中可能发现的:

7z <command> [<switch>...] <base_archive_name> [<arguments>...]

<arguments> ::= <switch> | <wildcard> | <filename> | <list_file>

So you just have to repeat your source content:

所以你只需要重复你的源内容:

ShellStr = PathZipProgram & "7z.exe a """ & NameZipFile & """ """ & FolderName & "\*.fat"" """ & FolderName & "\*.sh"""

Tip: You can escape quotes in strings doubling then.

提示:您可以转义字符串中的引号,然后加倍。