3

I am trying to download zip files from an FTP site, based off retrieving a directory list to find file names.

Download Portion:

$folderPath='ftp://11.111.11.11/'
$target = "C:\Scripts\ps\ftpdl\"

Foreach ($file in ($array | where {$_ -like "data.zip"})) {

$Source = $folderPath+$file
$Path = $target+$file

#$Source = "ftp://11.111.11.11/data.zip"
#$Path = "C:\Scripts\ps\ftpdl\data.zip"

$source
Write-Verbose -Message $Source -verbose
$path
Write-Verbose -message $Path -verbose

$U = "User"
$P = "Pass"
$WebClient2 = New-Object System.Net.WebClient
$WebClient2.Credentials = New-Object System.Net.Networkcredential($U, $P)
$WebClient2.DownloadFile( $source, $path )  
}

If I use the commented out and define the string it downloads correctly. But if I run it as shown I receive the exception error illegal characters in path. Interestingly enough, there is a difference between write-verbose and not.

Output when run as shown:

ftp://11.111.11.11/data.zip
data.zip
C:\Scripts\ps\ftpdl\data.zip
data.zip
Exception calling "DownloadFile" with "2" .........

Output when run with hard coded path & source

ftp://11.111.11.11/data.zip
VERBOSE: ftp://11.111.11.11/data.zip
C:\Scripts\ps\ftpdl\data.zip
VERBOSE: C:\Scripts\ps\ftpdl\data.zip    

And the file downloads nicely.

1 Answer 1

3

Well, of course once I post the question I figured it out. My $array contained `n and `r characters. I needed to find and replace both of them out.

$array=$array -replace "`n",""
$array=$array -replace "`r",""
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.