Vb.net 删除目录删除字符串中最后一个为空的目录
下面的子项搜索某些文件夹并删除任何空文件夹(除非该文件夹称为Bars或Backup) sPath设置为Vb.net 删除目录删除字符串中最后一个为空的目录,vb.net,visual-studio-2012,directory.delete,Vb.net,Visual Studio 2012,Directory.delete,下面的子项搜索某些文件夹并删除任何空文件夹(除非该文件夹称为Bars或Backup) sPath设置为c:\temp\working\,如果为空,则应扫描和删除的文件夹是working 但是,如果子目录中的所有子目录都是空的,并且都已被删除,那么文件夹Working就会被删除,这不是我需要的 有没有关于如何停止此操作的建议,或者如果目录进入该阶段,是否需要重新创建目录(不理想) 谢谢解决此问题的第一种方法是传递起始根目录,如果目录是起始根目录,则不要删除它 Public Sub DeleteEm
c:\temp\working\
,如果为空,则应扫描和删除的文件夹是working
但是,如果子目录中的所有子目录都是空的,并且都已被删除,那么文件夹Working
就会被删除,这不是我需要的
有没有关于如何停止此操作的建议,或者如果目录进入该阶段,是否需要重新创建目录(不理想)
谢谢解决此问题的第一种方法是传递起始根目录,如果目录是起始根目录,则不要删除它
Public Sub DeleteEmptyFolders(ByVal sPath As String, ByVal sRoot As String)
Dim SubDirectories() As String = Directory.GetDirectories(sPath )
For Each strDirectory As String In SubDirectories
DeleteEmptyFolders(strDirectory, sRoot)
Next
If Not (UCase(Path.GetDirectoryName(sPath)).Contains("BACKUP")) Or (UCase(Path.GetDirectoryName(sPath)).Contains("BARS")) Then
If Directory.GetFiles(sPath).Length + Directory.GetDirectories(sPath).Length = 0 Then
if sPath <> sRoot Then
Directory.Delete(sPath)
Console.WriteLine("Deleting empty folder: " & sPath)
End If
End If
End If
End Sub
似乎不喜欢第二个变量也是pathMy bad,从不需要的GetDirectories中删除了sRoot
Public Sub DeleteEmptyFolders(ByVal sPath As String, ByVal sRoot As String)
Dim SubDirectories() As String = Directory.GetDirectories(sPath )
For Each strDirectory As String In SubDirectories
DeleteEmptyFolders(strDirectory, sRoot)
Next
If Not (UCase(Path.GetDirectoryName(sPath)).Contains("BACKUP")) Or (UCase(Path.GetDirectoryName(sPath)).Contains("BARS")) Then
If Directory.GetFiles(sPath).Length + Directory.GetDirectories(sPath).Length = 0 Then
if sPath <> sRoot Then
Directory.Delete(sPath)
Console.WriteLine("Deleting empty folder: " & sPath)
End If
End If
End If
End Sub
DeleteEmptyFolders("D:\temp", "D:\temp")