Wednesday, May 23, 2012

A Simply Windows FTP Batch Script To Download Files From Multiple Different Folder To Your Multiple Different Local Folder


A few moments ago, i had a case at the office how to download reports from the ftp server from multiple locations with different folders and different dates. So we need a smart script to do that, and here is :

@echo off
SETLOCAL

REM #############################
REM Usage: UnduhFTP dd mm yyyy
REM Example: UnduhFTP 16 05 2012
REM %1=dd %2=mm %3=yyyy
REM #############################

set FTP_HOST=192.x.x.x
set FTP_USER=username
set FTP_TRANSFER_MODE=ascii

set FTP_REMOTE_DIR_A=/YourRemoteFolderA
set FTP_REMOTE_DIR_B=/YourRemoteFolderB

set FTP_LOCAL_DIR_A=D:\YourFolderA
set FTP_LOCAL_DIR_B=D:\YourFolderB



REM Prompt for ftp password
set /P FTP_PASSWD=Enter your password please:

echo .
echo =================
echo   Date : %1-%2-%3
echo =================
echo Note: To break the current process, press CTRL+C.
echo .

set SCRIPT_FILE=%TEMP%\ftp.txt
(
    echo %FTP_USER%
    echo %FTP_PASSWD%
    echo %FTP_TRANSFER_MODE%  

REM Destination Folder List A
for /f "tokens=1 delims=" %%G in (ListA.txt) do (  
echo lcd %FTP_LOCAL_DIR_A%\%3\%%G\%2-%3
echo cd %FTP_REMOTE_DIR_A%/%%G/
echo get %%G-%2%1%3.exe
    )

REM Destination Folder List B
for /f "tokens=1 delims=" %%G in (ListB.txt) do (  
echo lcd %FTP_LOCAL_DIR_B%\%3\%%G\%2-%3
echo cd %FTP_REMOTE_DIR_B%/%%G/
echo get %%G-%2%1%3.exe
    )

echo bye
) > %SCRIPT_FILE%

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

ENDLOCAL

Descryption:
File ListA.txt anda ListB.txt are the file contains list of local destination folder where the downloaded file will be saved in. And then It have child folders which named or divided into mm-yyyy format.

Example:
File ListA.txt contain like this:
BRANCH_010
BRANCH_020
BRANCH_030
BRANCH_040
BRANCH_050

File ListB.txt contain like this:
HEADOFFICE_A
HEADOFFICE_B
HEADOFFICE_C
HEADOFFICE_D
HEADOFFICE_E

The files you download will be saved in each destination folder according to the list and into the child folder with mm-yyyy format name.
Example for my local destination folder is: D:\YourFolderA\BRANCH_010\05-2012

How To Use
Usage: UnduhFTP dd mm yyyy
example: UnduhFTP 21 05 2012

Hope this usefull.

1 Comments:

Wheelchair said...

Hey, very nice site. I came across this on Google, and I am stoked that I did. I will definitely be coming back here more often. Wish I could add to the conversation and bring a bit more to the table, but am just taking in as much info as I can at the moment. Thanks for sharing.
S-Ergo 105 Wheelchair

Keep Posting:)