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.

Share:
114,733
psycho
Author by

psycho

Updated on January 28, 2020

Comments

  • psycho
    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
    Shawson over 10 years
    What is this witch craft??!
  • kitcar2000
    kitcar2000 over 9 years
    Why are you putting the redirection before the command? Is there a difference between >nul 2>nul command and command >nul 2>nul?
  • dbenham
    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
    dgo over 8 years
    This 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
    dbenham over 6 years
    An 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
    TripeHound almost 5 years
    OP explicitly states they don't want to test for a specific extension but for any file.
  • TripeHound
    TripeHound almost 5 years
    @kitcar2000 There can be subtle differences, depending on the command. For example, >file echo 1 2 will send 1 2 and a CR-LF to file. echo 1 2>file will not, because 2> is seen as "redirect stderr" and echo 1 2 >file will "work", but add a possibly unwanted space.
  • englebart
    englebart over 4 years
    Works 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
    Stephan over 4 years
    if {%~1}=={} will fail, when %~1 has any spaces. Better use quotes: if "%~1"=="" to avoid syntax errors.
  • jeb
    jeb over 4 years
    Fails when files begin with a semi colon ; Semi.txt and it count also directories not only files
  • Ste
    Ste over 4 years
    Fixed. The accepted answer also has a problem when files contain ; at the start. How can this be fixed? The dir command doesn't even list a file in this case.
  • Ste
    Ste over 4 years
    Fixed that issue. The history of the answer will show the fix. Thanks for pointing this out.
  • jeb
    jeb over 4 years
    I 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
    Ste over 4 years
    That 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 and 1, 2, 3 etc when it has that amount.
  • Ste
    Ste over 4 years
    It was the other way around. If the base directory had 0 files it was ok. But if there was one folder in that with x 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.