11
for /F %%i in ('dir /b "C:\Program Files\Apache folder\*.*"') do (
   echo Folder is NON empty
   goto launch_app
)
  • How to check a folder is empty?

    I tried above command, but it didn't work.

2

5 Answers 5

13

try this:

for /F %%i in ('dir /b /a "C:\Program Files\Apache folder\*"') do (
    echo if you see this the folder is NOT empty
    goto launch_app
)
Sign up to request clarification or add additional context in comments.

3 Comments

its says system cannot find the path specified. i have Apache folder in that location. inside apache i have one more folder. but still getting error.
To check for more folders, remove the -d option from dir.
i tried removing -d still it says system cannot find the path specified.
7

I wasn't satisfied with the given answers because I try to avoid for loops, goto and temp files whenever I can.

To check if the folder is empty:

dir /b /s /a "C:\Program Files\Apache folder\" | findstr .>nul || (
  echo Folder is empty
)

To check if folder is not empty:

dir /b /s /a "C:\Program Files\Apache folder\" | findstr .>nul && (
  echo Folder is NOT empty
)

1 Comment

These are gold. Thanks. Made a slight change to eliminate the creation of file called "nul". dir /b /s /a "C:\Program Files\Apache folder\" | findstr .>NUL 2>&1 || ( echo Folder is empty ) MUST be an upper case NUL.
2
@echo off
cls
echo.
echo.
echo.
echo                  TEST FOR EMPTY DIRECTORY
echo             was tested with "&" without a file
echo.
echo              Can use Full or Subfolder path
echo               (as below) of where test.bat
echo.
    set p=Raw\
echo.
echo                  Just to show p is set
echo.
echo                       "p=%p%"
echo.
echo.
echo.
pause

for /F %%i in ('dir /b /a "%p%*"') do (
    echo Folder %p% was NOT empty
    goto :process
)
    echo Folder %p% was empty 



:process
    echo "BLAH, BLAH, BLAH "
:end
    pause
    exit
rem Copy past in notepad to try. "Create and use or change the "raw" to your own fancy."
rem Hope this helps. Works great for me and I use it.
rem Have Fun.
rem Note use go to end next line after was empty. Worked great in w10. (this program hmm)
rem IF IT WORKS FOR YOU . GIVE A +. THX!

1 Comment

After relooking over this site I used Endoro's code 4-19-13 and modified it slightly. So a big thx to Endoro and a +.
2

File Not Found

@for /f "tokens=*" %%a in ('dir /b /a-d "C:\Progra~1\Apache"') do @...

The error that you see when you run this command, comes for the standard error output. But that is only a warning printed to your console. When this case happens, the body of the iteration won't be evaluated, and the algorithm based on this "for/dir" instruction, is in fact correct.

Now, if you want to get rid of this ugly error message, you need to add the following to your script, in order to redirect the standard error to null device:

2>NUL

so for instance, in a batch file, with the appropriate escape character:

@rem Print a list of file paths
@for /f "tokens=*" %%a in ('dir /b /a-d "%srcPath%" 2^>NUL') do @echo(%srcPath%\%%a

Comments

0
dir C:\TEST\*.* /a/b/d/od>C:\TEMP\CHECKFOLDER
for /R C:\TEMP\ %%? in (CHECKFOLDER) do (
 if "%%~z?"=="0" (
  ECHO Folder is empty.
 ) ELSE (
  ECHO Folder is NOT empty
 )
)

2 Comments

Code only answers can almost always be improved by the addition of some explanation of how and why they work?
Welcome to Stack Overflow! While this code may solve the question, including an explanation of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please edit your answer to add explanations and give an indication of what limitations and assumptions apply.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.