Slightly different answer, but my search led here and hopefully this helps others.
My use case is I want to run an exe if the current folder has files in it. If the current folder is empty or contains only other folders, then I want nothing to happen.
I started with trying something like if exist *.???
since in my case, the files in that folder always have a three char extension... but this did not work because ? seems to match o or more, not 1 or more. It always evaluated true, even when the folder had no files in it (I assume because every folder contains folders named . and .. )
So I tried a different approach using dir. I found in my case findstr
had no bearing on the result (I believe because in my case dir /A-d /b
returns "file not found" if there are no files in the dir due to -D parameter on /A... so the findstr .
always returns true).
In my case, the statement below worked like a charm. It executes the command if there are files in the folder and does nothing if there are no files or only other folders in the folder.
cd c:\interesting_dir
dir /a-D /b>nul && (
c:\path\to\program1.exe
c:\path\to\program2.exe
)