如何在vb.net中查找emtpy文件夹 [英] how to Find emtpy folder in vb.net

查看:124
本文介绍了如何在vb.net中查找emtpy文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何查找emtpy文件夹和零大小的文件和del thm。

in vb.net







如何在vb.net和del中查找零siz文件列表。

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.

推荐答案

没有自动方式要做到这一点:你首先必须分两个阶段完成:找到所有零长度文件删除它们,然后找到所有空文件夹。

零长度文件也不错: Directory.GetAllFiles [ ^ ]将返回目录下每个文件的列表您使用SearchOption.AllDirectories选项。



然后您可以解析该列表并通过创建 FileInfo [ ^ ]并检查 FileInfo.Length [ ^ ]属性。然后使用 File.Delete [ ^ ]删除它们。



空目录更难,但不是很多。如果在解析上面的文件时,您构建了一个包含至少一个非零长度文件的目录列表,您可以将其与 Directory.GetDirectories [ ^ ]并使用 Directory.Delete [ ^ ]删除它们 - 但是......小心!除非所有子目录都是空的,否则您不想删除目录! :笑:
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:


你好,



来获取一个驱动器/文件夹的空文件夹数量,你只需要尝试这种方法。它将返回一个空文件夹列表...



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


尝试此功能: -



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


这篇关于如何在vb.net中查找emtpy文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆