使用 VBA Shell 命令提取文件夹中的所有 .gz 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20222353/
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
Extract all .gz file in folder using VBA Shell command
提问by JTFRage
I have the following VBA code to extract all the files within a given directory.
我有以下 VBA 代码来提取给定目录中的所有文件。
Sub extractAllFiles()
Dim MyObj As Object, MySource As Object, file As Variant
Dim shellStr As String
file = Dir("C:\Downloads\")
While (file <> "")
If InStr(file, ".gz") > 0 Then
shellStr = "winzip32 -e C:\Downloads\" & file & " C:\Downloads\"
Call Shell(shellStr, vbHide)
End If
file = Dir
Wend
End Sub
When I execute this sub routine I get a Run-Time error 53, "File Not Found" error. When I copy the shellStr... Example: winzip32 -e C:\Downloads\file1.gz C:\Downloads\
and execute it from command prompt, it works perfectly! I get the text file within file1.gz extracted to the downloads directory. However running it from VBA doesn't seem to work.
当我执行这个子例程时,我得到一个运行时错误 53,“找不到文件”错误。当我复制 shellStr... 示例:winzip32 -e C:\Downloads\file1.gz C:\Downloads\
并从命令提示符执行它时,它完美地工作!我将 file1.gz 中的文本文件解压缩到下载目录中。但是从 VBA 运行它似乎不起作用。
Can anyone shed some light?
任何人都可以透露一些信息吗?
采纳答案by jacouh
You should try with the full path of the shell command, like this, that worked for me:
您应该尝试使用对我有用的 shell 命令的完整路径,如下所示:
Sub extractAllFiles()
Dim MyObj As Object, MySource As Object, file As Variant
Dim shellStr As String
file = Dir("C:\Downloads\")
While (file <> "")
If InStr(1, file, ".gz") > 0 Then
shellStr = "C:\Program Files (x86)\WinZip\winzip32 -e C:\Downloads\" & file & " C:\Downloads\"
Call Shell(shellStr, vbHide)
End If
file = Dir
Wend
End Sub
My winzip is installed as C:\Program Files (x86)\WinZip\winzip32. You should use yours. Your install path may be:
我的 winzip 安装为 C:\Program Files (x86)\WinZip\winzip32。你应该用你的。您的安装路径可能是:
C:\Program Files\WinZip\winzip32
回答by jacouh
WinZip path needs to be absolute path:
WinZip 路径需要是绝对路径:
C:\Program Files\WinZip\winzip32'
C:\Program Files\WinZip\winzip32'
回答by jacouh
Check your WinZip path. That needs to be fine fixed to the full path to your WinZip.
检查您的 WinZip 路径。这需要很好地修复到 WinZip 的完整路径。
回答by Jerome Montino
Using the logic from this SO post, try the below code:
使用此 SO post 中的逻辑,尝试以下代码:
Sub ExtractAllFiles()
Dim FileName As String
Dim ShellStr
FileName = Dir("C:\Downloads\*.gz")
Do While Len(FileName) > 0
ShellStr = "winzip32 -e " & FileName & " C:\Downloads"
Call Shell(ShellStr, vbHide)
FileName = Dir
Loop
End Sub
Let us know if this helps.
如果这有帮助,请告诉我们。