 |
| 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
|
|
|
|

June 11th, 2003, 07:57 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

June 11th, 2003, 10:53 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
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
|
|

June 11th, 2003, 12:59 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

June 11th, 2003, 01:50 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

June 11th, 2003, 02:03 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
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
|
|

June 12th, 2003, 02:30 PM
|
|
Registered User
|
|
Join Date: Jun 2003
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|
 |