A batch line to delete empty folders

October 15, 2010 at 13:22:44
Specs: microsoft office
Hi,
I'd like to run a command line in a batch file that looks into a folder that I specify, and if the folder is empty, the directory moves one level back and the folder is deleted.

Much thanks!


See More: A batch line to delete empty folders

Report •


#1
October 15, 2010 at 18:43:49
If the folder contains no files, do you want the script to ignore any subfolders?


=====================================
Life is too important to be taken seriously.

M2


Report •

#2
October 15, 2010 at 20:23:50
the easiest way is to use the correct tool meant for the job. download findutils for windows and use the find command.

C:\test>gnu_find.exe . -type d -empty -delete

it is named to "gnu_find.exe" so as not to conflict with Windows own find command.

OR if you absolutely hate downloading stuff. you can use vbscript

Set objFS = CreateObject( "Scripting.FileSystemObject" )
strFolder=WScript.Arguments(0)
Set objFolder = objFS.GetFolder(strFolder)
Go (objFolder)
Sub Go(objDIR)
  If objDIR <> "\System Volume Information" Then
    For Each eFolder in objDIR.SubFolders
      	Go eFolder
    Next
    If objDIR.Files.Count = 0 Then
    	WScript.Echo objDIR.Files.Count , objDIR.Name
    	objFS.DeleteFolder objDIR,True 
    End If 
  End If
End Sub 

usage:

C:\test>cscript //nologo mydelete.vbs c:\starting_folder

GNU win32 packages | Gawk


Report •

#3
October 15, 2010 at 20:29:15
rd is an OK tool. We just need to be clear about what's to be done.


=====================================
Life is too important to be taken seriously.

M2


Report •

Related Solutions

#4
October 29, 2010 at 10:50:23
Mechanix--- subfolders should not be ignored. The folder in question must contain NOTHING (definition of being empty) to be qualified for removal by deletion.

Report •

#5
October 29, 2010 at 10:52:03
Thanks to ghost dog and mechanix!

Report •

#6
October 29, 2010 at 11:01:42
Easiest method:
rd "a folder that I specify"

How To Ask Questions The Smart Way


Report •

#7
October 29, 2010 at 12:30:59
@echo off & setLocal EnableDELAYedeXpansion

for /f "tokens=* delims= " %%a in ('dir/b/ad') do (
rd %%a
)


=====================================
Life is too important to be taken seriously.

M2


Report •

Ask Question