Svein Terje Gaup
Svein Terje Gaup

Reputation: 1578

Check if a directory contains only subdirectories and no files

I have a directory which has subdirectories like this:

C:\FILES
C:\FILES\DONE

I want to check that FILES contains no files:

if not exist C:\FILES\*.* (
  echo No files to process.
  exit /b 
)

But the test "not exist ..." is never true because of the subdirectory.

Any ideas on how to check if the directory has any files that are not directories?

Upvotes: 0

Views: 1165

Answers (5)

Acca Emme
Acca Emme

Reputation: 376

If you want to check if exists sub-directories on this level:

(DIR /D "C:\FILES\" | findstr /i /p /r /c:"2 Directory" > nul 2> nul) && echo "No directories except current and parent."

This simple inline command could be easy adapted for any situation ;-)

Upvotes: 0

Aacini
Aacini

Reputation: 67296

Only when there are files, the for execute the goto.

for %%a in (C:\FILES\*.*) do goto files
echo No files to process.
exit /b

Upvotes: 1

Script_Coded
Script_Coded

Reputation: 710

Simple code:

for /f "delims=|" %%f in ('dir /b c:\FILES\') do goto :files
echo No files found
pause
goto:eof
:files
echo Files found
pause
goto:eof

Upvotes: 0

MC ND
MC ND

Reputation: 70961

Try to get the list of files and if it fails, there are no files

dir /a-d "c:\files\*" >nul 2>nul || (
    echo No files to process
    exit /b
)

If there are no files, the dir /a-d command (the switch means exclude directories) will fail. || is a conditional execution operator that will execute the following commands if the previous one has failed.

Upvotes: 2

Alp
Alp

Reputation: 3105

Try

if not exist dir c:\FILES\*.* /a:-d (

You can find more on dir here

PS

I do not have DOS at my reach. So I can't test the above code snippet. But I think it is close to what you are looking for.If not something very close to that should work.

Upvotes: -1

Related Questions