Home > Enterprise >  Determine if folder have a file or not in batch
Determine if folder have a file or not in batch

Time:03-25

Simple thing I want to do but, I try to do this with no success

@echo off

:start
if not exist "input" mkdir input

if not exist input\* echo Please put a video in "input" folder. && pause && goto start 

echo there is a file in "input" folder.
pause

Any help to fix?

CodePudding user response:

try to list files and see if it finds some:

dir /b /a-d input\* >nul 2>&1 && echo there is a file || echo folder is empty

/a-d excludes subfolders (lists files only)
>nul 2>&1 discards the output of dir (we don't need it, just if it is successful or not)
&& acts as if previous command was successful (files were found) then,
|| means if it failed (no files found) then

(just to explain why if exist doesn't work: it finds . and .. (current folder and parent folder), which technically are files)

  • Related