Search code examples
batch-filesftpwinscp

Use WinSCP to upload from a folder with a fiscal year in its name to an SFTP server


I am currently running a batch file that contains a WinSCP command line that uploads a file from a child folder to an SFTP server. The command is scripted as such:

put ""C:\Finance\FY 2021\%%TIMESTAMP#yyyymmdd%%\file.txt"" ^

The problem is, the parent folder (FY 2021) changes every October 1. So I currently will set a task to manually modify the path to FY 2022, FY 2023 and so on, at the end of the day on September 30. Is there a way to script the command so this change could be automated? Change meaning the target directory path. Thanks!


Solution

  • You can make use of future timestamp syntax of WinSCP. There are 61 days to the end of the year after October 1. So if you add 62 days to a timestamp, the calendar year of the resulting timestamp will be equal to the fiscal year of the original timestamp.

    With that knowledge, the following will produce the fiscal year: %TIMESTAMP+62D#yyyy%

    In your batch file, you can use it like this:

      "put ""C:\Finance\FY %%TIMESTAMP+62D#yyyy%%\%%TIMESTAMP#yyyymmdd%%\file.txt"" ^