1

I am trying to download all files located within a specific folder on a FTP site. The folder is called "In" (as shown in example code) and contains a number of .txt files.

When I run the below code it lists the four .txt files located in the FTP folder "ftp3.example.com/Jaz/In/", however it does not copy them to the target folder of "C:\Users\Jasdeep\Destination\"

Note: The list appears for a split second and then PowerShell closes.

Please see screenshot which shows output list List Example

I have granted full permissions to the folders and contents on FTP Site.

Could someone please advise where I am going wrong?

$ftp = "ftp://ftp3.example.com/Jaz/In/" 
$user = 'username' 
$pass = 'password'
$folder = "/"
$target = 'C:\Users\Jasdeep\Destination'

$credentials = new-object System.Net.NetworkCredential($user, $pass)

function Get-FtpDir ($url,$credentials) {
    $request = [Net.WebRequest]::Create($url)
    $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    if ($credentials) { $request.Credentials = $credentials }
    $response = $request.GetResponse()
    $reader = New-Object IO.StreamReader $response.GetResponseStream() 
    $reader.ReadToEnd()
    $reader.Close()
    $response.Close()
}

$folderPath= $ftp + "/" + $folder + "/"

$Allfiles=Get-FTPDir -url $folderPath -credentials $credentials
$files = ($Allfiles -split "`r`n")

$files 

$webclient = New-Object System.Net.WebClient 
$webclient.Credentials = New-Object System.Net.NetworkCredential($user,$pass) 
$counter = 0
 foreach ($file in ($files | where {$_ -like "*.*"})){
    $source=$folderPath + $file  
    $destination = $target + $file 
    $webclient.DownloadFile($source, (Join-Path $target $file))

    $counter++
    $counter
    $source
}

Thank you in advance!

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Jasdeep
  • 79
  • 1
  • 9

1 Answers1

1

Your code works for me. But you have too many slashes in the URL, so maybe your particular server cannot handle that.

Your download URL is like

ftp://ftp3.example.com/Jaz/In////test2.txt

Change the code to:

$folderPath = "ftp://ftp3.example.com/Jaz/In/"

The second problem is this:

$files = ($Allfiles -split "`r`n")

You rely on the server to return the listing with CR+LF EOL. That's true, only if you make the server use ASCII mode:

$request = [Net.WebRequest]::Create($url)
$request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
$request.UseBinary = $False

Or, as a quick hack specific to your particular FTP server, expect LF only:

$files = ($Allfiles -split "`n")

In any case, "The list appears for a split second and then Powershell closes." indicates that you didn't really debug the problem. Run your script from existing cmd.exe or PowerShell console window, to see its full output, including any error.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    Apparently this does not work: `($Allfiles -split "\`r\`n")` - try only `\`n` – Martin Prikryl Oct 05 '18 at 21:12
  • `Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request." At C:\Users\Jas\Desktop\scripting\FTPMultiDownload.ps1:36 char:5 + $webclient.DownloadFile($source, $target+$file) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : WebException 1 ftp://ftp3.example.com/Jaz/In/test2.txt test4.txt test3.txt test5.txt` – Jasdeep Oct 05 '18 at 21:16
  • That worked perfectly! thank you so much. Are you able to explain why that wasn't working please? – Jasdeep Oct 05 '18 at 21:19
  • See my updated answer. – Martin Prikryl Oct 06 '18 at 07:29
  • Though, in general, your question is duplicate. – Martin Prikryl Oct 06 '18 at 07:30