You are not logged in.
Hi!
I need to determine empty folders within a given file location (e.g. C:\test\) and output the empty folder file names to an output file (e.g. C:\test\output.txt).
I have this code for the batch file however I can only echo ALL the folders to the txt file. I only need the empty folder file names to be echoed.
pushd C:\test
for /d %%d in (*) do (
rd /q %%d
echo %%d >> c:\test\output.txt
)
popd
Can someone assist me with this?
Thanks!
Offline
Here's one way to do what you are asking. (You did not mention empty subfolders and this does not check for those).
pushd C:\test
set OutFile=output.txt
if exist %OutFile% DEL /q %OutFile%
for /d %%d in (*) do CALL :IsDirEmpty "%%d" && ECHO.%%d >> %OutFile%
popd
goto :eof
:: [url]http://www.commandline.co.uk/lib/treeview/index.php[/url]
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:IsDirEmpty %dir%
::
:: By: Ritchie Lawrence, 2003-09-22. Version 1.0
::
:: Func: If specified directory is empty of files and directories, then
:: errorlevel is set to zero. If directory is not empty or does not
:: exist, errorlevel is non-zero one. For NT4/2000/XP/2003.
::
:: Args: %1 Name of directory to be tested (by val)
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
setlocal ENABLEEXTENSIONS & dir/ad %1 >nul 2>nul || (endlocal & goto :EOF)
set i=0 & for /f %%a in ('dir %1/a/b 2^>nul') do set/a "i+=1"
md;2>nul & (if %i%==0 ver>nul) & endlocal & goto :EOF
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::Last edited by RG (22 Feb 2011 10:36)
CMD and InstallShield
Offline
@echo off>output.txt
pushd C:\test
for /d %%a in (.) do (
dir /b "%%~a" | find /v "" >nul ||echo empty folder detected & rd /q "%%~a" &echo "%%~a" >>output.txt)
popdLast edited by allal (22 Feb 2011 12:13)
Offline
With that code, this is all you need to do:
@echo off
type nul >c:\test\output.txt
pushd C:\test
for /d %%d in (*) do (
rd /q "%%~d" && echo "%%~d" >> c:\test\output.txt
)
popdLast edited by bluesxman (22 Feb 2011 15:47)
CMD | *sh | KiX | AutoIT3 | PowerShell
Offline
With that code, this is all you need to do:
@echo off type nul >c:\test\output.txt ) pushd C:\test for /d %%d in (*) do ( rd /q "%%~d" && echo "%%~d" >> c:\test\output.txt ) popd
i think you will log and remove all directories empty and non empty
Last edited by allal (22 Feb 2011 11:16)
Offline
It will only remove empty directories and will only report those it successfully removes (by virtue of the "&&"). That is the code as originally submitted with a couple of small tweaks. I made the assumption from that code that removing the directories and reporting those which were removed was the intended goal.
Last edited by bluesxman (22 Feb 2011 15:46)
CMD | *sh | KiX | AutoIT3 | PowerShell
Offline
It worked! thank you very much! ![]()
Offline