Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check folder is empty using batch command?

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.

like image 207
Bobby Rachel Avatar asked Jul 19 '13 11:07

Bobby Rachel


People also ask

How do you check if a directory is empty in Windows?

To check whether a directory is empty or not os. listdir() method is used. os. listdir() method of os module is used to get the list of all the files and directories in the specified directory.

What does 0 |% 0 Do in batch?

%0|%0 is a fork bomb. It will spawn another process using a pipe | which runs a copy of the same program asynchronously. This hogs the CPU and memory, slowing down the system to a near-halt (or even crash the system).

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

How can I see all folders in CMD?

Click in the address bar and replace the file path by typing cmd then press Enter. This should open a black and white command prompt displaying the above file path. Type dir /A:D. /B > FolderList.


3 Answers

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
)
like image 133
Endoro Avatar answered Sep 19 '22 15:09

Endoro


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
like image 25
numdig Avatar answered Sep 16 '22 15:09

numdig


@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!
like image 29
RHSR Avatar answered Sep 19 '22 15:09

RHSR