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

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


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 "destin\Filename_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 off
if \{%1\}

\{\} goto syntax if \{%2\}

\{\} goto syntax if \{%3\}

\{\} 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"

"File(s)" @set presize=%%g :moveloop sleep 61 for /f "Skip=4 Tokens=2,3" %%f in ('dir /w %file%') do if /i "%%f"=="File(s)" @set newsize=%%g if not "%newsize%" EQU "%presize%" goto :moveloop for /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 univdate set append=%yy%%mm%%dd%%append% move %file% "%destin%\%filename%_%append%%extn%" call %routine% "%destin%\%filename%_%append%%extn%" goto loop :syntax @echo FTPWait source destin routine endlocal goto :EOF :parse set filename=%~n1 set extn=%~x1
NOTE: If Sleep is not available, use the sleep alternative.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.