5

Using a simple batch file, compatible with the command processor in Windows 7, how can one detect if a folder has any contents (meaning one or more files or subfolders, i.e. not empty)?

I tried:

IF EXIST C:\FOLDERNAME\* GOTO ROUTINE

But this always returns TRUE and thus goes to ROUTINE.

How can this be accomplished?

4 Answers 4

7
dir /A /B "C:\FOLDERNAME" | findstr /R ".">NUL && GOTO ROUTINE

If there is at least one line of output, then the directory is not empty.
The findstr /R "." results the into a successful exitcode and the && will execute the goto routine

6
  • Might I suggest you use the DIR /A option to ensure that it doesn't miss, for instance, hidden items. DIR/B/A "C:\FOLDERNAME"2>NUL|FINDSTR .*>NUL&&GOTO ROUTINE
    – Compo
    Commented Apr 14, 2017 at 11:08
  • Your answer tries to solve another problem than the question author asks. He considered a folder with some (even empty) subfolders as nonempty, while you are looking only for files (and do this recursively). While your understanding of "emptiness" also makes sense, but it differs from the question author's understanding, for him dir /A /B "C:\FOLDERNAME" | findstr . >NUL && GOTO ROUTINE would be enough and more correct. Also, your implementation assumes that directory names never contains . and file names always contain ., but that's not true.
    – Sasha
    Commented Jul 24, 2019 at 7:02
  • 4
    Ouch. I've noticed that the original answer of user2956477 was fully correct, but then the Community user changed it in the weird way. Please, somebody, revert to the original user2956477's version!
    – Sasha
    Commented Jul 24, 2019 at 7:04
  • How about this code form ss64: ss64.com/nt/empty.html Commented Jan 14, 2020 at 10:52
  • At my end, findstr /L "." works with this folder name A.B.C, which contains dots. Commented Mar 4, 2020 at 11:19
2

The solution I prefer is to iterate through the files in the directory and then iterate through the attributes of each file to get its size and find out if it is greater than 0.

The following script I made validates the following:

  • If the specified directory exists.
  • Whether the folder contains files or not.
  • If the folder contains files larger than 0 bytes.

In case there are no files or the files do not exceed 0 bytes, the script will report that the directory does not contain any files.

To run the script you must use the following command, where "C:/path" is the directory to check:

start name.bat "C:/path"

Rename the script and change the directory.


Bat script:

@echo off

REM Command to launch the script:
REM >> start name.bat "C:/path"

REM Check if path exist
if exist %1 (

    REM Loop in dir files
    for /f "delims=" %%f in ('dir /a /b /s %1') do (
        
        REM Check file sizes
        for /f "usebackq delims=" %%s in ('%%f') do (

            REM Check if there is a file bigger than 0 bytes
            if %%~zs GTR 0 (
                echo The directory is not empty
                goto end
            )
        )
    )
    echo The directory is empty

) else (
    echo The directory does not exist
)

:end
pause
12
  • But if a directory contains a file, it's not empty, independent of the file size
    – jeb
    Commented Mar 15, 2021 at 16:28
  • 1
    Just remove the second for leaving the echo and the goto if you don't need this feature. Personally, I prefer to consider empty a folder that contains empty files. Commented Mar 15, 2021 at 16:37
  • You can only remove a directory when it's empty - there is nothing in it. Btw. Your code fails if there are files or directories beginning with ;
    – jeb
    Commented Mar 15, 2021 at 16:40
  • To remove all the files from directories and subdirectories you can use del /F/Q/S *.* > nul and using special characters in file and folder names is generally considered bad practice. Commented Mar 15, 2021 at 16:46
  • 1
    C:\Program Files (x86), C:\Documents & Settings are named by Microsoft. Btw. Even if it's bad practice, a function should always return a correct value
    – jeb
    Commented Mar 15, 2021 at 16:50
1

You can put something like this in a batch file (assuming you know the folder exists):

FOR /F %%A IN ('DIR /A /B FolderName 2^>nul') DO GOTO NonEmpty
GOTO Empty

Change /A to /A-D if you want to test whether the folder contains files (ie, ignore the subfolders).

Change /A to /AD if you want to test whether the folder contains subfolders (ie, ignore the files within the folder).

0

Slightly different answer, but my search led here and hopefully this helps others.

My use case is I want to run an exe if the current folder has files in it. If the current folder is empty or contains only other folders, then I want nothing to happen.

I started with trying something like if exist *.??? since in my case, the files in that folder always have a three char extension... but this did not work because ? seems to match o or more, not 1 or more. It always evaluated true, even when the folder had no files in it (I assume because every folder contains folders named . and .. )

So I tried a different approach using dir. I found in my case findstr had no bearing on the result (I believe because in my case dir /A-d /b returns "file not found" if there are no files in the dir due to -D parameter on /A... so the findstr . always returns true).

In my case, the statement below worked like a charm. It executes the command if there are files in the folder and does nothing if there are no files or only other folders in the folder.

cd c:\interesting_dir
dir /a-D /b>nul && (
  c:\path\to\program1.exe
  c:\path\to\program2.exe
)

Not the answer you're looking for? Browse other questions tagged or ask your own question.