Question : How to Transfer All ZIP files from a FTP Directory using Powershell

I am using the following code to attempt to logon to a similar website using Powershell to access an FTP site was wondering how you would transfer all files within a specidied directory to a local destination. For example, how do you transfer all zip files, regardless of name and size, using Powershell to access the FTP site and to transfer the files to my local device if the FTP site is not local to your machine. I have researched how to transfer a single file using Powerscript and came up with the example code below. How would I modify/change this code to allow for all files in the directory to be copied? In addition to this issue, I have also been receiving the error message "$readlength = $responsestream.Read( <<<< $readbuffer,0,1024)
+ You cannot call a method on a null-valued expression." What would cause this error message to appear, using similar code to the following code?

$sourceuri = "ftp://ftp.secureftp-test.com/hamlet.zip"
$targetpath = "C:\hamlet.zip"
$username = "test"
$password = "test"

# Create a FTPWebRequest object to handle the connection to the ftp server
$ftprequest = [System.Net.FtpWebRequest]::create($sourceuri)

# set the request's network credentials for an authenticated connection
$ftprequest.Credentials =
    New-Object System.Net.NetworkCredential($username,$password)

$ftprequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftprequest.UseBinary = $true
$ftprequest.KeepAlive = $false

# send the ftp request to the server
$ftpresponse = $ftprequest.GetResponse()

# get a download stream from the server response
$responsestream = $ftpresponse.GetResponseStream()

# create the target file on the local system and the download buffer
$targetfile = New-Object IO.FileStream ($targetpath,[IO.FileMode]::Create)
[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)

$targetfile.close()

Answer : How to Transfer All ZIP files from a FTP Directory using Powershell

So using the code from that link:

[void][Reflection.Assembly]::LoadFrom("C:\IndyNet-Daily-20051007\Indy.Sockets.dll")
$ftp = new-object Indy.Sockets.FTP
$ftp.Disconnect()
$ftp.Host = $ftphost
$ftp.Username = $username
$ftp.Password = $password
$ftp.Connect()
$ftp.Passive=$true;

wc = new-object System.Net.WebClient
$wc.Credentials = New-Object System.Net.NetworkCredential($strFtpUser,$strFtpPass)
$ls = new-object System.Collections.Specialized.StringCollection;
$ftp.List($ls, "", $true);
foreach($file in $ls){
   "Downloading {0} into {1}.." -f $sourceFileName, $targetDir;
    $ftp.Get($sourceFileName, ($targetDir + $sourceFileName), $true, $false);
}
Random Solutions  
 
programming4us programming4us