38

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

Bali C
  • 30,582
  • 35
  • 123
  • 152
psycho
  • 1,539
  • 4
  • 20
  • 36
  • possible duplicate of [Batch script: Search if a folder contains any files](http://stackoverflow.com/questions/3922200/batch-script-search-if-a-folder-contains-any-files) – Synetech Feb 08 '13 at 04:34

4 Answers4

56

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)
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • 31
    What is this witch craft??! – Shawson Jan 29 '14 at 14:02
  • Why are you putting the redirection before the command? Is there a difference between `>nul 2>nul command` and `command >nul 2>nul`? – kitcar2000 Aug 19 '14 at 19:02
  • 1
    @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! – dbenham Aug 19 '14 at 19:31
  • 1
    @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. – TripeHound Aug 09 '19 at 14:18
  • 1
    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 – englebart Dec 05 '19 at 15:10
13

For files in a directory, you can use things like:

if exist *.csv echo "csv file found"

or

if not exist *.csv goto nofile
Maikel
  • 163
  • 1
  • 2
  • 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. – dgo Feb 06 '16 at 10:03
  • 2
    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. – dbenham Sep 19 '17 at 23:29
  • 2
    OP explicitly states they don't want to test for a specific extension but for _any_ file. – TripeHound Aug 09 '19 at 14:15
7

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.

Bali C
  • 30,582
  • 35
  • 123
  • 152
0

A roll your own function.

Supports recursion or not with the 2nd switch.

Also, allow names of files with ; which the accepted answer fails to address although a great answer, this will get over that issue.

The idea was taken from https://ss64.com/nt/empty.html

Notes within code.

@echo off
title %~nx0
setlocal EnableDelayedExpansion
set dir=C:\Users\%username%\Desktop
title Echos folders and files in root directory...
call :FOLDER_FILE_CNT dir TRUE
echo !dir!
echo/ & pause & cls
::
:: FOLDER_FILE_CNT function by Ste
::
:: First Written:               2020.01.26
:: Posted on the thread here:   https://stackoverflow.com/q/10813943/8262102
:: Based on:                    https://ss64.com/nt/empty.html
::
:: Notes are that !%~1! will expand to the returned variable.
:: Syntax call: call :FOLDER_FILE_CNT "<path>" <BOOLEAN>
:: "<path>"   = Your path wrapped in quotes.
:: <BOOLEAN>  = Count files in sub directories (TRUE) or not (FALSE).
:: Returns a variable with a value of:
:: FALSE      = if directory doesn't exist.
:: 0-inf      = if there are files within the directory.
::
:FOLDER_FILE_CNT
if "%~1"=="" (
  echo Use this syntax: & echo call :FOLDER_FILE_CNT "<path>" ^<BOOLEAN^> & echo/ & goto :eof
  ) else if not "%~1"=="" (
  set count=0 & set msg= & set dirExists=
  if not exist "!%~1!" (set msg=FALSE)
  if exist "!%~1!" (
   if {%~2}=={TRUE} (
    >nul 2>nul dir /a-d /s "!%~1!\*" && (for /f "delims=;" %%A in ('dir "!%~1!" /a-d /b /s') do (set /a count+=1)) || (set /a count+=0)
    set msg=!count!
    )
   if {%~2}=={FALSE} (
    for /f "delims=;" %%A in ('dir "!%~1!" /a-d /b') do (set /a count+=1)
    set msg=!count!
    )
   )
  )
  set "%~1=!msg!" & goto :eof
  )
Ste
  • 1,729
  • 1
  • 17
  • 27
  • 1
    `if {%~1}=={}` will fail, when `%~1` has any spaces. Better use quotes: `if "%~1"==""` to avoid syntax errors. – Stephan Jan 26 '20 at 18:04
  • Fails when files begin with a semi colon `; Semi.txt` and it count also directories not only files – jeb Jan 26 '20 at 18:17
  • 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 Jan 26 '20 at 20:36
  • Fixed that issue. The history of the answer will show the fix. Thanks for pointing this out. – Ste Jan 26 '20 at 21:46
  • 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` ? – jeb Jan 27 '20 at 05:09
  • 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 Jan 27 '20 at 10:44
  • 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. – Ste Jan 27 '20 at 11:00