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.
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.
%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).
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.
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.
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
)
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
@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!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With