1

I want to download multiple files from FTP using PowerShell 5.0 with Windows 7. The script I wrote works fine for a single file but does not work for multiple files with wildcard character. Can someone please tell me what I am doing wrong? When I execute the script , I got an error stating:

Exception calling "DownloadFile" with "2" argument(s): "An exception occurred during a WebClient request."At 
C:\Users\Documents\Powershell_Script\write-demo7.ps1:49 char:9
+         $webclient.DownloadFile($fileuri, $localfilename
+         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WebException

I searched the internet and could not find my answer. Please help...
Here is my script:

function Get-FtpDir ($url,$credentials)
{
    $request = [Net.WebRequest]::Create($url)
    $request.Credentials = $credentials
    $request.Method = [System.Net.WebRequestMethods+FTP]::ListDirectory
    $response = $request.GetResponse()
    $reader = New-Object IO.StreamReader $response.GetResponseStream()
    $readline = $reader.ReadLine()
    $output = New-Object System.Collections.Generic.List[System.Object]
    while ($readline -ne $null)
    {
        $output.Add($readline)
        $readline = $reader.ReadLine()
    }
    $reader.Close()
    $response.Close()
    $output
}

$server = "msftran.tran.com"
$user = "myusername"
$pass = "mypassword"
$invocation = (Get-Variable MyInvocation).Value
$localpath = Split-Path $invocation.MyCommand.Path
$YestDate = (Get-Date).AddDays(-2).ToString('yyMMdd') 
$remotefilepath = "/"
$localfilename = "C:\Users\database\Nightly_Files\file*.nightly.out."+$YestDate
$localfilelocation = "$localfilename"
$uri = New-Object System.Uri(“ftp://$server/$remotefilepath”)

#List of all files on FTP-Server
$files = Get-FTPDir $uri -credentials (New-Object System.Net.NetworkCredential($user, $pass))

foreach ($file in $files)
{    
   if ($file -eq "file1.nightly.out.$YestDate" -or 
       $file -eq "file2.nightly.out."+$YestDate -or
       $file -eq "file3.nightly.out."+$YestDate)
     {
        $file        
        $fileuri = New-Object System.Uri(“ftp://$server/$remotefilepath/$file”)               
        $webclient = New-Object System.Net.WebClient        
        $webclient.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
        $webclient.DownloadFile($fileuri, $localfilelocation       
        )
        
     }
    
}
echo 'download completed'  
1

1 Answer 1

1

How would DownloadFile know what to do with your asterisk? And you have that information already in the $file variable. Instead of full local file name, begin only with the path to the folder:

$localfolder = "C:\Users\database\Nightly_Files\"

You could also do well with one if instead of three using -likeoperator:

if ($file -like "file[1-3].nightly.out.$YestDate")

And launch the download concatenating local path with the file name:

$webclient.DownloadFile($fileuri, "$localfolder$file")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.