JSI Tip 4668. General purpose routine to process randomly received files.

Jerold Schulman

January 9, 2002

2 Min Read
ITPro Today logo in a gray background | ITPro Today


If you receive unsolicited files, as in multiple sites FTP to you, I have scripted a general purpose routine that will allow you to process them.

FTPWait.bat performs the following functions:

 - Detects when a new file has started to arrive in the source folder. - Detects when the FTP download completes, by assuming it is finished if the file size doesn't change in 61 seconds. - Move the file, Filename.extension, to a destination folder as Filename_YYYYMMDDHHMM.extension - Call your routine to process the file. - Upon return from your routine, process the next file or wait for one.

The syntax for using FTPWait.bat is:

FTPWait source destin routine

where:

source is the folder that receives the files from FTP.

destin is the folder where the uniquely renamed source is moved to.

routine is your processing program or batch, which is invoked using call routine "destinFilename_YYYYMMDDHHMM.extension".

FTPWait source destin routine should be scheduled as a background task, that runs When my computer starts.

NOTE: FTPWait.bat uses UnivDate.bat, the universal date parser.

FTPWait.bat contains:

@echo offsetlocalif {%1}
<h1><a name="_goto_syntax_if_2_" id="_goto_syntax_if_2_">{} goto syntaxif {%2}</a></h1>
{} goto syntaxif {%3}
<h1><a name="_goto_syntax_if_not_exist_1_goto_syntax_if_not_exist_2_goto_syntax_if_not_exist_3_goto_syntax_pushd_1_set_destin_2_set_destin_destin_set_routine_3_loop_sleep_60_set_file_for_f_Tokens_a_in_dir_b_do_set_file_a_if_file_EQU_goto_loop_call_parse_file_for_f_Skip_4_Tokens_2_3_f_in_dir_w_file_do_if_i_f_" id="_goto_syntax_if_not_exist_1_goto_syntax_if_not_exist_2_goto_syntax_if_not_exist_3_goto_syntax_pushd_1_set_destin_2_set_destin_destin_set_routine_3_loop_sleep_60_set_file_for_f_Tokens_a_in_dir_b_do_set_file_a_if_file_EQU_goto_loop_call_parse_file_for_f_Skip_4_Tokens_2_3_f_in_dir_w_file_do_if_i_f_">{} goto syntaxif not exist %1 goto syntaxif not exist %2 goto syntaxif not exist %3 goto syntaxpushd %1set destin=%2set destin=%destin:"=%set routine=%3:loopsleep 60set file=""for /f "Tokens=*" %%a in ('dir /b') do set file="%%a"if %file% EQU "" goto loopcall :parse %file%for /f "Skip=4 Tokens=2,3" %%f in ('dir /w %file%') do if /i "%%f"</a></h1>
"File(s)" @set presize=%%g:moveloopsleep 61for /f "Skip=4 Tokens=2,3" %%f in ('dir /w %file%') do if /i "%%f"=="File(s)" @set newsize=%%gif not "%newsize%" EQU "%presize%" goto :moveloopfor /f "Tokens=*" %%f in ('time /t') do @set append=%%f#if "%append:~1,1%" EQU ":" set append=0%append%set append=%append::=%set append=%append: #=%set append=%append:#=%call univdateset append=%yy%%mm%%dd%%append%move %file% "%destin%%filename%_%append%%extn%"call %routine% "%destin%%filename%_%append%%extn%"goto loop:syntax@echo FTPWait source destin routineendlocalgoto :EOF:parseset filename=%~n1set extn=%~x1

NOTE: If Sleep is not available, use the sleep alternative.



Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like