Click here to Skip to main content
15,897,518 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to Find emtpy folder and zero size file and del thm.
in vb.net



how to find list of zero siz file in vb.net and del them.
Posted
Comments
phil.o 24-Oct-13 2:54am    

There is no "automatic" way to do that: you will first off have to do it in two stages: Find all zero length files delete them, then find all empty folders.
Zero length files isn't too bad: Directory.GetAllFiles[^] will return you a list of every file under a directory if you use the SearchOption.AllDirectories option.

You can then parse that list and check the file sizes by creating a FileInfo[^] for each file and checking the FileInfo.Length[^] property. It's then a simple matter to use File.Delete[^] to remove them.

Empty directories is harder, but not a lot. If while you are parsing the files above, you build a list of directories which contain at least one non-zero length file, you can compare that to the array returned by Directory.GetDirectories[^] and use Directory.Delete[^] to remove them - but...be careful! You do not want to remove a directory unless all it's subdirectories are empty as well! :laugh:
 
Share this answer
 
Hello ,

to get the number of empty folders against one drive / folder , you just try this method. It will return a list of empty folders ...

Private listbox As New List(Of String)()

Public Function getemptyfolders(folderpath As String) As List(Of String)
	
	Try
		Dim dr As New List(Of String)() From { _
			folderpath _
		}
		Dim Spath1 As String

		For Each dir As String In dr
			Dim di As New DirectoryInfo(dir)
			Dim direcorries As DirectoryInfo() = di.GetDirectories("*.*", SearchOption.TopDirectoryOnly)
			'get all the directories

			For Each dada As DirectoryInfo In direcorries
				'search through all directories to get inner directory
				Try
					If dada.ToString() <> "System Volume Information" Then
						'ignore this 'System Volume Information'
						Dim Sdada As String = (folderpath & Convert.ToString("\")) + dada
						If Directory.GetDirectories(Sdada).Length = 0 AndAlso Directory.GetFiles(Sdada).Length = 0 Then
							'check the empty folders
							If Sdada.Contains("\") Then
								Sdada = Sdada.Replace("\\", "\")
							End If							

							listbox.Add(Sdada)
						Else
							Spath1 = (folderpath & Convert.ToString("\")) + dada
							getemptyfolders(Spath1)
						End If
					End If
				Catch

				End Try
			Next

		Next
	Catch
	End Try
	Return listbox
End Function


and to delete the empty folders you can loop through the listbox and delete them one
by one like

Directory.Delete(string path);


thanks
 
Share this answer
 
v2
Try this functions:-

Private Sub EnumFolders(sDrive As String)

    Dim oFS As Scripting.FileSystemObject, oRoot As Folder

    Set oFS = New Scripting.FileSystemObject
    
    Set oRoot = oFS.GetFolder(sDrive & "\")
    Call RecurseFolders(oRoot)

    Set oRoot = Nothing
    Set oFS = Nothing

End Sub

Private Sub RecurseFolders(oFolder As Folder)

    Dim oSub As Folder
    
    For Each oSub In oFolder.SubFolders
        If oSub.SubFolders.Count = 0 Then
            If oSub.Files.Count = 0 Then
                oSub.Delete
            End If
        Else
            Call RecurseFolders(oSub)
        End If
    Next oSub

End Sub
 
Share this answer
 
C#
Dim dr As New List(Of String)() From { _
            folderpath _
        }


show error.
 
Share this answer
 
Comments
Animesh Datta 25-Oct-13 1:34am    
modify above line by this line..

Dim dr As List(Of String) = New List(Of String)() {folderpath}

thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900