I am trying to check if a folder contains files and I want to use
Code: Select all
if not exist * echo Folder is empty!
Moderator: DosItHelp
Code: Select all
if not exist * echo Folder is empty!
Code: Select all
if not exist * (echo Folder is not empty!) else (echo Folder is empty!)
renzlo wrote:Hi aleypuppy,
Try this:Code: Select all
if not exist * (echo Folder is not empty!) else (echo Folder is empty!)
-renzlo
Code: Select all
set "file="
for %%f in (*) do set file=1
if defined file (echo directory contains one or more files) else echo directory contains NO files
Code: Select all
set "dir="
for /d %%f in (*) do set dir=1
if defined dir (echo directory contains one or more folders) else echo directory contains NO folders
Code: Select all
set "file="
for %%f in (*) do set /a file+=1
dbenham wrote:I believe the if not exist * is seeing the implicit . folder (current directory) that exists in every folder, therefore it will never test true.
The FOR command can be used instead.
Use this to see if the current directory contains any files:Code: Select all
set "file="
for %%f in (*) do set file=1
if defined file (echo directory contains one or more files) else echo directory contains NO files
Use this to see if the current directory contains any foldersCode: Select all
set "dir="
for /d %%f in (*) do set dir=1
if defined dir (echo directory contains one or more folders) else echo directory contains NO folders
Of course you could do something likeif you wanted to get a count of the number of files. File will still be undefined if no files exist, otherwise it will contain the count of files. Or you could initialize file to 0 if you always want a value.Code: Select all
set "file="
for %%f in (*) do set /a file+=1
I'm not sure what will happen if the directory contains only hidden or system files or directories. I'm not sure if the FOR command will see them.
Dave Benham
Use this to see if the current directory contains any files:
...
Use this to see if the current directory contains any folders
...
Code: Select all
set "empty=true"
for /f %%a in ('dir /b/x') do set "empty=false"
[off]
Code:
IF NOT EXIST C:\MyDirectory\NUL ECHO It's FILE but _NOT_ FOLDER
[/off]