search
Categories
Sponsors
VirtualMetric Hyper-V Monitoring, Hyper-V Reporting
Archive
Blogroll

Badges
MCSE
Community

Cozumpark Bilisim Portali
Listing FTP directory and getting last file of the list with Powershell
Posted in Windows Powershell | 2 Comments | 9,661 views | 11/08/2012 17:01

I’ve used this script on my one of the projects to get last file of FTP directory.
So you always get latest log file in a directory with Powershell.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# FTP Config
$FTPHost = "10.10.10.20"
$Username = "ftpusr"
$Password = "12345678"
$FTPFolder = "%2f/my/log/path"
 
do
{
	# List Directory
	$FTPRequest = [System.Net.FtpWebRequest]::Create("$FTPFolderUrl") 
	$FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $Password) 
	$FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
 
	# Get Last Log File
	$FTPResponse = $FTPRequest.GetResponse()
	$ResponseStream = $FTPResponse.GetResponseStream()
	$FTPReader = New-Object System.IO.Streamreader -ArgumentList $ResponseStream
	do
	{
		try
		{
			$LogData = $FTPReader.ReadToEnd()
			Add-Content -Value $LogData -Path "$ScriptPath\FileList.txt"
			$LastLogFile = Get-Content -Path "$ScriptPath\FileList.txt" | ? {$_.trim() -ne ""} | Select -Last 1
			Clear-Content -Path "$ScriptPath\FileList.txt"
		}
		catch
		{
			Write-Debug $_
		}
	}
	while ($FTPReader.ReadLine() -ne $null)
	$FTPReader.Close()
 
	if ($LastLogFile -ne $null -and $LastLogFile -ne "" -and $LastLogFile -ne " ")
	{
		$ShouldProcess = "Continue"
	}
}
while ($ShouldProcess -ne "Continue")
 
Write-Host "Log File: $LastLogFile"

$LastLogFile is the name of the latest log file.


Comments (2)

fred

November 9th, 2014
13:11:47

Very useful. I needed a simple method to get the name of the most recent file in an ftp directory.
I just onelined the last file retreive method :
$LastLogFile = ($LogData.TrimEnd() -split [environment]::NewLine | Select -Last 1) -split ” ” | Select -Last 1

Regards
Fred.


Andul

July 17th, 2016
22:12:04

I get error properties of every variable,
Please advice..

New-Object : A constructor was not found. Cannot find an appropriate constructor for type System.IO.Streamreader.
At line:17 char:15
+ $FTPReader = New-Object System.IO.Streamreader -ArgumentList $ResponseStream
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [New-Object], PSArgumentException
+ FullyQualifiedErrorId : CannotFindAppropriateCtor,Microsoft.PowerShell.Commands.NewObjectCommand

You cannot call a method on a null-valued expression.
At line:29 char:4
+ Write-Debug $_
+ ~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:33 char:2
+ $FTPReader.Close()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

Exception calling “Create” with “1” argument(s): “Invalid URI: The format of the URI could not be determined.”
At line:10 char:2
+ $FTPRequest = [System.Net.FtpWebRequest]::Create(“$FTPFolderUrl”)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : UriFormatException

The property ‘Credentials’ cannot be found on this object. Verify that the property exists and can be set.
At line:11 char:2
+ $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username, $P …
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

The property ‘Method’ cannot be found on this object. Verify that the property exists and can be set.
At line:12 char:2
+ $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectory
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyNotFound

You cannot call a method on a null-valued expression.
At line:15 char:2
+ $FTPResponse = $FTPRequest.GetResponse()



Leave a Reply