Reputation: 5841
I transfer files (generated every weekday) to remote directory over FTP. I have set up a Windows scheduled task to automate transfer via WinSCP with a script which will be loaded as argument.
Filename format is prefixYESTDATE.txt
I am using %TIMESTAMP-1D#yyyymmdd%
to compose the filename which changes every weekday.
On Monday my logic of -1D
in TIMESTAMP
would fail (since it returns Sunday).
I am looking for a way to set the correct YESTDATE
on Monday for the script.
Script:
open ftp://uname:[email protected]/
cd destFolder
put prefix%TIMESTAMP-1D#yyyymmdd%.txt
close
exit
Upvotes: 1
Views: 368
Reputation: 202272
Correct "yesterday for Monday" is "Sunday".
WinSCP %TIMESTAMP%
syntax cannot calculate weekdays.
You can use the following PowerShell script with a use of WinSCP .NET assembly:
# Load WinSCP .NET assembly
Add-Type -Path "WinSCPnet.dll"
# Setup session options
$sessionOptions = New-Object WinSCP.SessionOptions
$sessionOptions.Protocol = [WinSCP.Protocol]::Ftp
$sessionOptions.HostName = "ftp.example.com"
$sessionOptions.UserName = "user"
$sessionOptions.Password = "password"
$session = New-Object WinSCP.Session
# Connect
$session.Open($sessionOptions)
# Calculate previous weekday
$d = Get-Date
$d = $d.AddDays(-1)
if ($d.DayOfWeek -eq "Sunday")
{
$d = $d.AddDays(-1)
}
if ($d.DayOfWeek -eq "Saturday")
{
$d = $d.AddDays(-1)
}
$timestamp = $d.ToString("yyyyMMdd")
# Upload the file
$session.PutFiles("C:\source\path\prefix$timestamp.txt", "/remote/path/").Check()
# Disconnect, clean up
$session.Dispose()
See also:
The second link shows, how to calculate the previous weekday for use in a batch file (although still using PowerShell), if you want to keep using batch file.
Upvotes: 1