Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB.NET 1.0 > VB.NET 2002/2003 Basics
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 11th, 2003, 07:57 AM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
  #2 (permalink)  
Old June 11th, 2003, 10:53 AM
Friend of Wrox
Points: 3,489, Level: 24
Points: 3,489, Level: 24 Points: 3,489, Level: 24 Points: 3,489, Level: 24
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Central, NJ, USA.
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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
  #3 (permalink)  
Old June 11th, 2003, 12:59 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
  #4 (permalink)  
Old June 11th, 2003, 01:50 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
  #5 (permalink)  
Old June 11th, 2003, 02:03 PM
Imar's Avatar
Wrox Author
Points: 70,322, Level: 100
Points: 70,322, Level: 100 Points: 70,322, Level: 100 Points: 70,322, Level: 100
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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
  #6 (permalink)  
Old June 12th, 2003, 02:30 PM
Registered User
 
Join Date: Jun 2003
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
display directories and files using only xsl/xml.. eteune XSLT 1 January 2nd, 2007 07:01 PM
Deleting files using a listbox control Indo77 ASP.NET 1.0 and 1.1 Basics 1 April 20th, 2006 10:43 AM
checking directories for files Trojan_uk Classic ASP Basics 1 March 16th, 2004 10:27 AM
Parse text files and save in sub directories malesd VB How-To 2 March 1st, 2004 09:32 AM
Including same files in different directories jeremy108 BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 August 8th, 2003 01:37 PM





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