gpt4 book ai didi

vb.net - 如何删除文件夹中的所有文件(包括子文件夹中的文件)而不删除文件夹本身或其任何子文件夹

转载 作者:行者123 更新时间:2023-12-02 21:24:51 24 4
gpt4 key购买 nike

我想删除文件夹中包含的所有文件。我使用的代码删除根文件夹中的所有文件,但不会删除子文件夹中的文件。这是代码:

If Not Directory.Exists("C:\New Folder") Then
Return
End If

Dim files() As String
files = Directory.GetFileSystemEntries("C:\New Folder")

For Each element As String In files
If (Not Directory.Exists(element)) Then
File.Delete(Path.Combine("C:\New Folder", Path.GetFileName(element)))
End If
Next

我想要的是:

I want to delete all the files inside the folder “New Folder”. At the same time, I want to keep the sub folders and delete all the files it contains. So, after the operation, “New Folder” may have any number of sub folders but it should not have even a single file.

最佳答案

尝试这个递归子

 Sub DeleteFilesFromFolder(Folder As String)
If Directory.Exists(Folder) Then
For Each _file As String In Directory.GetFiles(Folder)
File.Delete(_file)
Next
For Each _folder As String In Directory.GetDirectories(Folder)

DeleteFilesFromFolder(_folder)
Next

End If

End Sub

'Somewhere you call

DeleteFilesFromFolder("C:\New Folder")

关于vb.net - 如何删除文件夹中的所有文件(包括子文件夹中的文件)而不删除文件夹本身或其任何子文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25221181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com