我使用了Extract all .gz file in folder using VBA Shell command中的代码来提取.gz文件。问题是如果文件路径中存在间隙,代码不起作用,如果没有间隙,则可行,如下图所示: 请注意,在第一个示例中,'K'和'L'之间没有'_'而是间隙'',因此文件路径有间隙, 而有效的例子,有一个'_',整个文件路径没有间隙
'Example that doesn't work:
Sub extractAllFiles()
Dim MyObj As Object, MySource As Object, File As Variant
Dim shellStr As String
File = Dir("Z:\A_B_C\D_E_F\G_H_I\J_K L\_M_N_O\P_Q_R\*.gz")
While (File <> "")
If InStr(1, File, ".gz") > 0 Then
shellStr = "C:\Program Files\WinZip\winzip32 -e Z:\A_B_C\D_E_F\G_H_I\J_K L\_M_N_O\P_Q_R\" & File & " Z:\A_B_C\D_E_F\G_H_I\J_K L\_M_N_O\P_Q_R\"
Call Shell(shellStr, vbHide)
End If
File = Dir
Wend
End Sub
'Example that works:
Sub extractAllFiles()
Dim MyObj As Object, MySource As Object, File As Variant
Dim shellStr As String
File = Dir("Z:\A_B_C\D_E_F\G_H_I\J_K_L\_M_N_O\P_Q_R\*.gz")
While (File <> "")
If InStr(1, File, ".gz") > 0 Then
shellStr = "C:\Program Files\WinZip\winzip32 -e Z:\A_B_C\D_E_F\G_H_I\J_K_L\_M_N_O\P_Q_R\" & File & " Z:\A_B_C\D_E_F\G_H_I\J_K_L\_M_N_O\P_Q_R\"
Call Shell(shellStr, vbHide)
End If
File = Dir
Wend
End Sub
我想要第一个例子,但为什么不呢?
没有错误。代码运行,打开winzip,但它是空的,没有文件解压缩! 非常感谢。
答案 0 :(得分:0)
尝试在shell字符串中的路径周围加上引号:
shellStr = "C:\Program Files\WinZip\winzip32 -e ""Z:\A_B_C\D_E_F\G_H_I\J_K L\_M_N_O\P_Q_R\" & File & """ ""Z:\A_B_C\D_E_F\G_H_I\J_K L\_M_N_O\P_Q_R\"""
如果您还不知道,两个双引号(""
)将计算为字符串中的单个双引号。与C之类的语言相比,反斜杠将用于转义引号(\"
)。