|
|
 |
| 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 tens of thousands of computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other programmers’ questions, win occasional prizes given to our best members, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
|
 |
|
|
 |

June 11th, 2003, 07:57 AM
|
|
Registered User
|
|
Join Date: Jun 2003
Location: , , .
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
Location: Central, NJ, USA.
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
Points: 36,403, Level: 83 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,707
Thanks: 13
Thanked 305 Times in 301 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
Location: , , .
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
Points: 36,403, Level: 83 |
|
|
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,707
Thanks: 13
Thanked 305 Times in 301 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
Location: , , .
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
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
 |