1

I am using the following command to transfer from a SFTP location to a local folder. This transfers all the files from the SFTP location to the local folder.

How do I transfer files that are only for e.g. older than yesterday?

cmd /c c:/putty/pscp -q -batch -pw password -i C:/putty/key_pk.ppk -r root@xx.xxx.xxx.xxx:/home/user/Folder1/* C:/LocalFolder1/SFTP/

Thanks.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
VN Khan
  • 21
  • 7

1 Answers1

0

The pscp cannot do that.

All you can do, is to the list all the files using the -ls switch, parse the output to find the old files and generate a download script for the identified files.


Or use an SCP/SFTP client capable of selecting files by their timestamp.

For example with WinSCP SFTP/SCP client, you can use the following batch file (.bat) to download the files older than one day:

winscp.com /log=c:\path\to\winscp.log /command ^
    "open sftp://root:password@xx.xxx.xxx.xxx/ -privatekey=""C:\putty\key_pk.ppk"" -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...=""" ^
    "get -filemask=<1D /home/user/Folder1/* C:\LocalFolder1\SFTP\" ^
    "exit"

References:

(I'm the author of WinSCP)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Thanks, this helped. However, I was trying to get a file modified today and if I do it like the following, it gets all the file not just the ones updated/modified today. Why is that? Is my syntax wrong? `winscp.com /log=c:\path\to\winscp.log /command ^ "open sftp://root:password@xx.xxx.xxx.xxx/ -privatekey=""C:\putty\key_pk.ppk"" -hostkey=""ssh-rsa 2048 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx""" ^ "get -filemask*>=%TIMESTAMP#yyyy-mm-dd% /home/user/Folder1/* C:\LocalFolder1\SFTP\" ^ "exit" ` – VN Khan Jan 21 '16 at 05:59
  • You are missing an equal sign after the `-filemask`. It should be: `-filemask=*>=%TIMESTAMP#yyyy-mm-dd%` (and the `*` can be omitted, as it is in my answer) – Martin Prikryl Jan 21 '16 at 07:09
  • I tried that as well. But the script gets all the files on the SFTP location (I have 3 files; 19 Jan, 20 Jan and 21 Jan). Please see the output below: ` get -filemask=*>= /home/user1/folder1/* C:\localfolder1\ ` – VN Khan Jan 21 '16 at 07:26
  • OK, you run WinSCP from a batch file, where the `%` has a special meaning (actually the same as in WinSCP script). You have to double them to preserve them for WinSCP: `-filemask=*>=%%TIMESTAMP#yyyy-mm-dd%%` See http://winscp.net/eng/docs/scripting#timestamp *To use `%TIMESTAMP%` on command-line in a batch file, you need to escape the `%` by doubling it to `%%TIMESTAMP%%`, to avoid batch file interpreter trying to resolve the variable.* – Martin Prikryl Jan 21 '16 at 07:53