Help with shell command on paths with spaces

jryan15

Board Regular
Joined
Jan 27, 2005
Messages
168
Hi all,

I'm really struggling to workout this shell command on paths with spaces in them.

The code below works with no issue, but as soon as I uncomment the long path with the same set of 20 zip files, I get an error (Error: cannot find archive).

I feel like I've tried an infinite number of versions using multiple quotes and chr(34)'s, but I can seem to get anything to work.


Code:
Sub UnZipFiles()
    Dim command As String
       
       
    zip = "C:\Program Files\7-Zip\7z.exe"
       
    Source = "C:\test"
    Destin = "C:\dump"
    
 '   Source = "C:\Users\xxxxx\Documents\More Sad Attempts at VB\LEC stuff\test"
 '   Destin = "C:\Users\xxxxx\Documents\More Sad Attempts at VB\LEC stuff\dump"
    
    command = """" & zip & """" & " e " & Source & " -o" & Destin
       
    Shell "cmd /k " & (command), vbNormalFocus
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
I am not sure, but can you try this;

Code:
Command = """" & zip & """" & " e '" & Source & "' -o '" & Destin & "'"
 
Upvote 0
You need quotes around the executable and its two arguments. Multiple quotes in strings are hard to read and I usually prefer calling a helper function to apply the quotes.

Code:
    ':
    ':

    command = Q(zip) & " e " & Q(Source) & " -o " & Q(Destin)
    
    Shell "cmd /k " & command, vbNormalFocus
End Sub

Private Function Q(text As String) As String
     Q = Chr(34) & text & Chr(34)
End Function
 
Upvote 0
SOLVED! Re: Help with shell command on paths with spaces

Got it fixed! Had to find a way to wrap the entire phrase in quotes.


"C:\Program Files\7-Zip\7z.exe" e C:\test -oC:\dump

need to become:

""C:\Program Files\7-Zip\7z.exe" e C:\test -oC:\dump"


Here is the code for what worked:

Code:
Sub UnZipFiles()
    Dim command As String
       
    'path to 7zip program
    zip = "C:\Program Files\7-Zip\7z.exe"
    
    'path of source and destination folders
    Source = "C:\Users\XXXXXX\Documents\More Sad Attempts at VB\LEC stuff\C137-2678_XXXX-1\test"
    Destin = "C:\Users\XXXXXX\Documents\More Sad Attempts at VB\LEC stuff\C137-2678_XXXX-1\dump"
 
    'command string
    command = """""" & zip & """" & " e " & """" & Source & """" & " -o" & """" & Destin & """" & """"
       
    'shell operation
    Shell "cmd /k " & command, vbNormalFocus

End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top