title: folders downloading as files in ftp
body: <p>So in my powershell script when it starts up it polls a ftp server and downloads any files that aren't in the local folder. The problem is when it gets to a folders it downloads them as files. this is my code for checking for new files:</p>
  
  <pre><code>$LocFolder = 'C:\EMSDropBox\*'
  Remove-Item $LocFolder
  $ftprequest = [System.Net.FtpWebRequest]::Create("ftp://NZHQFTP1/tbarnes")
  $ftprequest.Proxy = $null
  $ftprequest.KeepAlive = $false
  $ftprequest.TimeOut = 10000000
  $ftprequest.UsePassive = $False
  $ftprequest.Credentials = New-Object System.Net.NetworkCredential("tbarnes", "Static_flow2290") 
  $ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
  
  $FTPResponse = $ftprequest.GetResponse()
  $ResponseStream = $FTPResponse.GetResponseStream()
  $FTPReader = New-Object System.IO.Streamreader($ResponseStream)
  $filename = $FTPReader.ReadLine()
  while($filename -ne $null)
  {
      try
      {
          if((Test-Path ("C:\emsdropbox\"+$filename)) -ne $true)
          {
              downloadFtp($filename)
          }
          $filename = $FTPReader.ReadLine()
      }
      catch
      {
          Write-Host $_
      }
  }
  $FTPReader.Close()
  $FTPResponse.Close()
  $ResponseStream.Close()
  </code></pre>
  
  <p>and this is the downloadFtp function:</p>
  
  <pre><code>     # FTP Config
  $FTPHost = "****"
  $Username = "******"
  $Password = "*********"
  $FTPFile = $file
  
  # FTP Log File Url
  $FTPFileUrl = "ftp://" + $FTPHost + "/tbarnes/" + $FTPFile
  
      # Create FTP Connection
  $FTPRequest = [System.Net.FtpWebRequest]::Create("$FTPFileUrl") 
  $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) 
  $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
  $FTPRequest.UsePassive = $false
  $FTPRequest.UseBinary = $true
  $FTPRequest.KeepAlive = $false
  $targetfile = New-Object IO.FileStream (("C:\emsdropbox\"+$file),[IO.FileMode]::Create)
  # Get FTP File
  $FTPResponse = $FTPRequest.GetResponse()
  $ResponseStream = $FTPResponse.GetResponseStream()
  $FTPReader = New-Object -typename System.IO.StreamReader -ArgumentList $ResponseStream
  [byte[]]$readbuffer = New-Object byte[] 1024
  
  #loop through the download stream and send the data to the target file
  do{
      $readlength = $ResponseStream.Read($readbuffer,0,1024)
      $targetfile.Write($readbuffer,0,$readlength)
  }
  while ($readlength -ne 0)
  $FTPReader.Close()
  </code></pre>
  
  <p>Im not sure why it wont pull them down as folders so any help or pointers would be great! </p>