Wednesday, April 04, 2012

Using WinSCP in DOS Batch file to copy files from Unix Host to Windows Server

 

Here is the sample script I use to copy from Unix Host to Windows Host by DOS Batch will using WinSCP.

Main Script

@echo off

del .\WinSCP_Copy.log /Q

cd "D:\Documents and Settings\Siva\My Documents\SQL_Pack\WinSCP\D1_Copy.txt" /log="D:\Documents and Settings\Siva\My Documents\SQL_Pack\WinSCP\"
cd D:

call UnixBox_Copy.bat

"C:\Program Files\WinSCP\WinSCP.exe" /console /script="D:\Documents and Settings\Siva\My Documents\SQL_Pack\WinSCP\UnixBox1_Copy_Script.txt" /log="D:\Documents and Settings\Siva\My Documents\SQL_Pack\WinSCP\WinSCP_Copy.log"

@echo on


UnixBox_Copy.bat

set lan_dir=D:\Documents and Settings\Siva\My Documents\SQL_Pack\WinSCP
set unix_dir1=Oracle_DB_Build
set unix_dir2=Build_SubDir
set uname=DBUser1
set pwd=1sJ8*&^!

del .\UnixBox1_Copy_Script.txt  /Q
 

:: To execute the script "C:\Program Files\WinSCP\WinSCP.exe" /console /script="D:\Documents and Settings\Siva\My Documents\SQL_Pack\WinSCP\D1_Copy.txt"

:: To switch to batch mode &  Automatically abort script on errors

echo option batch abort                                              > .\UnixBox1_Copy_Script.txt                                                                 

:: Disable overwrite confirmations that conflict with the previous

echo option confirm off                                              >> .\UnixBox1_Copy_Script.txt                                                                

:: connect using a password

echo open sftp://%uname%:%pwd%@unixDBsrvr1.production.siva.com      >> .\UnixBox1_Copy_Script.txt

                                                      
:: Change Local Directory

cd "%lan_dir%"

IF NOT EXIST "%unix_dir2%"  (mkdir "%lan_dir%\"%unix_dir2% ) ELSE ( GOTO _END )

echo lcd "%lan_dir%\%unix_dir2%" >> .\UnixBox1_Copy_Script.txt

:: Force binary mode transfer
echo option transfer binary                                              >> .\UnixBox1_Copy_Script.txt

:: Download the file to local directory Y:\
echo get /app/oracle/Build_PRD/%unix_dir1%/%unix_dir2%/*.* -append       >> .\UnixBox1_Copy_Script.txt

:: Disconnect
echo close                                                                    >> .\UnixBox1_Copy_Script.txt
:: Exit WinSCP
echo exit                                                                     >> .\UnixBox1_Copy_Script.txt

:: Exit WinSCP
echo exit                                                                     >> .\UnixBox1_Copy_Script.txt

:_END
echo *** W A R N I N I N G *** Floder already exists
pause ..

Sample UnixBox1_Copy_Script.txt, generated by  UnixBox_Copy.bat. The following Script is called on WinSCP as set of command to execute on WinSCP console.

option batch abort                                                                                                               
option confirm off                                                                                                              
open sftp://DBUser1:1sJ8*&^!@unixDBsrvr1.production.siva.com    
lcd "D:\Documents and Settings\Siva\My Documents\SQL_Pack\WinSCP\Build_SubDir"
option transfer binary                                             
get /app/oracle/Build_PRD/Oracle_DB_Build/Build_SubDir/*.* –append    
close                                                                   
exit                                                                    

No comments: