Solved

Deleting entire directory structure MSDN tip46

Posted on 2000-02-26
13
243 Views
Last Modified: 2010-05-02
Hello experts!

I have a question on this MSDN example TIP 46. It is not working in my VB5 as I have written it. It do delete all files, also files in a subdirectory and the subdirectory it self. But if there is a sub directory in the subdirectory ~:-\, then the first sub directory do not get deleted though all the files does. I have not checked with further subs. Besides the directory "nicknavn" itself does not get deleted either.

'#####

Sub del_go()

Dim TempDir As String
TempDir = "../webpages/usr/" & nicknavn  '#This path is caused that it is a script I am working on for a webserver.
Nuke TempDir

End Sub

'#####

Sub Nuke(DirName As String)

Const ATTR_NORMAL = 0
Const ATTR_DIRECTORY = 16

Dim OriginalDir, FileName, NextFileName As String

OriginalDir = CurDir$
ChDir DirName
FileName = Dir$("*.*", ATTR_NORMAL)

Do While FileName <> ""
  Kill FileName
  FileName = Dir$
Loop

Do
  FileName = Dir$("*.*", ATTR_DIRECTORY)
  While FileName = "." Or FileName = ".."
    FileName = Dir$
  Wend

  If FileName = "" Then Exit Do
  Nuke (FileName)
Loop

ChDir OriginalDir
RmDir DirName

End Sub

'Can someone help with this?
Thanks!
/Jens*
0
Comment
Question by:Jens979
  • 4
  • 3
  • 3
  • +2
13 Comments
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2561432
Looks like the Do..Loop is wrongly coded, it should be something like this...

FileName = Dir$("*.*", ATTR_DIRECTORY)
Do While (Len(Filename) > 0)
  If Not ((FileName = ".") Or (FileName = "..")) Then
    Call Nuke(FileName)
  End If
  FileName = Dir$
Loop
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2561439
Why do you use the ChDir statement in this code? Why not using code like the following?...


Sub Nuke(DirName As String)

  Dim FileName As String

  If (Right$(DirName, 1) <> "\") Then DirName = DirName & "\"

  FileName = Dir$(DirName & "*.*", vbDirectory + vbArchive + vbHidden + vbReadOnly + vbSystem)
  Do While (Len(Filename) > 0)
    If CBool(GetAttr(Dirname & Filename) And vbDirectory) Then
      'Remove directory
      Call Nuke(Dirname & Filename & "\")
    Else
      'Remove file
      Kill Dirname & FileName
    End If
    Filename = Dir$
  Loop
  RmDir DirName

End Sub
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2561467
If the app is for personal use, this should work for you:

x = Shell("Command /c deltree /y " & FolderToDelete & " > nul", vbMinimized)
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2561559
 FileSystem.Kill <Path> (use kill to delete the entire structure or RmDir to delete a directory that is already empty.)
0
 
LVL 1

Expert Comment

by:Maquiavelo
ID: 2561565
Maquiavelo changed the proposed answer to a comment
0
 

Author Comment

by:Jens979
ID: 2561830
Adjusted points to 75
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Jens979
ID: 2561831
Maquiavelo; tried you line but it didnt work for me, so ... KDivad I tried your line; success! (with a little rearrangement of the command line)-(but how do I keep it from giving back the line that says the structure has been deleted???)
VBmaster; thank you, but ... one line to do the job is .. kind of easier, thank you though, all!
I have raised to 75 points now, so KDivad, I will like if you will help me to get the responder line from the command 'deltree' not showing, since it comes up on my webpage.
Can you help with that?
Thank alot!

/Jens*

0
 
LVL 5

Expert Comment

by:KDivad
ID: 2562278
The " > nul" (without the quotes, of course) should have prevented the line from showing up in the DOS window. If you change the properties of the deltree app, (right-click, properties, close on exit) the window should close as soon as it's done.

Let me know!
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2562455
Jens979: one line... that depends on how you look at things. Using the FileSystemObject will sure look like only one line to you but it will also mean you have another large dependency file. I do not have it on my system but I think the size is about 1mb. So looking at the size it should be no question about what solution to use.
0
 
LVL 5

Accepted Solution

by:
KDivad earned 75 total points
ID: 2562748
Vbmaster,
I think Jens was talking about my one line using deltree rather than Maquiavelo's using FileSystemObject. Shelling to deltree ain't pretty, but...

Jens,
What rearrangement of the command line did you do?
0
 
LVL 4

Expert Comment

by:TigerZhao
ID: 2564322
MUST Dir all subdirectory to a collection, then loop this collection call Nuke oSubDir(I)
0
 

Author Comment

by:Jens979
ID: 2567616
KDivad I did this;
x = Shell("c:\windows\Command\deltree /y " & "../webpages/usr/" & nicknavn & " > 0", vbHide)

It works fine, so I grade yo an A. Thanks to you all!!
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2567900
The "nul" I put in the line is actually supposed to be "nul" and not "0", but probably doesn't do anything at all since you aren't passing the line through the DOS command interpreter. You shouldn't need to hardcode the path to deltree as it should be in the environment path variable. You also don't need to seperate the first two actual text parts.
This line should work just as well for you, if you want to change it:

x = Shell("deltree /y ../webpages/usr/" & nicknavn, vbHide)

Glad I could help you!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Mysql vs Oracle 10 134
Visual Basic Excel Formatting error 4 81
MS Access Search and Replace Using VBA 6 57
Visual Studio 2005 text editor 10 35
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now