PowerShell: System.Net.WebClient.DownloadFile won't download files collected from ListDirectory
<p>I'm trying to download some zip files from an FTP server. The FTP server is structured like this</p>

<pre class="lang-none prettyprint-override"><code>ftp://ftp.example.com
|
 -&gt; /Download
    | 
     -&gt; file1.zip, file2.zip, file3.zip etc
</code></pre>

<p>I have pulled all of the files into an array called <code>$ftpFiles</code></p>

<pre><code>foreach ($zip in $ftpFiles)
{
    $LocalFile = "C:\Temp\$zip"
    $RemoteFile = "$site/$zip"
    $ftp = New-Object System.Net.WebClient
    $ftp.Credentials = new-object System.Net.NetworkCredential($Username, $realPassword)
    $uri = New-Object System.Uri("$RemoteFile")
    $ftp.DownloadFile($uri, $LocalFile)
    Write-Host "$zip download complete"
}
</code></pre>

<p>The problem is <code>$ftp.DownloadFile</code> will not work with my <code>$LocalFile</code> variable. However, if I manually input the <code>$LocalFile</code> information it will.</p>

<p>For example</p>

<pre><code>$ftp.DownloadFile($uri, "C:\temp\file1.zip")
</code></pre>

<p>works just fine, but</p>

<pre><code>$ftp.DownloadFile($uri, $LocalFile)
</code></pre>

<p>gives me the following error</p>

<pre class="lang-none prettyprint-override"><code>Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."
At line:1 char:34
 +                 $ftp.DownloadFile &lt;&lt;&lt;&lt; ($uri, $LocalFile)
 + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
 + FullyQualifiedErrorId : DotNetMethodException
</code></pre>

<p>For debugging purposes I have been doing</p>

<pre><code>write-host $LocalFile
</code></pre>

<p>which will return correctly as</p>

<pre><code>C:\Temp\file1.zip
</code></pre>

<p>As I expected.</p>

<p>I can only assume that <code>DownloadFile</code> doesn't like the nested variable and is reading it as <code>C:\Temp\$zip</code> and I'm not sure how I can go about fixing it.</p>

<p>EDIT: Comments want to see how the <code>$files</code> array is built</p>

<pre><code>$site = $ftpbase + $dir

$ftp = [System.Net.FtpWebRequest]::Create("$site")
$ftp.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory #Details
$ftp.Credentials = new-object System.Net.NetworkCredential($Username, $realPassword)

$response = $ftp.getresponse() 
$stream = $response.getresponsestream() 

$buffer = new-object System.Byte[] 1024 
$encoding = new-object System.Text.AsciiEncoding 

$outputBuffer = "" 
$foundMore = $false 

## Read all the data available from the stream, writing it to the 
## output buffer when done. 
do 
{

    ## Allow data to buffer for a bit 
    start-sleep -Seconds 2 

    ## Read what data is available 
    $foundmore = $false 
    $stream.ReadTimeout = 1000

    do 
    { 
        try 
        { 
            $read = $stream.Read($buffer, 0, 1024) 

            if($read -gt 0) 
            { 
                $foundmore = $true 
                $outputBuffer += ($encoding.GetString($buffer, 0, $read))

            } 
        } catch { $foundMore = $false; $read = 0 } 
    } while($read -gt 0) 
} 
while($foundmore)

$files = $outputBuffer -split ("\n")
</code></pre>