Ever have a collection of files that you need to send off to something else for processing, but need to process the files one at a time waiting for the completion of each (signaled by a file’s existence because you have no hook to the return code of the originating process) before continuing with the next?
No? Just me? 🤣 Anyway, here’s a script that takes an array of files, copies them to a directory for a different app to process, waits for that process to end (by looking for the file with a done extension added) before continuing on with the next.
@echo off
setlocal enabledelayedexpansion
set IMPORTDIR=X:\App\Process\
rem List your files here (one per line)
set FILES[0]=X:\Data\File1.csv
set FILES[1]=X:\Data\File2.csv
set FILES[2]=X:\Data\File3.csv
rem Set the number of files
set NUMFILES=3
set SLEEP_SECONDS=5
for /L %%I in (0,1,%NUMFILES%-1) do (
  call :WAITLOOP "!FILES[%%I]!"
)
echo All files processed.
exit /b
:WAITLOOP
rem Get the base name (without extension) and directory
set "FULLFILE=%~1"
set "BASEDIR=%~dp1"
set "BASENAME=%~n1"
if "%BASENAME%"=="" (
  rem This is important so we actually terminate
  goto :EOF
)
rem Compose the .done filename
set "DONEFILE=%IMPORTDIR%%BASENAME%.csv.done"
if exist "%DONEFILE%" (
  echo Removing previous done file.
  del "%DONEFILE%"
)
copy "%FULLFILE%" "%IMPORTDIR%%BASENAME%.csv"
:LOOP
if exist "%DONEFILE%" (
  echo File found: %DONEFILE%
  goto :EOF
) else (
  echo Waiting for file: %DONEFILE%
  timeout /t %SLEEP_SECONDS% /nobreak >nul
  goto LOOP
)
Just update the details as needed, but the above is tested and works!
Please remember to subscribe to the newsletter or feed to stay up to date!Disclaimer: Thoughts and opinions are my own, and do not reflect the views of any employer, family member, friend, or anyone else. Some links may be affiliate links, but I don't link to anything I don't use myself.