How to connect SFTP(Linux) from Windows DOS

I need to write a batch script for file transfer from SFTP to Windows system. SFTP is on Linux system. I kept this code in batch file and executing it.. but not working.. Even i tried from Command prompt like this "open sftp.host.com" but getting error. Can anyone help with the code and tell me how to connect SFTP from command prompt.

Here is my sample code,

open sftp.host.com
<username>
<password>
lcd <test/test/../>
cd /test/test/test/
get test.txt
quit

--
Thanks,
Mohan

You need an SFTP-compatible executable on the Windows side. You can try the psftp.exe that comes with PuTTY. You would store the SFTP commands in a file, say sftpcommands.txt, and use psftp.exe -b sftpcommands.txt.

I installed Putty with psftp.exe, and kept my scripts in sftpcommands.txt.
I went to command prompt and psftp.exe -b sftpcommands.txt, but it is i am getting this error "'pstfp.exe' is not recognized as an internal or external command, operable program or batch file".

If i open but psftp.exe, it is asking for hostname, login and password. Which is working fine. Can you tell me how to execute that above command which you gave.

--
Thanks,
Mohan Tummalapalli

You will either need to add it to your PATH, or specify the full path to the location of the psftp.exe, e.g. "C:\Program Files\PuTTY\psftp.exe" -b "C:\Documents and Settings\Mohan\My Documents\sftpcommands.txt".

Thanks it works. I can run my scripts using "C:\Program Files\PuTTY\psftp.exe" -b "C:\Documents and Settings\Mohan\My Documents\sftpcommands.txt".

I have another doubt it the same script, I need to rename file which iam getting from SFTP, i am not sure the file name which iam getting so iam using "" but as example iam sending this "ren Test.txt Test123.txt"

Can you tell me how to rename with date format something like test123[DDMMYYYY][time].

--
Thanks,
Mohan

Thsi isn't really a Unix related question, is it? :slight_smile:

Windows batch/command/cmd.exe scripting is one of the most ridiculous languages I have ever encountered. Anyway, something like this might work for you:

for /f "tokens=1-3 delims=/ " %d in ('date /t') do ren test*.txt test%d%e%f.txt

I should mention, you have to double the %s if you are putting this in a batch file, i.e.

for /f "tokens=1-3 delims=/ " %%d in ('date /t') do ren test*.txt test%%d%%e%%f.txt

I'm not sure how you would include the time in the filename as well (I forgot about that part). Maybe it would be best to install Cygwin and get a real scripting environment!

Hi,

I am getting this error "psftp: unknown command "for"" if i use for /f "tokens=1-3 delims=/ " %%d in ('date /t') do ren test*.txt test%%d%%e%%f.txt

--
Thanks,
Mohan

Iam getting error "psftp: unknown command "for" when iam using for /f "tokens=1-3 delims=/ " %%d in ('date /t') do ren test*.txt test%%d%%e%%f.txt

--
Thanks,
Mohan

Mohan, am i late..?

ren Test*.txt "Test%Date:/= % %Time::=.%.txt"

--ilan

Mohan, am i late..?

ren Test*.txt "Test%Date:/= % %Time::=.%.txt"

--ilan

Nice tip ilan, I never knew you could do that kind of substitution in cmd.exe... do you know where that is documented because I can't see it anywhere?

Hi Annihilannic,

I always used this substitution in my scripts to write into error logs; that way I can nail down exactly... what time a particular occured. I got it somewhere online a long back... :slight_smile:

--ilan

Grab a copy of PERL from activestate. It is quite easy to automate when you have a real language to work with.

Can some please explain this command?

siquadri,

ren Test*.txt "Test%Date:/= % %Time::=.%.txt"

The above command is suggested to rename a file through a script with the current data and time; and is also an good illustration of character replace in batch scripts..

if you do a.. echo %Date:/= % it will replace : with a blank space

examples:

C:\Documents and Settings\ilango>echo %Date:/= %
Mon 04 06 2009

C:\Documents and Settings\ilango>echo %Date:/=;%
Mon 04;06;2009

C:\Documents and Settings\ilango>echo %Date:/=ilan%
Mon 04ilan06ilan2009

C:\Documents and Settings\ilango>echo %Date:/=*ilan*%
Mon 04*ilan*06*ilan*2009

/ilan