coder
coder

Reputation: 13248

Copy local text file to remote desktop

I am trying to copy a local text file which is in my working directory to other remote desktop

This is the way I am trying to do as mentioned here

ExecuteCommand("Copy" & Directory.GetCurrentDirectory & "\Output\Files.txt \\myservername -u username -p password C$\Files.txt")

Public Sub ExecuteCommand(ByVal Command As String)
        Dim ProcessInfo As ProcessStartInfo
        Dim Process As Process
        ProcessInfo = New ProcessStartInfo("cmd.exe", "/K" & Command)
        ProcessInfo.CreateNoWindow = True
        ProcessInfo.UseShellExecute = True
        Process = Process.Start(ProcessInfo)
End Sub

I'm getting this error:

The filename, directory name or volume label syntax is incorrect

Upvotes: 0

Views: 1756

Answers (1)

Christian.K
Christian.K

Reputation: 49310

Well, first of all, you are missing a space after the "Copy":

ExecuteCommand("Copy" & Directory.GetCurrentDirectory & ...

that will turn into (given that the current directory is "C:\MYDIR" as an example)

cmd.exe /kCopyC:\MYDIR

The lack of a space after the /k option to cmd.exe is not a problem, but looks awkward. I'd put one there as well.

Second, the "\\myservername -u username -p password C$\Files.txt" looks just wrong. That should probably be "\\myservername\C$\Files.txt" following your example. Username and password makes no sense at this point and in the context of the Copy command (copy past error?).

Then you have some bogus(?) line wrappings in the "ExecuteCommand..." example of your question. Could be that those are causing more issues but this is hard to tell as it stands.

Output the value of the Command variable in your ExecuteCommand method (or use a debugger) and check if it is sound. Also, try to execute the whole thing from the command line first to make sure it works.

Putting it all together, I would have written it like this:

ExecuteCommand("Copy " & Directory.GetCurrentDirectory & "\Output\Files.txt \\myservername\C$\Files.txt")

' ...

Public Sub ExecuteCommand(ByVal Command As String)
        Dim ProcessInfo As ProcessStartInfo
        Dim Process As Process
        ProcessInfo = New ProcessStartInfo("cmd.exe", "/K " & Command)
        ProcessInfo.CreateNoWindow = True
        ProcessInfo.UseShellExecute = True
        Process = Process.Start(ProcessInfo)
        ' You might want to wait for the copy operation to actually finish.
        Process.WaitForExit()
        ' You might want to check the success of the operation looking at
        ' Process.ExitCode, which should be 0 when all is good (in this case).
        Process.Dispose()
End Sub

Finally, you could have just used File.Copy instead. No need to invoke cmd.exe for that:

File.Copy(Directory.GetCurrentDirectory & "\Output\Files.txt", 
   "\\myservername\C$\Files.txt")

Upvotes: 1

Related Questions