2

I like to use date variables in a PuTTY/PSFTP batch-script to select files with a certain timestamp in the filename.

But I couldn't find anything in the docs. Is there a command for PuTTY/PSFTP batch-script to use date-variables or is there a way to pass Windows Batch Variables to PSFTP?

The file looks like this: FILENAME_2015-06-25.TXT (based on the current date)


Something like this:

cd /subdir
get FILENAME_%year%-%month%-%day%.TXT
quit

In a Windows Batch File i can easily get the current date by something like this:

SET YEAR=%DATE:~-4%
SET MONTH=%DATE:~-7,2%
SET DAY=%DATE:~-10,2%

ECHO FILENAME_%YEAR%-%MONTH%-%DAY%.TXT
Sven Rojek
  • 5,476
  • 2
  • 35
  • 57

1 Answers1

4

You have to generate the psftp script file dynamically like:

SET YEAR=%DATE:~-4%
SET MONTH=%DATE:~-7,2%
SET DAY=%DATE:~-10,2%

ECHO cd /subdir > script.txt
ECHO get FILENAME_%YEAR%-%MONTH%-%DAY%.TXT >> script.txt

psftp.exe -b script.txt ...

Though note that the %DATE% is not really a reliable approach to retrieving timestamp with a fixed format as its value changes with locale.

You can use this instead:

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if '.%%i.'=='.LocalDateTime.' set LDT=%%j
ECHO get FILENAME_%LDT:~0,4%-%LDT:~4,2%-%LDT:~6,2%.TXT >> script.txt

For details see:
How to get current datetime on Windows command line, in a suitable format for using in a filename?


Alternatively use WinSCP scripting (that supports timestamps natively) from a batch file (.bat) like:

winscp.com /log=winscp.log /command ^
    "open ftp://username:password@host" ^
    "cd /subdir" ^
    "get FILENAME_%%TIMESTAMP#yyyy-mm-dd%%.TXT" ^
    "exit"

For details see:

(I'm the author of WinSCP)

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992