hello,
anybody help me please,
i want to know "delete a folder" code.If you know please help me.
Thanks for your read.
Thanks all

Dani AI

Generated

As noted by and , using the scripting runtime (FileSystemObject) is a simple solution for classic VB. The following adds a robust, .NET-focused alternative and practical troubleshooting tips that weren't shown in the thread.

If you are in VB.NET it is usually easier and more reliable to use the System.IO API, clear any read-only attributes first, then delete recursively with error handling. Example pattern:

Try
    If System.IO.Directory.Exists(folderPath) Then
        For Each f As String In System.IO.Directory.GetFiles(folderPath, "*", System.IO.SearchOption.AllDirectories)
            System.IO.File.SetAttributes(f, System.IO.FileAttributes.Normal)
        Next
        System.IO.Directory.Delete(folderPath, True)
    End If
Catch ex As System.UnauthorizedAccessException
    ' insufficient permissions or read-only / locked file
Catch ex As System.IO.IOException
    ' file in use or path problems
Catch ex As Exception
    ' other errors
End Try

Common pitfalls and fixes:

  • Permissions: run elevated or give the process Delete rights on the folder (correct NTFS ACL).
  • Locked files: a handle held by another process will cause an IOException; use Process Explorer to find the handle.
  • Read-only attributes: clear them before deleting (see File.SetAttributes).
  • Long paths: very long nested paths can trigger PathTooLong errors on older frameworks; consider long-path support or shorter paths.
  • Network shares: UNC paths and remote permissions can behave differently.

Official reference for the delete method and attribute handling:

For classic VB projects continue to use the scripting runtime approach shown earlier in the thread; for VB.NET the System.IO approach gives clearer error handling and control over attributes and recursion.

Recommended Answers

All 6 Replies

One way is to add the Scripting Type Library to your project and use the File System Object. From the Project Menu in your IDE, choose the References Menu. Look for the Microsoft scripting runtime libray and add it to your project.

Private Sub DeleteFolder (ByVal FolderName as String)
Dim fso as New FileSystemObject

fso.DeleteFolder (FolderName)
' Zero out memory for file system object
set fso = Nothing
End Sub

The RmDir command can delete a directory only if it doesn't contain files or sub-directories. If the directory you want to delete does contain other files or, worse, subdirectories it seems that you are forced to use a recursive routine that does the job.

A simpler solution is offered by the DeleteFolder method of the FileSystemObject object, exposed by the Microsoft Scripting Runtime library:

' NOTE: this code requires that you add a reference to the
'       Microsoft Scripting Runtime type library

' delete the C:\TEMP directory and all its sub-directories
Dim fso As New Scripting.FileSystemObject
fso.DeleteFolder "c:\temp"

You can also pass a second argument and set it to True if you want to force the deletion of read-only files:

fso.DeleteFolder "c:\temp", True

does it works ????????????????

thanks,for helping......

then please mark it solved.....

it work,thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.