echo off

rem Script: getrmanfiles.bat
rem Author: Theodore Zacharia
rem Version: 1.0
rem Date: 12/03/2010
rem A DOS (yuk) script to enable getting files off a UNIX server when we think
rem a problem / disconnection / timeout is highly likely and you need to re-start
rem at some known point.

set DESTDIR="d:\test\rman"
set RMTIP="10.96.xxx.xx"
set RMTUSER="oracle"
set RMTDIR="/u02/DONOTDELETE"
set FLIST="flist.txt"
set USEFLIST="n"
set MAXSESS=3
set CURSESS=0
set TOTSESS=0
echo 0 > sess.id

set /p PASSWORD="password "
cls

:CHECKRS
IF "%1%"=="" GOTO ALLFILES
set RESTART=%1%
set DOCP=0
echo Looking for a restart at %RESTART%
goto STARTJOB

:ALLFILES
set RESTART=""
set DOCP=1

rem If you are asking for a restart, you don't get the option to re-download the flist
IF NOT "%1%" == "" GOTO restart


rem First get list of files file, on remote server do
rem if one already exits, ask if to get
IF NOT EXIST %FLIST% GOTO getflist
set /p USEFLIST="The file %FLIST exists, shall we use this copy (y/n)"
IF "%USEFLIST%" == "n" GOTO getflist
goto STARTJOB


:getflist
echo "getting file from %RMTDIR%"
pscp -pw %PASSWORD% %RMTUSER%@%RMTIP%:%RMTDIR%/%FLIST% .
goto STARTJOB

:restart
echo "Restarting from file %1%"

rem Now process the files, restarting if necessary
:STARTJOB

for /f %%a IN ('type %FLIST%') do call :copyafile %%a 
goto end 

:copyafile
IF "%1"=="%RESTART%" set DOCP=1 
IF %DOCP%==1 GOTO docopy 
echo skipping %1 
goto end 

:docopy 
echo copying %1
echo "running %CURSESS% concurrent sessions"
:tryagain
call setsessid.bat 0
set CURSESS=%errorlevel%
IF "%CURSESS%" LSS "%MAXSESS%" GOTO donow
echo "Waiting for sessions %CURSESS% to complete"
ping 127.0.0.1 -n 5 -w 1000 > null
goto tryagain
:donow
set /a TOTSESS=%TOTSESS%+1
echo "now running %CURSESS% concurrent of a total %TOTSESS% sessions"
call setsessid.bat 1
set CURSESS=%errorlevel%
start "session %CURSESS%" docpnow.bat %PASSWORD% %RMTUSER% %RMTIP% %RMTDIR% %1 %DESTDIR%
echo "back from call for %1" >> getfiles.log
goto end 


goto end



:end