2

I have a query regarding the putty batch file. This is my .bat file:

C:
cd Program Files (x86)\PuTTY
pscp -2 -v -pw khair1 -sftp  abc@****.na.ab.com:/qwe/asd/tryu/*.csv.zip P:\Projects\abc\Test_bacth\Batch_download
pause"

So every week I have to give file name like /qwe/asd/tryu/**04242016***.csv.zip

How can I dynamically get all the file which is last modified.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Ashique Sheikh
  • 155
  • 2
  • 4
  • 13
  • There is a good description of doing this with UNIX/Linux commands at http://superuser.com/questions/524974/is-it-possible-with-scp-to-only-copy-files-that-match-a-certain-date If you can install http://www.cygwin.com/ it would probably work well. – lit Apr 26 '16 at 00:03
  • If you are permitted to install PuTTY, then you are probably permitted to install WinSCP. – lit Apr 26 '16 at 00:24

1 Answers1

2

You can use these commands to generate the today's stamp:

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

See How do I get current datetime on the Windows command line, in a suitable format for using in a filename?


Or use some more powerful SFTP/SCP client.

For example with WinSCP scripting, you can do:

"C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^
    "open sftp://abc:password@****.na.ab.com/ -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...=""" ^
    "get /qwe/asd/tryu/%%TIMESTAMP#yyyymmdd%%*.csv.zip ""P:\Projects\abc\Test_bacth\Batch download\""" ^
    "exit"

See documentation for the %TIMESTAMP% syntax.


If the timestamp is actually not today's, instead of specifying the timestamp, just download the latest file for each pattern/mask.

It's easy with WinSCP, just use the -latest switch:

"C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^
    "open sftp://abc:password@****.na.ab.com/ -hostkey=""ssh-rsa 2048 xxxxxxxxxxx...=""" ^
    "lcd ""P:\Projects\abc\Test_bacth\Batch download""" ^
    "cd /qwe/asd/tryu" ^
    "get -latest *_cpg_aob_detail.csv.zip" ^
    "get -latest *_fmcg_cob_detail.csv.zip" ^
    ...
    "exit"

See also other options for downloading the most recent files.

(I'm the author of WinSCP)

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • This runs in my system. get /qwe/asd/tryu/%%TIMESTAMP#yyyymmdd%%*.csv.zip. But any idea how to subtract 1 day from it. – Ashique Sheikh Apr 26 '16 at 08:17
  • Fixed 4 files. Which have name pattern like 04262016abc,04262016def,04262016ghi,04262016jkl. – Ashique Sheikh Apr 26 '16 at 08:32
  • no. it is like time stamp of the day like 04242016173153_cpg_aob_detail.csv second file like 04242016173655_fmcg_cob_detail.csv. – Ashique Sheikh Apr 26 '16 at 09:28
  • But it have other date folders also like 04172016173153_cpg_aob_detail.csv and 04112016173655_fmcg_cob_detail.csv etc – Ashique Sheikh Apr 26 '16 at 10:16
  • Its stopped here...Authenticated. Starting the session... Session started. Active session: [1] abc@****.na.ab.com P:\Projects\abc\Test_bacth\Batch_download /qwe/asd/tryu winscp> – Ashique Sheikh Apr 26 '16 at 12:09
  • Error :- Unknown switch 'latest'. – Ashique Sheikh Apr 26 '16 at 12:17
  • Thanks . Thank you so much it works. have 2 query more first is how to pass password to it and second can we unzip all 4 folders in a particular folder. Thanks again – Ashique Sheikh Apr 26 '16 at 12:29
  • Password: see my edited answer. Unzip: Do not see how's that related. You should probably ask a new question. – Martin Prikryl Apr 26 '16 at 12:34
  • http://stackoverflow.com/questions/36865582/how-to-unzip-the-folder-after-downloaded-from-winscp – Ashique Sheikh Apr 26 '16 at 12:55
  • How can i run this code when it have space in it.see the download file path "C:\Program Files (x86)\WinSCP\WinSCP.com" /command ^ "open sftp://abc:password@****.na.ab.com/ -hostkey=""ssh-dss 1024 xx:xx:xx:...""" ^ "lcd \\fastrg01\Olg\DFF abc(25-kml-0234)\VM\ZIP_INPUT" ^ "cd /qwe/asd/tryu" ^ "get -latest *_cpg_aob_detail.csv.zip" ^ "get -latest *_fmcg_cob_detail.csv.zip" ^ ... "exit" – Ashique Sheikh Apr 29 '16 at 12:56
  • The same way as with the space in the `-hostkey`, enclose the path with double double-quotes. See my updated answer. And also https://winscp.net/eng/docs/commandline#syntax – Martin Prikryl Apr 29 '16 at 14:53
  • Hi Martin Got stuck in again. This week the client did not put the file in that path so it took the last week file as latest. Can we make it dynamic so that it can only picks the latest monday date file. the pattern for monday date is like 06262016173113_cpg_aob_detail.csv.zip and 06262016173113_fmcg_cob_detail.csv.zip. Can we do something of it. – Ashique Sheikh Jun 30 '16 at 09:16
  • This is not a discussion forum. If you have a question, [ask it](http://stackoverflow.com/questions/ask). – Martin Prikryl Jun 30 '16 at 09:19