Downloading yesterday files in PowerShell using WinSCPUpload only the latest file with WinSCPChecking WinSCP...
How do we edit a novel that's written by several people?
Using AWS Fargate as web server
Why is this code uniquely decodable?
Eww, those bytes are gross
How to satisfy a player character's curiosity about another player character?
I am on the US no-fly list. What can I do in order to be allowed on flights which go through US airspace?
ip vs ifconfig commands pros and cons
Can chords be played on the flute?
Is Draco canonically good-looking?
How much time does it take for a broken magnet to recover its poles?
Why is commutativity optional in multiplication for rings?
Can the Count of Monte Cristo's calculation of poison dosage be explained?
Finding an integral using a table?
Do authors have to be politically correct in article-writing?
Sometimes a banana is just a banana
If I delete my router's history can my ISP still provide it to my parents?
A Wacky, Wacky Chessboard (That Makes No Sense)
Why do members of Congress in committee hearings ask witnesses the same question multiple times?
Do commercial flights continue with an engine out?
Where is this triangular-shaped space station from?
Is my plan for fixing my water heater leak bad?
Should I choose Itemized or Standard deduction?
Why does the DC-9-80 have this cusp in its fuselage?
On what did Lego base the appearance of the new Hogwarts minifigs?
Downloading yesterday files in PowerShell using WinSCP
Upload only the latest file with WinSCPChecking WinSCP connection status in PowerShell scriptFiles not uploading when using winscpWinSCP overwrites files without askingWinSCP: Unknown command 'winscp.com'WinSCP Synchronisation files buffer (210K files!)Batch Script using Powershell to download from the webParsing and reformatting date string using PowerShellMake WinSCP commands run synchronously in PowerShellDownloading files with Powershell
Design Scope: Use PowerShell and WinSCP .NET assembly to automate nightly downloads by using the file timestamp to identify the files to be downloaded. The FTP server being connected to is IIS so it does not support all commands like MLSD in it's current configuration and I don't see them making a change if requested.
Problem: Files that have a 1 or 2 digit month return different string lengths that I'm unsure how to get around this. My code works now, but will stop working in October.
e.g. March displays 3/dd/yyyy instead of 03/dd/yyyy
Other notes: At first I tried scripting this using WinSCP.com, but I could not find a way to specify the date correctly.
e.g. you can specify *.zip>=1D
or *.zip<=1D
, but *.zip=1D
or *.zip==1D
are not currently supported with the latest release of WinSCP.
Code:
$yesterday = [DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
# OR I have to use ToString("MM/dd/yyyy") for months 10-12,
# but I need both formats to work.
#delete the temporary file
del .FTPfiles.txt
# Load WinSCP .NET assembly
Add-Type -Path "C:Program Files (x86)WinSCPWinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "server.com"
UserName = "joe"
Password = "smith"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$directory = $session.ListDirectory("/Folder")
foreach ($fileInfo in $directory.Files)
{
Write-Output ("{1} {0}" -f
$fileInfo.Name, $fileInfo.LastWriteTime) >> FTPfiles.txt
}
$fileList = get-content .FTPfiles.txt | findstr $yesterday
$stripped = $fileList -creplace '^.*Z12', 'Z12'
# Download files
$remotePath = "/Folder/"
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$lines = $stripped
foreach ($line in $lines)
{
Write-Host ("Downloading {0} ..." -f $line)
$session.GetFiles($remotePath+$line, "C:Downloads").Check()
}
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
The $fileInfo in $directory.Files
writes the last write time and then file name into FTPfiles.txt
for all of the files contained on the FTP server from the specified folder. This text file is then read and then further reduced to just the files that have a write time that occurred on yesterday's date, which currently only works 9 months out of the year because of the date format using 1 digit for the month instead of 2 digits.
Next that file is read and stripped of the dates before the filenames so that the filenames will be used to loop through to download. The transformation looks like this:
FROM:
3/14/2017 2:04:00 AM Z1234_20170314050001_1.zip
3/14/2017 3:04:00 AM Z1234_20170315060002_1.zip
3/14/2017 4:04:00 AM Z1234_20170316070001_1.zip
3/14/2017 5:04:00 AM Z1234_20170317080001_1.zip
TO:
Z1234_20170314050001_1.zip
Z1234_20170315060002_1.zip
Z1234_20170316070001_1.zip
Z1234_20170317080001_1.zip
Then the script uses those filenames to download the needed files from the previous day.
powershell ftp winscp
add a comment |
Design Scope: Use PowerShell and WinSCP .NET assembly to automate nightly downloads by using the file timestamp to identify the files to be downloaded. The FTP server being connected to is IIS so it does not support all commands like MLSD in it's current configuration and I don't see them making a change if requested.
Problem: Files that have a 1 or 2 digit month return different string lengths that I'm unsure how to get around this. My code works now, but will stop working in October.
e.g. March displays 3/dd/yyyy instead of 03/dd/yyyy
Other notes: At first I tried scripting this using WinSCP.com, but I could not find a way to specify the date correctly.
e.g. you can specify *.zip>=1D
or *.zip<=1D
, but *.zip=1D
or *.zip==1D
are not currently supported with the latest release of WinSCP.
Code:
$yesterday = [DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
# OR I have to use ToString("MM/dd/yyyy") for months 10-12,
# but I need both formats to work.
#delete the temporary file
del .FTPfiles.txt
# Load WinSCP .NET assembly
Add-Type -Path "C:Program Files (x86)WinSCPWinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "server.com"
UserName = "joe"
Password = "smith"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$directory = $session.ListDirectory("/Folder")
foreach ($fileInfo in $directory.Files)
{
Write-Output ("{1} {0}" -f
$fileInfo.Name, $fileInfo.LastWriteTime) >> FTPfiles.txt
}
$fileList = get-content .FTPfiles.txt | findstr $yesterday
$stripped = $fileList -creplace '^.*Z12', 'Z12'
# Download files
$remotePath = "/Folder/"
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$lines = $stripped
foreach ($line in $lines)
{
Write-Host ("Downloading {0} ..." -f $line)
$session.GetFiles($remotePath+$line, "C:Downloads").Check()
}
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
The $fileInfo in $directory.Files
writes the last write time and then file name into FTPfiles.txt
for all of the files contained on the FTP server from the specified folder. This text file is then read and then further reduced to just the files that have a write time that occurred on yesterday's date, which currently only works 9 months out of the year because of the date format using 1 digit for the month instead of 2 digits.
Next that file is read and stripped of the dates before the filenames so that the filenames will be used to loop through to download. The transformation looks like this:
FROM:
3/14/2017 2:04:00 AM Z1234_20170314050001_1.zip
3/14/2017 3:04:00 AM Z1234_20170315060002_1.zip
3/14/2017 4:04:00 AM Z1234_20170316070001_1.zip
3/14/2017 5:04:00 AM Z1234_20170317080001_1.zip
TO:
Z1234_20170314050001_1.zip
Z1234_20170315060002_1.zip
Z1234_20170316070001_1.zip
Z1234_20170317080001_1.zip
Then the script uses those filenames to download the needed files from the previous day.
powershell ftp winscp
2
I don't see the problem ATM, the format[DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
will expand to a two digit month in October, the one M just means no leading zero?
– LotPings
Mar 15 '17 at 22:51
@LotPings is right: e.g.[DateTime]::Today.AddDays(-120).ToString("M/dd/yyyy")
returns (cca four months ago date)11/15/2016
with two-digit month. BTW, you don't need to create a temporary file and use externalfindstr.exe
tool. You can check$fileInfo.LastWriteTime
directly in PowerShell using appropriate regex and-match
operator…
– JosefZ
Mar 15 '17 at 23:12
@LotPings is correct, the .NET string formatting will handle that just fine.[datetime]::Now.AddDays(200).ToString("M/dd/yyyy")
will verify (currently puts you into Oct. Or use string format MM for always getting 2 digits
– Austin T French
Mar 16 '17 at 3:42
add a comment |
Design Scope: Use PowerShell and WinSCP .NET assembly to automate nightly downloads by using the file timestamp to identify the files to be downloaded. The FTP server being connected to is IIS so it does not support all commands like MLSD in it's current configuration and I don't see them making a change if requested.
Problem: Files that have a 1 or 2 digit month return different string lengths that I'm unsure how to get around this. My code works now, but will stop working in October.
e.g. March displays 3/dd/yyyy instead of 03/dd/yyyy
Other notes: At first I tried scripting this using WinSCP.com, but I could not find a way to specify the date correctly.
e.g. you can specify *.zip>=1D
or *.zip<=1D
, but *.zip=1D
or *.zip==1D
are not currently supported with the latest release of WinSCP.
Code:
$yesterday = [DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
# OR I have to use ToString("MM/dd/yyyy") for months 10-12,
# but I need both formats to work.
#delete the temporary file
del .FTPfiles.txt
# Load WinSCP .NET assembly
Add-Type -Path "C:Program Files (x86)WinSCPWinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "server.com"
UserName = "joe"
Password = "smith"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$directory = $session.ListDirectory("/Folder")
foreach ($fileInfo in $directory.Files)
{
Write-Output ("{1} {0}" -f
$fileInfo.Name, $fileInfo.LastWriteTime) >> FTPfiles.txt
}
$fileList = get-content .FTPfiles.txt | findstr $yesterday
$stripped = $fileList -creplace '^.*Z12', 'Z12'
# Download files
$remotePath = "/Folder/"
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$lines = $stripped
foreach ($line in $lines)
{
Write-Host ("Downloading {0} ..." -f $line)
$session.GetFiles($remotePath+$line, "C:Downloads").Check()
}
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
The $fileInfo in $directory.Files
writes the last write time and then file name into FTPfiles.txt
for all of the files contained on the FTP server from the specified folder. This text file is then read and then further reduced to just the files that have a write time that occurred on yesterday's date, which currently only works 9 months out of the year because of the date format using 1 digit for the month instead of 2 digits.
Next that file is read and stripped of the dates before the filenames so that the filenames will be used to loop through to download. The transformation looks like this:
FROM:
3/14/2017 2:04:00 AM Z1234_20170314050001_1.zip
3/14/2017 3:04:00 AM Z1234_20170315060002_1.zip
3/14/2017 4:04:00 AM Z1234_20170316070001_1.zip
3/14/2017 5:04:00 AM Z1234_20170317080001_1.zip
TO:
Z1234_20170314050001_1.zip
Z1234_20170315060002_1.zip
Z1234_20170316070001_1.zip
Z1234_20170317080001_1.zip
Then the script uses those filenames to download the needed files from the previous day.
powershell ftp winscp
Design Scope: Use PowerShell and WinSCP .NET assembly to automate nightly downloads by using the file timestamp to identify the files to be downloaded. The FTP server being connected to is IIS so it does not support all commands like MLSD in it's current configuration and I don't see them making a change if requested.
Problem: Files that have a 1 or 2 digit month return different string lengths that I'm unsure how to get around this. My code works now, but will stop working in October.
e.g. March displays 3/dd/yyyy instead of 03/dd/yyyy
Other notes: At first I tried scripting this using WinSCP.com, but I could not find a way to specify the date correctly.
e.g. you can specify *.zip>=1D
or *.zip<=1D
, but *.zip=1D
or *.zip==1D
are not currently supported with the latest release of WinSCP.
Code:
$yesterday = [DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
# OR I have to use ToString("MM/dd/yyyy") for months 10-12,
# but I need both formats to work.
#delete the temporary file
del .FTPfiles.txt
# Load WinSCP .NET assembly
Add-Type -Path "C:Program Files (x86)WinSCPWinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions -Property @{
Protocol = [WinSCP.Protocol]::Ftp
HostName = "server.com"
UserName = "joe"
Password = "smith"
}
$session = New-Object WinSCP.Session
try
{
# Connect
$session.Open($sessionOptions)
$directory = $session.ListDirectory("/Folder")
foreach ($fileInfo in $directory.Files)
{
Write-Output ("{1} {0}" -f
$fileInfo.Name, $fileInfo.LastWriteTime) >> FTPfiles.txt
}
$fileList = get-content .FTPfiles.txt | findstr $yesterday
$stripped = $fileList -creplace '^.*Z12', 'Z12'
# Download files
$remotePath = "/Folder/"
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.TransferMode = [WinSCP.TransferMode]::Binary
$lines = $stripped
foreach ($line in $lines)
{
Write-Host ("Downloading {0} ..." -f $line)
$session.GetFiles($remotePath+$line, "C:Downloads").Check()
}
}
catch [Exception]
{
Write-Host ("Error: {0}" -f $_.Exception.Message)
exit 1
}
finally
{
# Disconnect, clean up
$session.Dispose()
}
The $fileInfo in $directory.Files
writes the last write time and then file name into FTPfiles.txt
for all of the files contained on the FTP server from the specified folder. This text file is then read and then further reduced to just the files that have a write time that occurred on yesterday's date, which currently only works 9 months out of the year because of the date format using 1 digit for the month instead of 2 digits.
Next that file is read and stripped of the dates before the filenames so that the filenames will be used to loop through to download. The transformation looks like this:
FROM:
3/14/2017 2:04:00 AM Z1234_20170314050001_1.zip
3/14/2017 3:04:00 AM Z1234_20170315060002_1.zip
3/14/2017 4:04:00 AM Z1234_20170316070001_1.zip
3/14/2017 5:04:00 AM Z1234_20170317080001_1.zip
TO:
Z1234_20170314050001_1.zip
Z1234_20170315060002_1.zip
Z1234_20170316070001_1.zip
Z1234_20170317080001_1.zip
Then the script uses those filenames to download the needed files from the previous day.
powershell ftp winscp
powershell ftp winscp
edited Mar 22 '17 at 15:45
Brad
asked Mar 15 '17 at 22:03
BradBrad
21128
21128
2
I don't see the problem ATM, the format[DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
will expand to a two digit month in October, the one M just means no leading zero?
– LotPings
Mar 15 '17 at 22:51
@LotPings is right: e.g.[DateTime]::Today.AddDays(-120).ToString("M/dd/yyyy")
returns (cca four months ago date)11/15/2016
with two-digit month. BTW, you don't need to create a temporary file and use externalfindstr.exe
tool. You can check$fileInfo.LastWriteTime
directly in PowerShell using appropriate regex and-match
operator…
– JosefZ
Mar 15 '17 at 23:12
@LotPings is correct, the .NET string formatting will handle that just fine.[datetime]::Now.AddDays(200).ToString("M/dd/yyyy")
will verify (currently puts you into Oct. Or use string format MM for always getting 2 digits
– Austin T French
Mar 16 '17 at 3:42
add a comment |
2
I don't see the problem ATM, the format[DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
will expand to a two digit month in October, the one M just means no leading zero?
– LotPings
Mar 15 '17 at 22:51
@LotPings is right: e.g.[DateTime]::Today.AddDays(-120).ToString("M/dd/yyyy")
returns (cca four months ago date)11/15/2016
with two-digit month. BTW, you don't need to create a temporary file and use externalfindstr.exe
tool. You can check$fileInfo.LastWriteTime
directly in PowerShell using appropriate regex and-match
operator…
– JosefZ
Mar 15 '17 at 23:12
@LotPings is correct, the .NET string formatting will handle that just fine.[datetime]::Now.AddDays(200).ToString("M/dd/yyyy")
will verify (currently puts you into Oct. Or use string format MM for always getting 2 digits
– Austin T French
Mar 16 '17 at 3:42
2
2
I don't see the problem ATM, the format
[DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
will expand to a two digit month in October, the one M just means no leading zero?– LotPings
Mar 15 '17 at 22:51
I don't see the problem ATM, the format
[DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
will expand to a two digit month in October, the one M just means no leading zero?– LotPings
Mar 15 '17 at 22:51
@LotPings is right: e.g.
[DateTime]::Today.AddDays(-120).ToString("M/dd/yyyy")
returns (cca four months ago date) 11/15/2016
with two-digit month. BTW, you don't need to create a temporary file and use external findstr.exe
tool. You can check $fileInfo.LastWriteTime
directly in PowerShell using appropriate regex and -match
operator…– JosefZ
Mar 15 '17 at 23:12
@LotPings is right: e.g.
[DateTime]::Today.AddDays(-120).ToString("M/dd/yyyy")
returns (cca four months ago date) 11/15/2016
with two-digit month. BTW, you don't need to create a temporary file and use external findstr.exe
tool. You can check $fileInfo.LastWriteTime
directly in PowerShell using appropriate regex and -match
operator…– JosefZ
Mar 15 '17 at 23:12
@LotPings is correct, the .NET string formatting will handle that just fine.
[datetime]::Now.AddDays(200).ToString("M/dd/yyyy")
will verify (currently puts you into Oct. Or use string format MM for always getting 2 digits– Austin T French
Mar 16 '17 at 3:42
@LotPings is correct, the .NET string formatting will handle that just fine.
[datetime]::Now.AddDays(200).ToString("M/dd/yyyy")
will verify (currently puts you into Oct. Or use string format MM for always getting 2 digits– Austin T French
Mar 16 '17 at 3:42
add a comment |
2 Answers
2
active
oldest
votes
Try this function and pass your specified date range in parameter StartDate
and EndDate
Function Download-Files
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][Object]$Session,
[Parameter(Mandatory=$true)][String]$RemotePath,
[Parameter(Mandatory=$true)][String]$LocalPath,
[Parameter(Mandatory=$false)][String]$StartDate,
[Parameter(Mandatory=$false)][String]$EndDate
)
If (-Not (Test-Path -Path $LocalPath -PathType Container)) {
New-Item -Path $LocalPath -ItemType Directory
}
Get-WinSCPChildItem -WinSCPSession $Session -Path $RemotePath | ForEach-Object {
If (-not($_.IsThisDirectory) -and -not($_.IsParentDirectory) -and $_.IsDirectory) {
Download-Files -Session $Session -RemotePath "$RemotePath$($_.Name)" -LocalPath "$LocalPath$($_.Name)" -StartDate $StartDate -EndDate $EndDate
}
If (-not($_.IsDirectory)) {
If ($_.LastWriteTime -ge $StartDate -and $_.LastWriteTime -le $EndDate) {
Receive-WinSCPItem -WinSCPSession $Session -Path "$RemotePath$($_.Name)" -Destination $LocalPath
}
}
}
}
You can download the complete code sample from How to download recent files in PowerShell by WinSCP
add a comment |
You can use both upper and lower time constraint in a single WinSCP file mask:*>=2017-03-15<2017-03-16
(midnight times are implied, that's also why *=2017-03-15
is not, what you want; and also the reason why it's not implemented, as it would not be of any use)
In PowerShell, you can implement it like:
$yesterday = (Get-Date).AddDays(-1)
$yesterday_timestamp = $yesterday.ToString("yyyy-MM-dd")
$today = Get-Date
$today_timestamp = $today.ToString("yyyy-MM-dd")
$transferOptions = New-Object WinSCP.TransferOptions
$file_mask = "*>=$yesterday_timestamp<$today_timestamp"
$transferOptions.FileMask = $file_mask
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See WinSCP article on formatting timestamps in PowerShell.
Though you can also use WinSCP %TIMESTAMP%
syntax for even simpler implementation:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=%TIMESTAMP-1D#yyyy-mm-dd%<%TIMESTAMP#yyyy-mm-dd%"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
And it's even more easier with WinSCP 5.14:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=yesterday<today"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See the WinSCP article about Downloading the most recent file.
When I use $session.GetFiles("/Folder/*" it's recursively downloading child folders too. How can I exclude folders?
– Brad
Mar 22 '17 at 18:45
I can filter to *.zip which works, but if I needed everything from the specified folder without being recursive, how can I exclude this?
– Brad
Mar 22 '17 at 18:51
See WinSCP FAQ How do I transfer (or synchronize) directory non-recursively?.
– Martin Prikryl
Mar 22 '17 at 19:30
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1189111%2fdownloading-yesterday-files-in-powershell-using-winscp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Try this function and pass your specified date range in parameter StartDate
and EndDate
Function Download-Files
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][Object]$Session,
[Parameter(Mandatory=$true)][String]$RemotePath,
[Parameter(Mandatory=$true)][String]$LocalPath,
[Parameter(Mandatory=$false)][String]$StartDate,
[Parameter(Mandatory=$false)][String]$EndDate
)
If (-Not (Test-Path -Path $LocalPath -PathType Container)) {
New-Item -Path $LocalPath -ItemType Directory
}
Get-WinSCPChildItem -WinSCPSession $Session -Path $RemotePath | ForEach-Object {
If (-not($_.IsThisDirectory) -and -not($_.IsParentDirectory) -and $_.IsDirectory) {
Download-Files -Session $Session -RemotePath "$RemotePath$($_.Name)" -LocalPath "$LocalPath$($_.Name)" -StartDate $StartDate -EndDate $EndDate
}
If (-not($_.IsDirectory)) {
If ($_.LastWriteTime -ge $StartDate -and $_.LastWriteTime -le $EndDate) {
Receive-WinSCPItem -WinSCPSession $Session -Path "$RemotePath$($_.Name)" -Destination $LocalPath
}
}
}
}
You can download the complete code sample from How to download recent files in PowerShell by WinSCP
add a comment |
Try this function and pass your specified date range in parameter StartDate
and EndDate
Function Download-Files
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][Object]$Session,
[Parameter(Mandatory=$true)][String]$RemotePath,
[Parameter(Mandatory=$true)][String]$LocalPath,
[Parameter(Mandatory=$false)][String]$StartDate,
[Parameter(Mandatory=$false)][String]$EndDate
)
If (-Not (Test-Path -Path $LocalPath -PathType Container)) {
New-Item -Path $LocalPath -ItemType Directory
}
Get-WinSCPChildItem -WinSCPSession $Session -Path $RemotePath | ForEach-Object {
If (-not($_.IsThisDirectory) -and -not($_.IsParentDirectory) -and $_.IsDirectory) {
Download-Files -Session $Session -RemotePath "$RemotePath$($_.Name)" -LocalPath "$LocalPath$($_.Name)" -StartDate $StartDate -EndDate $EndDate
}
If (-not($_.IsDirectory)) {
If ($_.LastWriteTime -ge $StartDate -and $_.LastWriteTime -le $EndDate) {
Receive-WinSCPItem -WinSCPSession $Session -Path "$RemotePath$($_.Name)" -Destination $LocalPath
}
}
}
}
You can download the complete code sample from How to download recent files in PowerShell by WinSCP
add a comment |
Try this function and pass your specified date range in parameter StartDate
and EndDate
Function Download-Files
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][Object]$Session,
[Parameter(Mandatory=$true)][String]$RemotePath,
[Parameter(Mandatory=$true)][String]$LocalPath,
[Parameter(Mandatory=$false)][String]$StartDate,
[Parameter(Mandatory=$false)][String]$EndDate
)
If (-Not (Test-Path -Path $LocalPath -PathType Container)) {
New-Item -Path $LocalPath -ItemType Directory
}
Get-WinSCPChildItem -WinSCPSession $Session -Path $RemotePath | ForEach-Object {
If (-not($_.IsThisDirectory) -and -not($_.IsParentDirectory) -and $_.IsDirectory) {
Download-Files -Session $Session -RemotePath "$RemotePath$($_.Name)" -LocalPath "$LocalPath$($_.Name)" -StartDate $StartDate -EndDate $EndDate
}
If (-not($_.IsDirectory)) {
If ($_.LastWriteTime -ge $StartDate -and $_.LastWriteTime -le $EndDate) {
Receive-WinSCPItem -WinSCPSession $Session -Path "$RemotePath$($_.Name)" -Destination $LocalPath
}
}
}
}
You can download the complete code sample from How to download recent files in PowerShell by WinSCP
Try this function and pass your specified date range in parameter StartDate
and EndDate
Function Download-Files
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$true)][Object]$Session,
[Parameter(Mandatory=$true)][String]$RemotePath,
[Parameter(Mandatory=$true)][String]$LocalPath,
[Parameter(Mandatory=$false)][String]$StartDate,
[Parameter(Mandatory=$false)][String]$EndDate
)
If (-Not (Test-Path -Path $LocalPath -PathType Container)) {
New-Item -Path $LocalPath -ItemType Directory
}
Get-WinSCPChildItem -WinSCPSession $Session -Path $RemotePath | ForEach-Object {
If (-not($_.IsThisDirectory) -and -not($_.IsParentDirectory) -and $_.IsDirectory) {
Download-Files -Session $Session -RemotePath "$RemotePath$($_.Name)" -LocalPath "$LocalPath$($_.Name)" -StartDate $StartDate -EndDate $EndDate
}
If (-not($_.IsDirectory)) {
If ($_.LastWriteTime -ge $StartDate -and $_.LastWriteTime -le $EndDate) {
Receive-WinSCPItem -WinSCPSession $Session -Path "$RemotePath$($_.Name)" -Destination $LocalPath
}
}
}
}
You can download the complete code sample from How to download recent files in PowerShell by WinSCP
answered Apr 12 '17 at 9:25
EricEric
1311
1311
add a comment |
add a comment |
You can use both upper and lower time constraint in a single WinSCP file mask:*>=2017-03-15<2017-03-16
(midnight times are implied, that's also why *=2017-03-15
is not, what you want; and also the reason why it's not implemented, as it would not be of any use)
In PowerShell, you can implement it like:
$yesterday = (Get-Date).AddDays(-1)
$yesterday_timestamp = $yesterday.ToString("yyyy-MM-dd")
$today = Get-Date
$today_timestamp = $today.ToString("yyyy-MM-dd")
$transferOptions = New-Object WinSCP.TransferOptions
$file_mask = "*>=$yesterday_timestamp<$today_timestamp"
$transferOptions.FileMask = $file_mask
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See WinSCP article on formatting timestamps in PowerShell.
Though you can also use WinSCP %TIMESTAMP%
syntax for even simpler implementation:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=%TIMESTAMP-1D#yyyy-mm-dd%<%TIMESTAMP#yyyy-mm-dd%"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
And it's even more easier with WinSCP 5.14:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=yesterday<today"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See the WinSCP article about Downloading the most recent file.
When I use $session.GetFiles("/Folder/*" it's recursively downloading child folders too. How can I exclude folders?
– Brad
Mar 22 '17 at 18:45
I can filter to *.zip which works, but if I needed everything from the specified folder without being recursive, how can I exclude this?
– Brad
Mar 22 '17 at 18:51
See WinSCP FAQ How do I transfer (or synchronize) directory non-recursively?.
– Martin Prikryl
Mar 22 '17 at 19:30
add a comment |
You can use both upper and lower time constraint in a single WinSCP file mask:*>=2017-03-15<2017-03-16
(midnight times are implied, that's also why *=2017-03-15
is not, what you want; and also the reason why it's not implemented, as it would not be of any use)
In PowerShell, you can implement it like:
$yesterday = (Get-Date).AddDays(-1)
$yesterday_timestamp = $yesterday.ToString("yyyy-MM-dd")
$today = Get-Date
$today_timestamp = $today.ToString("yyyy-MM-dd")
$transferOptions = New-Object WinSCP.TransferOptions
$file_mask = "*>=$yesterday_timestamp<$today_timestamp"
$transferOptions.FileMask = $file_mask
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See WinSCP article on formatting timestamps in PowerShell.
Though you can also use WinSCP %TIMESTAMP%
syntax for even simpler implementation:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=%TIMESTAMP-1D#yyyy-mm-dd%<%TIMESTAMP#yyyy-mm-dd%"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
And it's even more easier with WinSCP 5.14:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=yesterday<today"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See the WinSCP article about Downloading the most recent file.
When I use $session.GetFiles("/Folder/*" it's recursively downloading child folders too. How can I exclude folders?
– Brad
Mar 22 '17 at 18:45
I can filter to *.zip which works, but if I needed everything from the specified folder without being recursive, how can I exclude this?
– Brad
Mar 22 '17 at 18:51
See WinSCP FAQ How do I transfer (or synchronize) directory non-recursively?.
– Martin Prikryl
Mar 22 '17 at 19:30
add a comment |
You can use both upper and lower time constraint in a single WinSCP file mask:*>=2017-03-15<2017-03-16
(midnight times are implied, that's also why *=2017-03-15
is not, what you want; and also the reason why it's not implemented, as it would not be of any use)
In PowerShell, you can implement it like:
$yesterday = (Get-Date).AddDays(-1)
$yesterday_timestamp = $yesterday.ToString("yyyy-MM-dd")
$today = Get-Date
$today_timestamp = $today.ToString("yyyy-MM-dd")
$transferOptions = New-Object WinSCP.TransferOptions
$file_mask = "*>=$yesterday_timestamp<$today_timestamp"
$transferOptions.FileMask = $file_mask
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See WinSCP article on formatting timestamps in PowerShell.
Though you can also use WinSCP %TIMESTAMP%
syntax for even simpler implementation:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=%TIMESTAMP-1D#yyyy-mm-dd%<%TIMESTAMP#yyyy-mm-dd%"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
And it's even more easier with WinSCP 5.14:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=yesterday<today"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See the WinSCP article about Downloading the most recent file.
You can use both upper and lower time constraint in a single WinSCP file mask:*>=2017-03-15<2017-03-16
(midnight times are implied, that's also why *=2017-03-15
is not, what you want; and also the reason why it's not implemented, as it would not be of any use)
In PowerShell, you can implement it like:
$yesterday = (Get-Date).AddDays(-1)
$yesterday_timestamp = $yesterday.ToString("yyyy-MM-dd")
$today = Get-Date
$today_timestamp = $today.ToString("yyyy-MM-dd")
$transferOptions = New-Object WinSCP.TransferOptions
$file_mask = "*>=$yesterday_timestamp<$today_timestamp"
$transferOptions.FileMask = $file_mask
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See WinSCP article on formatting timestamps in PowerShell.
Though you can also use WinSCP %TIMESTAMP%
syntax for even simpler implementation:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=%TIMESTAMP-1D#yyyy-mm-dd%<%TIMESTAMP#yyyy-mm-dd%"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
And it's even more easier with WinSCP 5.14:
$transferOptions = New-Object WinSCP.TransferOptions
$transferOptions.FileMask = "*>=yesterday<today"
$session.GetFiles("/Folder/*", "C:Downloads", $False, $transferOptions).Check()
See the WinSCP article about Downloading the most recent file.
edited 13 mins ago
answered Mar 16 '17 at 7:05
Martin PrikrylMartin Prikryl
11.1k43278
11.1k43278
When I use $session.GetFiles("/Folder/*" it's recursively downloading child folders too. How can I exclude folders?
– Brad
Mar 22 '17 at 18:45
I can filter to *.zip which works, but if I needed everything from the specified folder without being recursive, how can I exclude this?
– Brad
Mar 22 '17 at 18:51
See WinSCP FAQ How do I transfer (or synchronize) directory non-recursively?.
– Martin Prikryl
Mar 22 '17 at 19:30
add a comment |
When I use $session.GetFiles("/Folder/*" it's recursively downloading child folders too. How can I exclude folders?
– Brad
Mar 22 '17 at 18:45
I can filter to *.zip which works, but if I needed everything from the specified folder without being recursive, how can I exclude this?
– Brad
Mar 22 '17 at 18:51
See WinSCP FAQ How do I transfer (or synchronize) directory non-recursively?.
– Martin Prikryl
Mar 22 '17 at 19:30
When I use $session.GetFiles("/Folder/*" it's recursively downloading child folders too. How can I exclude folders?
– Brad
Mar 22 '17 at 18:45
When I use $session.GetFiles("/Folder/*" it's recursively downloading child folders too. How can I exclude folders?
– Brad
Mar 22 '17 at 18:45
I can filter to *.zip which works, but if I needed everything from the specified folder without being recursive, how can I exclude this?
– Brad
Mar 22 '17 at 18:51
I can filter to *.zip which works, but if I needed everything from the specified folder without being recursive, how can I exclude this?
– Brad
Mar 22 '17 at 18:51
See WinSCP FAQ How do I transfer (or synchronize) directory non-recursively?.
– Martin Prikryl
Mar 22 '17 at 19:30
See WinSCP FAQ How do I transfer (or synchronize) directory non-recursively?.
– Martin Prikryl
Mar 22 '17 at 19:30
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1189111%2fdownloading-yesterday-files-in-powershell-using-winscp%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
I don't see the problem ATM, the format
[DateTime]::Today.AddDays(-1).ToString("M/dd/yyyy")
will expand to a two digit month in October, the one M just means no leading zero?– LotPings
Mar 15 '17 at 22:51
@LotPings is right: e.g.
[DateTime]::Today.AddDays(-120).ToString("M/dd/yyyy")
returns (cca four months ago date)11/15/2016
with two-digit month. BTW, you don't need to create a temporary file and use externalfindstr.exe
tool. You can check$fileInfo.LastWriteTime
directly in PowerShell using appropriate regex and-match
operator…– JosefZ
Mar 15 '17 at 23:12
@LotPings is correct, the .NET string formatting will handle that just fine.
[datetime]::Now.AddDays(200).ToString("M/dd/yyyy")
will verify (currently puts you into Oct. Or use string format MM for always getting 2 digits– Austin T French
Mar 16 '17 at 3:42