How to use PSCP to copy file from Unix machine to Windows machine where target path has spaces?

I am having a problem using PSCP in a C# program to copy a file from a Unix machine to a Windows machine. The problem only happens when the target folder on the Windows machine has a space in it. For example, the following works fine: (NOTE: the IP address and password have been changed for this example)

pscp.exe -pw MyPassword [email protected]:/etc/myfolder/myfile.opt C:\download

But when I change it to this:

pscp.exe -pw MyPassword [email protected]:/etc/myfolder/myfile.opt C:\download files

I get the following error: More than one remote source not supported.

I realize that is because PSCP inteprets the space as another target. But how can I do it? I have tried all kinds of things like putting it in quotes and escaping the space. I have tried all of the following and nothing works:

pscp.exe -pw MyPassword [email protected]:/etc/myfolder/myfile.opt "C:\download files"
pscp.exe -pw MyPassword [email protected]:/etc/myfolder/myfile.opt C:\"download files"
pscp.exe -pw MyPassword [email protected]:/etc/myfolder/myfile.opt C:\download\\ files

Any ideas?


Solution 1:

Although the proper answer is probably to use WinSCP and their C# library, I did find a way to get PSCP to work when the target folder has a space in it.

The correct answer is to do this:

pscp.exe -pw MyPassword [email protected]:/etc/myfolder/myfile.opt "C:\download files"

It turns out that I was having another problem that was making me think the above was not working. Originally I was using the full path to the PSCP.EXE executable. The full path included spaces. So I was trying to do the following:

"C:\My PSCP Folder\pscp.exe" -pw MyPassword [email protected]:/etc/myfolder/myfile.opt "C:\download files"

And I was trying to call that from C# using Process.Start() and it was failing. It seemed like it could handle it if there was a space in either the PSCP path or the target path, but not both. I fixed that by including the path to the PSCP executable in my Windows Environment variables. Now I am able to just call PSCP.EXE and it works with the quotes around the target folder.

Solution 2:

Don't use pscp.

The WinSCP client (which is built on top of PuTTY) provides a .NET assembly. Here's an example.

Solution 3:

Append a period to the destination folder. The following should work with the appended . on your example:

"C:\My PSCP Folder\pscp.exe" -pw MyPassword [email protected]:/etc/myfolder/myfile.opt "C:\download files\."

Solution 4:

pscp.exe -pw MyPassword [email protected]:/etc/myfolder/myfile.opt "C:\download files"

well first, as you already mentioned, you have spaces in your path. So it would be wise to use quotes "C:\download files". Second as you already pointed out you need to add your application to your PATH in order to call pscp without any weird path. and third. Use naming conventions that prevent this mess caused by quoting everything.

camelCaseNaming
underscore_naming

Both solve the problem, and you look 100500 times cooler.

Solution 5:

I was having the same problem, I added the pscp.exe directory as a PATH variable for the session in cmd.exe:

set PATH="C:\Program Files\PuTTY;%PATH"

I had an old Windows netbook that I disassembled for parts. I connected the HDD to my Ubuntu desktop as my only current Windows machine is a laptop, and I didn't want to have to get a SATA to USB adapter. I mounted the HDD correctly and could navigate the files, but when trying to transfer from Ubuntu to Windows 10 using:

pscp [email protected]:/media/path/to/file.zip "D:\backups

I received multiple errors, including Local to Local copy not supported, Host does not exist, etc.

I was able to get the file to transfer with no problem by using the [OPTION] provided by pscp called -load sessname where sessname is the name of a saved session. The full command was as follows:

pscp -load ubuntu@local [email protected]:/media/path/to/file.zip "D:\backups

This solved the issue, and using the -load sessname option has worked for every file and directory I've tried to transfer so far, whether I use the user@ipaddress format, or the user@hostname format for the source machine.

Hopefully this helps someone trying to use an Ubuntu machine to restore files from a Windows HDD.