Topic "Automate --> copy new file SFTP to local machine and copy file from local machine to different SFTP"

Author Message
skumarlingam
[View user's profile]

Joined: 2016-10-12
Posts: 5
Location: NC
Hi experts please guide me how do i need to put below steps in one batch script

Here is the scenario what I am trying to make my code to work.

1. Get a new file(.xml) from SFTP to local system (new file will generate every 15 minutes)

2. While copying a new file(.xml) for every 15 minutes from SFTP to local machine the file name and extension should change to (SDF123.UVW.RFTI.YYYYMMDD.HH.01) and extension(bkp)

example:
Quote:
UVW_TNI_09_CHAR_E_MM_DD_YYYY.xml (Source filename on sftp) --> SDF123.UVW.RFTI.YYYYMMDD.HHMM.01.bkp (renamed file name in local machine)



Code:
# Connect to SFTP server using password
open sftp://xyz:P@ssw0rd;fingerprint=ssa-xyz-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00@ftp.fg.com/usefor/fordownloadfiles/

# Download file
get UVW_TNI_09_CHAR_E_MM_DD_YYYY.XML D:\Shares\SDF123.UVW.RFTI.YYYYMMDD.HHMM.01.bkp

# Exit WinSCP
exit




3.From Destination folder(local machine) the renamed file(SDF123.UVW.RFTI.YYYYMMDD.HHMM.01.bkp) have to move to sub-folder in same local machine.

Code:
set mytime=.0
if /I %time:~0,2% gtr 09 set mytime=%date:~10%_%date:~4,2%%date:~7,2%_%time:~0,2%%time:~3,2%
if /I %time:~0,2% lss 10 set mytime=%date:~10%_%date:~4,2%%date:~7,2%_0%time:~1,1%%time:~3,2%

COPY D:\Shares\SDF123.UVW.RFTI.YYYYMMDD.HHMM.01.bkp    D:\Shares\SIDS\SDF123.UVW.RFTI.%mytime%.bkp


(Note: I was trying to accomplish format YYYYMMDD.HHMM using set mytime code, somehow I am getting different format)


4.From Destination folder(local machine) the renamed file (SDF123.UVW.RFTI.YYYYMMDD.HHMM.01.bkp) have to move to different SFTP site.


Code:
# Connect to SFTP server using a password
open sftp://FRANK:P@ssw0rd;fingerprint=FRA-UVW-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00-00@ftp.gg.com/useme/foruploadfiles/

# Upload file

put D:\Shares\SDF123.UVW.RFTI.YYYYMMDD.HH.01.bkp /useme/foruploadfiles/ -nopreservetime
exit



currently I am calling four different batch scripts in one separate batch script

Code:
CALL D:\Shares\Hold\BatScr\GETfileandrename.bat  (Covers 1 and 2 task)
timeout /t 30
CALL D:\Shares\Hold\BatScr\Copyfile.bat  (covers task 3)
timeout /t 30
CALL D:\Shares\Hold\BatScr\PUTfile.bat
(covers task 4)

YYYYMMDD.HHMM I am changing now manually now since script failing, somewhere my code is wrong in Task3.

Please guide me.

Thank you in advance.
martin
[View user's profile]
Site Admin
Joined: 2002-12-10
Posts: 26572
Location: Prague, Czechia
Use %TIMESTAMP% syntax
https://winscp.net/eng/docs/scripting#timestamp

get UVW_TNI_09_CHAR_E_MM_DD_YYYY.XML D:\Shares\SDF123.UVW.RFTI.%TIMESTAMP#yyyymmdd.hhnn%.01.bkp

You can use it even in batch file to generate the timestamp for your batch file commands:
https://winscp.net/eng/docs/script_formatting_timestamp_batch_file
skumarlingam
[View user's profile]

Joined: 2016-10-12
Posts: 5
Location: NC
Thank you so much. I modified code, Its working now.
Advertisements

You can post new topics in this forum






Search

What is WinSCP?

It is award-winning SFTP client, SCP client, FTPS client and FTP client integrated into one software program for file transfer to FTP server or secure SFTP server. [More]

And it's free!

Donate

About donations

$9   $19   $49   $99

About donations

Recommend

WinSCP Privacy Policy

WinSCP License