#1 22 Feb 2011 03:12

rubyroseveloso
Member
Registered: 22 Feb 2011
Posts: 2

batchfile: create output file w/ empty folder

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

#2 22 Feb 2011 10:35

RG
Member
From: Ohio
Registered: 18 Feb 2010
Posts: 318

Re: batchfile: create output file w/ empty folder

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

#3 22 Feb 2011 10:41

allal
Member
Registered: 10 Jan 2011
Posts: 48

Re: batchfile: create output file w/ empty folder

  @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)

popd

Last edited by allal (22 Feb 2011 12:13)

Offline

#4 22 Feb 2011 10:47

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 931

Re: batchfile: create output file w/ empty folder

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

Last edited by bluesxman (22 Feb 2011 15:47)


cmd | *sh  | KiX | AutoIT3 | PowerShell | Puppet

Offline

#5 22 Feb 2011 11:15

allal
Member
Registered: 10 Jan 2011
Posts: 48

Re: batchfile: create output file w/ empty folder

bluesxman wrote:

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

#6 22 Feb 2011 15:45

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 931

Re: batchfile: create output file w/ empty folder

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 | Puppet

Offline

#7 23 Feb 2011 00:57

rubyroseveloso
Member
Registered: 22 Feb 2011
Posts: 2

Re: batchfile: create output file w/ empty folder

It worked! thank you very much! smile

Offline

Board footer

Powered by FluxBB