Tuesday, May 29, 2012

Simply FTP Script With DateTime Manipulation

This is a new simply FTP Script related to my previous post that completed with date time manipulation argument.

@echo off
SETLOCAL

echo ################################################
echo  Source  : http://golekupo.blogspot.com
echo  Version : 1.0
echo  Syntax  : UnduhFTP dd mm yyyy flag
echo  Contoh  : UnduhFTP 16 05 2012 NOW
echo  Args    : %1=dd %2=mm %3=yyyy %4=Flag
echo            Flag Date: set with NOW or YESTERDAY
echo ################################################

set FTP_HOST=192.xxx.xxx.xxx
set FTP_USER=username
set FTP_PASSWD=password
set FTP_TRANSFER_MODE=ascii
set FTP_LOCAL_DIR_A=\\192.168.x.x\drive_d$\locationA
set FTP_LOCAL_DIR_B=\\192.168.x.x\drive_d$\locationB
set FILE_LIST_A=listA.txt
set FILE_LIST_B=listB.txt

REM Change the date to Date-1
set dd=%1
set mm=%2
set yyyy=%3
set flag=%4



REM Check Flag
if %flag%==NOW goto DOWNLOAD
if %flag%==YESTERDAY goto CHANGEDATE

echo Flag error, set with NOW or YESTERDAY
echo process aborted, please retry.
goto FINISH 

:CHANGEDATE
if %dd%==08 (
set dd=8 ) else (
if %dd%==09 (
set dd=9 ) )

if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )

set /A dd=%dd% - 1
set /A mm=%mm% + 0

if /I %dd% GTR 0 goto DONE
set /A mm=%mm% - 1
if /I %mm% GTR 0 goto ADJUSTDAY
set /A mm=12
set /A yyyy=%yyyy% - 1

:ADJUSTDAY
if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
if %mm%==12 goto SET31
goto ERROR

:SET31
set /A dd=31 + %dd%
goto DONE

:SET30
set /A dd=30 + %dd%
goto DONE

:LEAPCHK
set /A tt=%yyyy% %% 4
if not %tt%==0 goto SET28
set /A tt=%yyyy% %% 100
if not %tt%==0 goto SET29
set /A tt=%yyyy% %% 400
if %tt%==0 goto SET29

:SET28
set /A dd=28 + %dd%
goto DONE

:SET29
set /A dd=29 + %dd%

:DONE
if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%

REM set YESTERDAY=%dd%.%mm%.%yyyy%
REM echo.
REM echo YESTERDAY : %YESTERDAY%
REM echo.

:DOWNLOAD
echo.
echo =====================================
echo   Flag Date     : %Flag%
echo   Report Date : %dd%-%mm%-%yyyy%
echo =====================================
echo Note: To abort the current process, press CTRL+C 
echo.

set SCRIPT_FILE=%TEMP%\ftp.txt
(
    echo %FTP_USER%
    echo %FTP_PASSWD%
    echo %FTP_TRANSFER_MODE%   
   
    REM List A
    for /f "tokens=1 delims=" %%G in (%FILE_LIST_A%) do (   
        echo lcd %FTP_LOCAL_DIR_A%\%yyyy%\%%G\%mm%-%yyyy%
        echo cd /%%G/
        echo get %%G-%mm%%dd%%yyyy%.exe
    )
   
    REM List B
    for /f "tokens=1 delims=" %%G in (%FILE_LIST_B%) do (   
        echo lcd %FTP_LOCAL_DIR_B%\%yyyy%\%%G\%mm%-%yyyy%
        echo cd /%%G/       
        echo get %%G-%mm%%dd%%yyyy%.exe
    )
   
    echo bye
) > %SCRIPT_FILE%

ftp -s:%SCRIPT_FILE% %FTP_HOST%
del %SCRIPT_FILE%

echo.
echo Download files done.
echo.

:FINISH 
pause

ENDLOCAL

Happy scripting and Have Fun.

0 Comments: