Results 1 to 6 of 6

Thread: How do I delete a folder including files?

  1. #1
    Pirre001
    Guest

    Question How do I delete a folder including files?

    How do I delete a folder including files in a code?

  2. #2
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033
    DeleteFolder Method


    Description

    Deletes a specified folder and its contents.

    Syntax

    object.DeleteFolder folderspec[, force]

    The DeleteFolder method syntax has these parts:

    Part Description
    object Required. Always the name of a FileSystemObject.
    folderspec Required. The name of the folder to delete. The folderspec can contain wildcard characters in the last path component.
    force Optional. Boolean value that is True if folders with the read-only attribute set are to be deleted; False (default) if they are not.


    Remarks

    The DeleteFolder method does not distinguish between folders that have contents and those that do not. The specified folder is deleted regardless of whether or not it has contents.

    An error occurs if no matching folders are found. The DeleteFolder method stops on the first error it encounters. No attempt is made to roll back or undo any changes that were made before an error occurred.
    Help keep this forum clean: Remember to mark your thread as resolved · Search before you post · Remember to rate posts that help

    VS2010: Visual Studio 2010 Keybinding Posters
    · Service Pack 1
    Tools: GhostDoc - automatically generates XML documentation comments
    · NuGet package Manager · PowerCommands IDE extensions
    Source Control: ankhsvn - integration for SVN
    · Windows Shell Extension for Subversion

    Development Laptop: Intel Core i5 430M 2.26 GHz @ 2.53 GHz
    · 4096 MB, DDR3 PC3-8500F (533 MHz), Kingston · ATI Mobility Radeon HD 5470 · 15.6 @ 16:9, 1366x768 pixel, HD LED LCD

    I follow:
    JoelOnSoftware - A weblog by Joel Spolsky, a programmer working in New York City, about software and software companies
    ScottGu's Blog - Scott Guthrie works for Microsoft as the Product Manager of the .NET Framework
    Portugal-a-Programar - Portuguese Developers Community
    .NET Rocks! - is a weekly Internet audio talk show for .NET Developers.

    Programming Languages:
    C#
    · VB.NET · JAVA · PHP · Javascript
    Other:
    XML
    · HTML · CSS · JQuery · SQL



    *** Proudly Portuguese ***

  3. #3
    Frenzied Member zuperman's Avatar
    Join Date
    Dec 2000
    Location
    Portugal
    Posts
    1,033
    Just an example:

    VB Code:
    1. Private Sub Command1_Click()
    2.  
    3.     Set fs = CreateObject("Scripting.FileSystemObject")
    4.     fs.Deletefolder "c:\MyFolder", True
    5.  
    6. End Sub
    Help keep this forum clean: Remember to mark your thread as resolved · Search before you post · Remember to rate posts that help

    VS2010: Visual Studio 2010 Keybinding Posters
    · Service Pack 1
    Tools: GhostDoc - automatically generates XML documentation comments
    · NuGet package Manager · PowerCommands IDE extensions
    Source Control: ankhsvn - integration for SVN
    · Windows Shell Extension for Subversion

    Development Laptop: Intel Core i5 430M 2.26 GHz @ 2.53 GHz
    · 4096 MB, DDR3 PC3-8500F (533 MHz), Kingston · ATI Mobility Radeon HD 5470 · 15.6 @ 16:9, 1366x768 pixel, HD LED LCD

    I follow:
    JoelOnSoftware - A weblog by Joel Spolsky, a programmer working in New York City, about software and software companies
    ScottGu's Blog - Scott Guthrie works for Microsoft as the Product Manager of the .NET Framework
    Portugal-a-Programar - Portuguese Developers Community
    .NET Rocks! - is a weekly Internet audio talk show for .NET Developers.

    Programming Languages:
    C#
    · VB.NET · JAVA · PHP · Javascript
    Other:
    XML
    · HTML · CSS · JQuery · SQL



    *** Proudly Portuguese ***

  4. #4
    Pirre001
    Guest
    Why doesen't this code work with NT? It works with Win98?

    VB Code:
    1. Dim fso As New FileSystemObject
    2. Dim fldr As Folder
    3.  
    4.  
    5. Private Sub Command1_Click()
    6.   Set fldr = fso.GetFolder("C:\Winnt\Test")
    7.       fldr.Delete True
    8. End Sub

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Try this
    VB Code:
    1. Private Sub DeleteFolder(FolderPath)
    2. 'Purpose:  This function will delete all the folder(s) and its
    3. 'contents under the root folder. All you need to do is to pass in
    4. 'the Path as a Parameter
    5. 'Usage:  Call DeleteFolder("d:\temp\")
    6. 'From:  [url]www.vbcode.com[/url] by Dan Liu
    7. Dim MyFile, MyPath, MyName
    8. Dim Fs As Scripting.FileSystemObject
    9. Set Fs = CreateObject("Scripting.FileSystemObject")
    10. ' Display the names that represent directories.
    11. MyPath = FolderPath   ' Set the path.
    12. MyName = Dir(MyPath, vbDirectory)   ' Retrieve the first entry.
    13. Do While MyName <> ""   ' Start the loop.
    14.    ' Ignore the current directory and the encompassing directory.
    15.    If MyName <> "." And MyName <> ".." Then
    16.       ' Use bitwise comparison to make sure MyName is a directory.
    17.       If (GetAttr(MyPath & MyName) And vbDirectory) = vbDirectory Then
    18.          MsgBox MyPath & MyName
    19.          Fs.DeleteFolder (MyPath & MyName)
    20.       End If   ' it represents a directory.
    21.    End If
    22.    MyName = Dir   ' Get next entry.
    23. Loop
    24.  
    25. End Sub

  6. #6
    Pirre001
    Guest
    Hack,

    Does this code work with NT? What's the differens between this code and the code i suggested?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width