Solved

Remove directory

Posted on 2011-02-18
4
318 Views
Last Modified: 2012-05-11
I have a directory C:\Test\Test2\Test3\Test4

I need to programmatically delete it but only if Neither folder contains any files.

Foe example if there is one file test.txt in Test2, besides folder Test3, then I want only Teast4 and Test3 folders deleteed and I want C:\Test\Test2\test.txt left intact. But if neither directory contains any files, i want them all deleted. Is there a way to traverse through directories, checking for files and delting empty directories?


I tried
 
Dim objFSO, strFolder, strFile
strFolder= wscript.Arguments(0) 

set objFSO = createobject("Scripting.FileSystemObject")

IF objFSO.FolderExists(strFolder) Then

	If ((objFSO.GetFolder(strFolder).SubFolders.Count = 0) AND 	(objFSO.GetFolder(strFolder).Files.Count = 0)) Then
		objFSO.DeleteFolder strFolder, true
	End If
End If

Open in new window


and passed it C:\Test\Test2\Test3\Test4 as an argument
and it only removes Test4 folder, leaving C:\Test\Test2\Test3\ even though there are not any files
0
Comment
Question by:YZlat
  • 2
4 Comments
 
LVL 13

Expert Comment

by:connectex
ID: 34930464
Your code doesn't attempt to delete anything but the last folder. You could use the split function with "\" to split the path. Then within a loop using the number of items that the previous split returned do the same checks and delete those folders as well. Just remember you must work your way back up the file tree.
0
 
LVL 13

Accepted Solution

by:
connectex earned 500 total points
ID: 34930910
Actually I think my first post was a bit confusing as it's actually probably hard doing it that way. It's really better to loop backwards through the folder path via InStrRev function. Here's the code I come up with.
' Variables
Dim objFSO, strFolder, strFile, bFlag, intPos

' Setup objects
set objFSO = createobject("Scripting.FileSystemObject")

' Get arguments
strFolder = WScript.Arguments(0)

' Loop through folders and delete any empty folders from bottom upwards
bFlag = True
While bFlag = True 
  If objFSO.FolderExists(strFolder) Then
    If ((objFSO.GetFolder(strFolder).SubFolders.Count = 0) And (objFSO.GetFolder(strFolder).Files.Count = 0)) Then
      WScript.Echo "Deleting " & strFolder
      objFSO.DeleteFolder strFolder, True
    Else
      WScript.Echo "Folder " & strFolder & " is not empty."
      bFlag = False
    End If
    intPos = InStrRev(strFolder, "\", -1) 		
    If intPos > 0 And Mid(strFolder, intPos-1, 1) <> ":" Then
      strFolder = Left(strFolder, intPos-1)
    Else
      bFlag = False
    End If
  Else
    WScript.Echo "ERROR - Folder " & strFolder & " doesn't exists."
    bFlag = False
  End If
Wend

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 34931045
Here's some logic I have used in the past. It will remove all empty subfolders under the starting folder.  As low level empty folders are removed, then if parent folders become empty, they are removed.

Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
RemoveEmptyFiles objFSO.GetFolder("c:\test")

Sub RemoveEmptyFiles(objFolder)
    ' First remove all empty subfolders of this one
    For Each objSubFolder In objFolder.Subfolders
        RemoveEmptyFiles objSubFolder
    Next

    ' If folder is now empty, remove it
    If objFolder.Files.Count = 0 And objFolder.Subfolders.Count = 0 Then
        objFolder.Delete
    End If
End Sub

Open in new window

~bp
0
 
LVL 35

Author Closing Comment

by:YZlat
ID: 34943761
Thank you!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This video discusses moving either the default database or any database to a new volume.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now