Winzip not executing with gap in file path

174 views Asked by At

I've used the code from Extract all .gz file in folder using VBA Shell command, to extract .gz files.The problem is that if there is a gap in the filepath, code doesn't work, if there is no gap, it works, as illustrated below: Notice in first example, there is no '_' but a gap ' ', between 'K' and 'L', therefore file path has gaps, whereas example that works, there is an '_', and the whole filepath has no gaps

'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

I want the first example to work, but why doesn't it?

There are no errors. The code runs, opens winzip, but it's empty, no file is unzipped! Many thanks.

1

There are 1 answers

2
Blackhawk On

Try putting quotation marks around the paths in your shell string:

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\"""

In case you didn't already know, two double quotes ("") evaluates to a single double quote inside the string. Compare to languages like C where the backslash would be used to escape the quotation mark (\").