0

Need a help in transfer file to sftp server. We have a script with 3 stages.

Driver

ECHO off
path\file.sftp.sc.bat

Driver -file.sftp.sc.bat

@ECHO OFF

set PUTTY_HOME= LOCATION\ PUTTY
set PATH=%PUTTY_HOME%;%PATH%
REM ECHO %DOWNLOADDATA1%
set DOWNLOADDATA1="N"
IF EXIST LOCATION\"FileName.txt"  SET DOWNLOADDATA1="Y"
ECHO %DOWNLOADDATA1%
IF %DOWNLOADDATA1% =="N" goto unsucc
ECHO %DOWNLOADDATA1%
ECHO GETTING DATA FILE
psftp -password -b LOCATION\Summary_LOC_SC.ftp user name
ECHO %ERRORLEVEL%
IF %ERRORLEVEL% EQU 0 LOCATION\ Success_email.vbs
goto end
IF %ERRORLEVEL% NEQ 0 LOCATION\UNSuccess_Email.vbs
goto end
:UNSUCC
ECHO %DOWNLOADDATA1%
ECHO UNSUCCESSFUL
LOCATION\UNSuccess_FNF_Email.vbs
:END

Driver-Summary_LOC_SC.ftp

cd/fall2014
1cd LOCATION OF FILE
put "File.Txt"
quit

Now, the file name is going to be changed with date, I mean, today it will be file20141118.txt, tomorrow will be file20141119.txt.

In the above script how to implement it, so it will pull up the current file.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
user3657257
  • 37
  • 2
  • 9
  • Martin, In second driver, we have "FileName.txt" and in third driver we have "File.Txt", this is same (typo). So, we need to transfer this file right. But when the file generates , it will be generated adding current date. such as File20141110.txt. and next day it will be 20141112.txt. So my question was, how this script will know which file to pick, as the file name changes everyday. right. – user3657257 Nov 20 '14 at 13:12
  • I understand, so modify the batch file to generate the expected name (with today's date) according the question I've linked to above. – Martin Prikryl Nov 20 '14 at 13:15
  • We can't really use File.Txt in above script right as file name will be different today and tomorrow and so on. – user3657257 Nov 20 '14 at 13:15
  • Well, either you do not understand me or I do not understand you. I know that the file name is going to be different every day. The date in the name changes, right? So you can generate (or calculate) the actual name using the examples I've linked too. Or is there any other component (except for the timestamp) that will change each day? – Martin Prikryl Nov 20 '14 at 13:22
  • I have a script to modify the file based on current timestamp..I want to automate the process. So, first script will change the file name and script above will sftp the file to target location. but the script above wont work right as the file name is statIC. We are using File.txt in second script whereas file name is already changed to File20141120.txt – user3657257 Nov 20 '14 at 13:23
  • yesm you are right.only the date in the name changes. – user3657257 Nov 20 '14 at 13:25
  • OK, I hope I understand now (at least partially), see my answer. – Martin Prikryl Nov 20 '14 at 13:48

1 Answers1

3

If I understand the problem, you do not know how to modify the psftp script to cater for dynamic file name.

You can generate the script on the fly like:

...
ECHO GETTING DATA FILE
echo cd/fall2014 > LOCATION\Summary_LOC_SC.ftp
echo cd LOCATION OF FILE >> LOCATION\Summary_LOC_SC.ftp
echo put "File%date:~-4,4%%date:~-7,2%%date:~-10,2%.Txt" >> LOCATION\Summary_LOC_SC.ftp
echo quit >> LOCATION\Summary_LOC_SC.ftp
psftp -password -b LOCATION\Summary_LOC_SC.ftp user name
...

Note that the %date% magic above is locale specific. You may need to adjust it. Refer to How to get a UNIVERSAL Windows batch file timestamp and other similar questions here.

If you already have the renamed filename in some variable (what I understand from your comments, but do not see it in the code), it's easier, just use your variable instead of the %date%.

EDIT: Seeing your related question Batch file with current date, just use the finance variable from the answer:

echo put "%finance%" >> LOCATION\Summary_LOC_SC.ftp

Alternatively use WinSCP instead of the psftp.

It makes the code way easier thanks to:

Example:

...
ECHO GETTING DATA FILE
winscp.com /command ^
    "open sftp://username:password@host/" ^
    "cd /fall2014" ^
    "cd LOCATION OF FILE" ^
    "put ""File%%TIMESTAMP#yyyymmdd%%""" ^
    "exit"
...

References:
https://winscp.net/eng/docs/scripting#timestamp
https://winscp.net/eng/docs/guide_automation

(I'm the author of WinSCP)

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • 1
    Thanks Martin. You are correct. Really appreciate your help. Dynamic file name, thats the word.:) Got it now. Thanks. – user3657257 Nov 20 '14 at 14:33
  • Had no idea about it. :) Will do that from now onwards – user3657257 Nov 20 '14 at 15:41
  • Martin, have a question. As we are on windows server to use sftp we need to use Putty correct. To me the winscp code is simpler and would like to use that. Do we need to use Putty for this. – user3657257 Nov 20 '14 at 19:58
  • You can use any scriptable Windows SFTP client, including WinSCP and psftp. Choice is yours. You do not need PuTTY to make WinSCP working, it is standalone application (if that's the question). – Martin Prikryl Nov 20 '14 at 20:00
  • Martin. One more question. If the winscp code is standalone then do I need to portable the file to winscp.com? If yes, how can I do that – user3657257 Nov 21 '14 at 14:59
  • I do not know what you mean by "do I need to portable the file to winscp.com"? You need `winscp.exe` and typically also `winscp.com` (read about [WinSCP executables](http://winscp.net/eng/docs/executables)). It does not matter if you install them using "Installation package", or use ["Portable executables"](http://winscp.net/eng/docs/portable) package. – Martin Prikryl Nov 21 '14 at 15:01
  • Okay. So, basically after downloading the portable executable I can run the above script stand alone right. Sorry for bothering, pretty new in winscp and scripting :) – user3657257 Nov 21 '14 at 15:20
  • Correct. You just need to [verify the host key](http://winscp.net/eng/docs/scripting#hostkey) (as with psftp). – Martin Prikryl Nov 21 '14 at 15:23
  • Martin, back after sometime. Have a question on winscp command above.ECHO GETTING DATA FILE winscp.com /command ^ "open sftp://username:password@host/" ^ "cd /fall2014" ^ "cd LOCATION OF FILE" ^ "put ""File%%TIMESTAMP#yyyymmdd%%""" ^ "exit" Here winscp.com /command ^ (winscp is installed in C:\WINSCP folder. So shall I use, the location winscp is installed. It is giving error, command is not recognized.. – user3657257 Dec 09 '14 at 15:21
  • @user3657257 Yes, generally you should use a full path to `winscp.com`, unless you have it in `PATH`. – Martin Prikryl Dec 11 '14 at 14:35