Karthikeyan
Karthikeyan

Reputation: 2001

Copying a file from an FTP server using an absolute path in PowerShell/WebClient

Am trying to copy a file from HP-UX to my Windows machine using PowerShell script.

Please find my script below.

$File = "d:\copiedfile.txt"
$ftp = "ftp://my_Unix_Domain_name/tmp/sourceFile.txt"
"ftp url: $ftp"
$webclient = New-Object System.Net.WebClient
$uri = New-Object System.Uri($ftp)
"Downloading $File..."
$webclient.DownloadFile($uri, $File)

Am able to connected via FTP but, files not copied to my destination directory.

I'm getting an error:

The remote server return ed an error: (550) File unavailable (e.g., file not found, no access).

Not sure, what is wrong in it.

I'm able to download the file using command-line ftp:

ftp> get /tmp/text.sh
200 PORT command successful.
150 Opening ASCII mode data connection for /tmp/test.sh (71 bytes).
226 Transfer complete.
ftp: 76 bytes received in 0.00Seconds 76000.00Kbytes/sec.
ftp>

Upvotes: 1

Views: 805

Answers (1)

Martin Prikryl
Martin Prikryl

Reputation: 202168

The .NET implementation of FTP (WebClient or FtpWebRequest) do not consider the slash between hostname and file path to be a part of the file path.

So if you need to use an absolute path to a file (like /tmp/sourceFile.txt) in the URL, you have to add yet another slash:

$ftp = "ftp://my_Unix_Domain_name//tmp/sourceFile.txt"

Upvotes: 1

Related Questions