Execute Windows command using shell

Hi to everybody,
I am using next code on Windows 7 (64) in Parallels 9 (virtual machine).

Dim s As New Shell
Dim cmd As String

#If TargetWin32
#If Target64Bit
cmd = “copy c:/origen/. c:/destino/” // Building for 64-bit Windows
#Else
cmd = “copy c:/origen/. c:/destino/” // Building for 32-bit Windows
#Endif
#EndIf
s.Execute(cmd)
If s.ErrorCode=0 Then
TextField1.Text = s.Result
Else
TextField1.Text = "Error " + Str(s.ErrorCode)
End If

All the time give me the same error: Error 1.
What am I doing wrong ?

Thanks in advance
Ignacio

Try surrounding the source and destination paths in quotes:

cmd = "copy ""c:/origen/*.*"" ""c:/destino/"""

I’m sure windows needs a backslash as it sees forward slashes as switches on the command line.
Because you’re shelling out you have to comply with the shell format, not whatever Xojo allows.

…and in addition to that, try to execute your cmd in dosshell first and see if there is no confirmation/ interaction needed. I see you are using your copy command without any switch like /V or /Y and I would use xcopy instead of copy. It has more options.

[quote=41674:@David Wylie]I’m sure windows needs a backslash as it sees forward slashes as switches on the command line.
Because you’re shelling out you have to comply with the shell format, not whatever Xojo allows.[/quote]

Yes, that’s it. I just tried it and got the error, then I swapped the forward slashes for backslashes and it worked.

Thanks for all your answers… it works perfectly with next code:

  Dim s As New Shell
  Dim cmd As String
  Dim Fichero As String        
  Fichero = toAddressFld4.Text + "SaverisFull" + (Str(d.Month)) + (Str(d.Day)) + (Str(d.Year)) +".bak"

  If TargetWin32 Then
    cmd = "xcopy " + Fichero + " " + "c:\\destino\"
  End If
  
  s.Execute(cmd)
  If s.ErrorCode=0 Then
    TextField1.Text = s.Result
  Else
    TextField1.Text = "Error " + Str(s.ErrorCode)
  End If

nice… and in case your software is used on modern windows desktops (greater than vista) I suggest you to check robocopy. It might have even more options than xcopy. And it leaves your files untouched whats very useful in shared network enviroments.

Ah and always use try… catch…finally… end try esp. when launching Shell scripts or doing I/O stuff.