Topic "Powershell example for downloading latest file"

Author Message
freff
[View user's profile]

Joined: 2014-06-02
Posts: 2
Location: Uk
Hi,

I am new to scripting and opted for the powershell example purely because I have powershell on my laptop.

In the example that is supplied on the website I am having issue with the code and suspect that it is related to me not the example.

here is the example that you have supplied my first thought it was going to be straight forward, however had an issue registering winscpnet.dll then an issue with the script Directoryinfo was not a cmd, I guess that this line needed a $ before the command.

after inserting a $ the script now moves onto the next issue

Can't get attributes of file '/usr/xxx/xxx.cvs'

Can some one please point me in the right direction I have ensured that I have two files in the folder and while they have the same date stamp they do have different time stamps.

any help would be great

regards

Steve



try
{
# Load WinSCP .NET assembly
[Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCPnet.dll") | Out-Null

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
$sessionOptions.HostName = "XXXXXXXXX"
$sessionOptions.UserName = "XXXXXX"
$sessionOptions.Password = "XXXXXXX"
$sessionOptions.SshHostKeyFingerprint = "ssh-dss 1024 xx:xx:"

$session = New-Object WinSCP.Session

try
{
# Connect
$session.Open($sessionOptions)

$localPath = "c:\downloaded\"
$remotePath = "/usr/xxxx"

# Gel list of files in the directory
$directoryInfo = $session.ListDirectory($remotePath)

# Select the most recent file
$latest =
$directoryInfo.Files | ****************Added $
Where-Object { -Not $_.IsDirectory } |
Sort-Object LastWriteTime |
#Select-Object -Last 1

# Any file at all?
if ($latest -eq $Null)
{
Write-Host "No file found"
exit 1
}

# Download the selected file
$session.GetFiles($session.EscapeFileMask($remotePath + $latest.Name), $localPath).Check()
}
finally
{
# Disconnect, clean up
$session.Dispose()
}

exit 0
}
catch [Exception]
{
Write-Host $_.Exception.Message
exit 1
}
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 24530
Location: Prague, Czechia
First, thanks for pointing out the problem with missing $.

Regarding the error: What is /usr/xxx/xxx.cvs? Is it a path you set $remotePath to? The purpose of the script is to find the latest file in a directory. So the $remotePath should be set to a path to the directory, not a specific path.

Posting a complete error message, or even better a log file, would help.
_________________
Martin Prikryl
freff
[View user's profile]

Joined: 2014-06-02
Posts: 2
Location: Uk
prikryl wrote:
First, thanks for pointing out the problem with missing $.

Regarding the error: What is /usr/xxx/xxx.cvs? Is it a path you set $remotePath to? The purpose of the script is to find the latest file in a directory. So the $remotePath should be set to a path to the directory, not a specific path.

Posting a complete error message, or even better a log file, would help.


Hi Prikryl,


When I run the Powershell script it does not create a log file

After looking at the error I can see that on the remote path I am missing the / at the end I have added the / for the path and it appears to be working now.

Many thanks for your help

Regards

steve
Advertisements

You can post new topics in this forum






Search Site

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License