p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   VB.NET 2002/2003 Basics (http://p2p.wrox.com/forumdisplay.php?f=76)
-   -   Deleting directories and files ... (http://p2p.wrox.com/showthread.php?t=429)

DennisTh June 11th, 2003 07:57 AM

Deleting directories and files ...
 
I am trying to delete a directory with Directory.Delete(Path, True) but there are a number of files that are set as read-only. How do I enumerate all files and folders so I can reset the files with File.SetAttributes(entry.FullName, FileAttributes.Normal)

Thx. in advance

Hal Levy June 11th, 2003 10:53 AM

I'd look at the Microsoft.VisualBasic Namespace as well as the File System Object. There are methods there that can be used to get directory listings and then you can loop though them to change the file attributes.

While the end result it to put the list in a datagrid on this page.. It can give you the information you need...
http://aspnet.4guysfromrolla.com/articles/052803-1.aspx


Hal Levy
Daddyshome, LLC

Imar June 11th, 2003 12:59 PM

Hi Dennis,

I ran into this issue some time ago when I tried to write an application that recursively deleted empty folders within a given folder.

The code I'll post is just an example. It won't win a beauty contest, but it works. Hope you can use it somewhere. If you have any questions, just let me know (through this list).

Code:

Private Sub ProcessDirectory(ByVal targetDirectory As String)
    If Directory.Exists(targetDirectory) And Len(targetDirectory) >= Len(txtDirToProcess.Text) Then
        If Directory.GetFileSystemEntries(targetDirectory).Length = 0 Then
            Dim directoryInfo As System.IO.DirectoryInfo
            directoryInfo = System.IO.Directory.GetParent(targetDirectory)
            Dim theCurrentDir As DirectoryInfo = New DirectoryInfo(targetDirectory)
            ' Check attributes and reset if necessary
            If (theCurrentDir.Attributes And FileAttributes.ReadOnly) > 0 Then
                theCurrentDir.Attributes = theCurrentDir.Attributes And Not FileAttributes.ReadOnly
            End If
            Directory.Delete(targetDirectory)
            ProcessDirectory(directoryInfo.FullName)
        Else
            ' Recurse into subdirectories of this directory
            Dim subdirectory As String
            Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
            For Each subdirectory In subdirectoryEntries
                ProcessDirectory(subdirectory)
            Next subdirectory
        End If
    End If
End Sub


Regards,

Imar

DennisTh June 11th, 2003 01:50 PM

Thanx Imar, it looks promisising - I have one question (for now) what is the txtDirToProcess variable used for or coming from (Line 2) ?

Regards

Dennis

Imar June 11th, 2003 02:03 PM

Hi Dennis,

In my case / application, it's the starting value. A user can browse to a folder, select it and then press the start button which starts the ProcessDirectory method for the first time. Something like this (pseudo):

Sub btnStart_onClick()
    ProcessDirectory(txtBrowseForFolder.Text)
End Sub

Since I recurse (upwards), I try to avoid going above the level of the starting folder, so that's why I check the length.

HtH

Imar

DennisTh June 12th, 2003 02:30 PM

Thanx for your help - here is the result which does the works :D

Regards

Dennis
Code:

    Sub deldir(ByVal targetDirectory As String)
        If Directory.GetDirectories(targetDirectory).Length > 0 Then
            Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
            Dim subdirectory As String
            For Each subdirectory In subdirectoryEntries
                deldir(subdirectory)
                Dim dirEntry As File
                dirEntry.SetAttributes(subdirectory, FileAttributes.Normal)
                Directory.Delete(subdirectory)
            Next
        End If

        Dim fileEntries As String() = Directory.GetFileSystemEntries(targetDirectory)
        Dim fileEntry As File
        Dim sFile As String
        For Each sFile In fileEntries
            fileEntry.SetAttributes(sFile, FileAttributes.Normal)
            fileEntry.Delete(sFile)
        Next
    End Sub



All times are GMT -4. The time now is 08:43 AM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.