Post a reply

Options
Add an Attachment

If you do not want to add an Attachment to your Post, please leave the Fields blank.

(maximum 10 MB; please compress large files; only common media, archive, text and programming file formats are allowed)

Options

Topic review

martin

Re: Powershell Multiple FileMasks and ModifiedTime criteria

Your solution is best possible (assuming that you really want to download all remote files - even those in subdirectories - to the same local directory - i.e. that you want to flatten a remote directory structure).
kilde

If-solution

So, I've implemented an if statement in the loop, checking the files timestamps, but I'd hope that a mask could execute much faster.

For those looking for a solution, here is my current implementation:

try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
#Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "ftp.host.com"
UserName = "testuser"
Password = "testpass"
}

$cuttime = $(get-date).addminutes(-30)

$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
Write-Host "Session opened at $(Get-Date)."

$remotePath = "/folder/"
$localPath = "C:\data"
$mask = "*.csv"

$files = $session.EnumerateRemoteFiles(
$remotePath, $mask, [WinSCP.EnumerationOptions]::AllDirectories)
foreach ($fileInfo in $files)
{
$remoteWriteTime = $fileInfo.LastWriteTime

if($remoteWriteTime -gt $cuttime)
{
#Write-Host "Downloading $($fileInfo.FullName) ..."
$filePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
$session.GetFiles($filePath, $localPath + "\*").Check()
#Write-Host "Download of $($fileInfo.FullName) succeeded"
}

}
}
finally
{
# Disconnect, clean up
$session.Dispose()
Write-Host "Session closed at $(Get-Date)."
}

exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}
kilde

Powershell Multiple FileMasks and ModifiedTime criteria

Hi,

I have a script which works when $mask = "*.csv". It retrieves what it should. However, I would like to apply another mask, namely files modified within last 3 hours.
I've tried $mask = "*.csv | *>3H" which fails, and also $mask = "*>3H" which fails too.

If necessary, I can do without my file extention criteria.

I hope someone can help me implement the modifiedTime mask.

My script is:
try
{
# Load WinSCP .NET assembly
Add-Type -Path "C:\Program Files (x86)\WinSCP\WinSCPnet.dll"
#Add-Type -Path "WinSCPnet.dll"

# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::ftp
HostName = "ftp.host.com"
UserName = "testuser"
Password = "testpass"
}

$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
Write-Host "Session opened at $(Get-Date)."

$remotePath = "/folder/"
$localPath = "C:\data"
$mask = "*.csv"

$files = $session.EnumerateRemoteFiles(
$remotePath, $mask, [WinSCP.EnumerationOptions]::AllDirectories)
foreach ($fileInfo in $files)
{
Write-Host "Downloading $($fileInfo.FullName) ..."
$filePath = [WinSCP.RemotePath]::EscapeFileMask($fileInfo.FullName)
$session.GetFiles($filePath, $localPath + "\*").Check()
Write-Host "Download of $($fileInfo.FullName) succeeded"
}
}
finally
{
# Disconnect, clean up
$session.Dispose()
Write-Host "Session closed at $(Get-Date)."
}

exit 0
}
catch
{
Write-Host "Error: $($_.Exception.Message)"
exit 1
}