How to delete folders/files within a folder but not the folder itself 
Author Message
 How to delete folders/files within a folder but not the folder itself

Hi!

I need to write a small program that deletes all folders and files within a
specified folder but not the folder itself.

So, for better understanding, heres my code:

the problem is, o.Name will be killed, too...
Is there a way to keep the parent directory?

The Code lists all users of my computer and defines the homedirectories.
Each user has an own directory, which must not be deleted!

Dim o As DirectoryEntry

Dim c As DirectoryEntries

o = New DirectoryEntry(LDAPusers)

c = o.Children

Dim snc As SchemaNameCollection

snc = c.SchemaFilter

snc.Add("User")

For Each o In c

Dim homedir = "G:\test\" & o.Name

Dim d As DirectoryInfo

d = New DirectoryInfo(homedir)

On Error Resume Next

d.Delete(True)

Next

THX

Jan Wrage



Tue, 23 Aug 2005 00:13:57 GMT  
 How to delete folders/files within a folder but not the folder itself
Hello Jan,


Quote:
> I need to write a small program that deletes all folders and files
within a
> specified folder but not the folder itself.

Try this code:

\\\
Dim dir As New System.IO.DirectoryInfo("C:\test\")
Dim di As System.IO.DirectoryInfo
For Each di In dir.GetDirectories()
    di.Delete(True)
Next di
Dim fi As System.IO.FileInfo
For Each fi In dir.GetFiles()
    fi.Delete()
Next fi
///

Es gibt brigens auch eine deutschsprachige NG zu VB .NET:


Grsse,
Herfried K. Wagner



Tue, 23 Aug 2005 05:19:28 GMT  
 How to delete folders/files within a folder but not the folder itself
Hallo Herfried!

Danke fr den Code,

habe es vorhin auch so gemacht :-)

Hat bloss etwas gedauert, bis ich drauf gekommen bin..

Danke und Gru?

Jan


Quote:
> Hello Jan,


> > I need to write a small program that deletes all folders and files
> within a
> > specified folder but not the folder itself.

> Try this code:

> \\\
> Dim dir As New System.IO.DirectoryInfo("C:\test\")
> Dim di As System.IO.DirectoryInfo
> For Each di In dir.GetDirectories()
>     di.Delete(True)
> Next di
> Dim fi As System.IO.FileInfo
> For Each fi In dir.GetFiles()
>     fi.Delete()
> Next fi
> ///

> Es gibt brigens auch eine deutschsprachige NG zu VB .NET:


> Grsse,
> Herfried K. Wagner



Tue, 23 Aug 2005 05:44:18 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Build XML of folders, sub folders and files from specified folder

2. Deleting certian Files in Folders and there sub folders

3. folder in a folder in a folder...

4. Determining if a folder is a local pst folder or a imap (server folder)

5. Find Sub Folder Name within a Folder

6. Check for a Folder or Create a New Folder from within vb

7. How to access million files from a folder and move them to separate folders

8. Copy a file in a folder and past the copy in another folder

9. How to show the files/folder in a folder

10. use sendmessage to find folders and files in the folders in vb6

11. Copy the files and sub folders to another folder

12. Smart Delete Files And Smart Delete Folders Routines?

 

 
Powered by phpBB® Forum Software