Check if any type of files exist in a directory using BATCH script
114,733
Solution 1
To check if a folder contains at least one file
>nul 2>nul dir /a-d "folderName\*" && (echo Files exist) || (echo No file found)
To check if a folder or any of its descendents contain at least one file
>nul 2>nul dir /a-d /s "folderName\*" && (echo Files exist) || (echo No file found)
To check if a folder contains at least one file or folder.
Note addition of /a
option to enable finding of hidden and system files/folders.
dir /b /a "folderName\*" | >nul findstr "^" && (echo Files and/or Folders exist) || (echo No File or Folder found)
To check if a folder contains at least one folder
dir /b /ad "folderName\*" | >nul findstr "^" && (echo Folders exist) || (echo No folder found)
Solution 2
For files in a directory, you can use things like:
if exist *.csv echo "csv file found"
or
if not exist *.csv goto nofile
Solution 3
You can use this
@echo off
for /F %%i in ('dir /b "c:\test directory\*.*"') do (
echo Folder is NON empty
goto :EOF
)
echo Folder is empty or does not exist
Taken from here.
That should do what you need.
Author by
psycho
Updated on January 28, 2020Comments
-
psycho over 4 years
Hello I'm looking to write a batch file to check to see if there are any files of any type inside a given folder.
So far I've tried the following
if EXIST FOLDERNAME\\*.* ( echo Files Exist ) ELSE ( echo "Empty" )
I can get it to work if I know the file extension such as a txt file with the follwing
if EXIST FOLDERNAME\\*.txt ( echo Files Exist ) ELSE ( echo "Empty" )
Thank you for your help
-
Shawson over 10 yearsWhat is this witch craft??!
-
kitcar2000 over 9 yearsWhy are you putting the redirection before the command? Is there a difference between
>nul 2>nul command
andcommand >nul 2>nul
? -
dbenham over 9 years@kitcar2000 - It makes no difference what-so-ever. The redirection can appear at the beginning, at the end, or even in the middle of the command!
-
dgo over 8 yearsThis is only helpful if there could only be a very limited subset of filetypes; and you know that for sure; and you know what those are. This is a very limited solution.
-
dbenham over 6 yearsAn extension of
.csv
does not guarantee that the entry is a file. There is nothing to stop you from creating a directory with a.csv
, or any other extension. -
TripeHound almost 5 yearsOP explicitly states they don't want to test for a specific extension but for any file.
-
TripeHound almost 5 years@kitcar2000 There can be subtle differences, depending on the command. For example,
>file echo 1 2
will send1 2
and a CR-LF tofile
.echo 1 2>file
will not, because2>
is seen as "redirect stderr" andecho 1 2 >file
will "work", but add a possibly unwanted space. -
englebart over 4 yearsWorks great. I changed the ending to look like ... && (SET FOUND_FILE=1) || (SET FOUND_FILE=0) So I could use it in an IF statement later
-
Stephan over 4 years
if {%~1}=={}
will fail, when%~1
has any spaces. Better use quotes:if "%~1"==""
to avoid syntax errors. -
jeb over 4 yearsFails when files begin with a semi colon
; Semi.txt
and it count also directories not only files -
Ste over 4 yearsFixed. The accepted answer also has a problem when files contain
;
at the start. How can this be fixed? Thedir
command doesn't even list a file in this case. -
Ste over 4 yearsFixed that issue. The history of the answer will show the fix. Thanks for pointing this out.
-
jeb over 4 yearsI can't see why the answer from dbenham should have any problems, because the problem only occurs with
FOR /F
. Btw. I don't understand your stuff>nul 2>nul dir /a-d "!%~1!\*" &&
now you are counting only files in subdirectories when there is at least one file in the base directory. Why do you need|| set /a count+0
? -
Ste over 4 yearsThat was part of the accepted answer. I used it to check if any files existed otherwise I got an Files not found message. ../now you are counting only files in subdirectories when there is at least one file have you tried it because it works for me. If the base directory has
0
it returns that and1
,2
,3
etc when it has that amount. -
Ste over 4 yearsIt was the other way around. If the base directory had
0
files it was ok. But if there was one folder in that withx
amount of files, then it failed. I've added the/s
switch to the>nul 2>nul dir /a-d
part to fix this. I was wrong about the accepted answer having the;
issue. I don't know what I tried then.