I have a setup where there is a watch folder, and folders with files are placed in it for processing. The app is great at recursively processing files in any subfolders, and moving the processed files to a final location, but it leaves behind the empty directories.

I wish to run a cmd prompt command/batch to scan these folders and then do something (echo output or create a txt file) if they are empty so we are aware that the processing has finished and emptied the folders, without looking inside every one.

Things I have tried:


if not exist "Folder\*.file" echo "not"

This almost works exactly as I want, but only works when the files are directly in that folder, and there are no subfolders. Quite a lot of the folders being processed only have files in several subfolders, and so this fails the test in that instance.

Next I've modified a script from here that accepts the folder name as parameter:

echo off
echo 1: %1
setlocal enableDelayedExpansion
for /f %%i in ('dir /a:d /b /s D:\watch_folder\%1') do (
  set _dir=%%i
    echo _dir
  if exist !_dir!\*.file (
    echo .files exist here:  !_dir!
    ) else (
    echo No .files Here: !_dir!

This again, almost works as expected - it will check subfolders and report back if they have the files in them or not, however if the files are in the folder itself, and not subfolders, it doesn't even check it, it only checks subfolders.

I then noticed another problem with the script - if there are two subfolders, one with no special .files and one with, it reports one as being empty and so would trigger the 'not exist' status

So I tried just scanning the watch_folder itself (dir /a:d /b /s D:\watch_folder) but this then scans every folder, which I don't really want it to do.

tl;dr Problem:
I want to recursively check a folder for files with a certain extension, that may or may not be in (a) randomly named subfolder(s) and get one result (not a list) as to whether any files that match the criteria exist within that tree, using this result I want to create a text file named as the original folder name such that:


Restrictions: command line/batch is prefereable as the app is restricted to using comspec

EDIT: Just had a thought - could I do a tree listing and somehow search that for the file extension (I'm imagining a situation similar to grepping the output in linux)

tree /F Folder | find /c ".file"

This brings back a count of all files matching that anywhere in the tree - How can push this output into an "if 0" type statement ?

I'm trying this at the moment:

for /f "delims=" %%a in ('tree /F D:\watchfolder\%1 | find /c ".mov"') do @set result=%%a
echo %result%

but it doesn't like the fact I've pipe tree into find

  • not sure if I should just delete this question now...
    – Smock
    Jun 19, 2019 at 14:41

1 Answer 1


Finally figured it out thanks to this post! (for the ^| )

@echo off
for /f "delims=" %%a in ('tree /F D:\watch_folder\%1 ^| find /c ".mov"') do @set result=%%a
if %result% == 0 (
    echo Finished > D:\watch_folder\%1.txt
) else (
    echo LOTS %result%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .