Reputation: 1578
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
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
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
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
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