Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trying to download file from ftp site

Status
Not open for further replies.

mgallot

MIS
Jan 22, 2001
93
US
Trying to download a file from ftp site but receiving errors. "You cannot call a method on a null-valued expression"
Is anybody achieving this?
# create the FtpWebRequest

$ftp=[system.net.ftpwebrequest]::Create('ftp://ftp.site/files/export/')
$ftp = [System.Net.FtpWebRequest]$ftp
$ftp.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
$ftp.Credentials = new-object System.Net.NetworkCredential("id","pw")
$ftp.UseBinary = $true
$ftp.UsePassive = $true
$folderPath="ftp://ftp.site/files/export"



$ftp.Method=[system.net.webrequestmethods+ftp]::ListDirectoryDetails
$response=$ftp.GetResponse()
$strm=$response.GetResponseStream()
$reader=New-Object System.IO.StreamReader($strm,'UTF-8')
$reader.ReadToEnd()

# create the target file on the local system and the download buffer

$targetfile = New-Object IO.FileStream ($targetpath = "C:\Exports\ftpfile7",[IO.FileMode]::Create)
[byte[]]$readbuffer = New-Object byte[] 1024
$folderName = (Get-Date).tostring("dd-MM-yyyy-hh-mm-ss")
New-Item -itemType Directory -Path c:\Exports -Name $FolderName

#Loop through the download
do {
$ReadLength = $ResponseStream.Read($ReadBuffer,0,1024)
$LocalFileFile.Write($ReadBuffer,0,$ReadLength)
}
while ($ReadLength -ne 0)
$LocalFileFile.close()
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top