O
O
Oleg2017-12-27 12:31:14
Visual Basic
Oleg, 2017-12-27 12:31:14

VBA how to run command line with parameters?

I use the command robocopyin the Windows command line with the following parameters:

robocopy "D:\My files" "E:\My backup" /e /z /s /xd "temp files"

Directory names contain spaces, and so does the excluded directory.
I'm trying to write a simple program in VBA that would launch cmd.exe with these parameters when a button is clicked. But I don't know how to specify directories with a space in their name. Tell me please! Without spaces, you get:
Private Sub Command1_Click()

    Shell "cmd /c robocopy " & sSource & " /e /z /s " & sPath & " D:\Files E:\Backup " & sFile
    
End Sub

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lvv85, 2017-12-27
@lvv85

Shell "cmd /c robocopy " & Chr(34) & sSource & Chr(34) & " " & Chr(34) & sPath & Chr(34) & " /e /z /s /xd " & Chr(34) & sFile & Chr(34)

A
ATauenis, 2017-12-28
@ATauenis

Shell "cmd /c robocopy """ & sSource & """ /e /z /s """ & sPath & """ D:\Files E:\Backup " & sFile

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question