Deleting entire directory structure MSDN tip46

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*
Jens979Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
KDivadConnect With a Mentor Commented:
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
 
VbmasterCommented:
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
 
VbmasterCommented:
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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
KDivadCommented:
If the app is for personal use, this should work for you:

x = Shell("Command /c deltree /y " & FolderToDelete & " > nul", vbMinimized)
0
 
MaquiaveloCommented:
 FileSystem.Kill <Path> (use kill to delete the entire structure or RmDir to delete a directory that is already empty.)
0
 
MaquiaveloCommented:
Maquiavelo changed the proposed answer to a comment
0
 
Jens979Author Commented:
Adjusted points to 75
0
 
Jens979Author Commented:
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
 
KDivadCommented:
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
 
VbmasterCommented:
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
 
TigerZhaoCommented:
MUST Dir all subdirectory to a collection, then loop this collection call Nuke oSubDir(I)
0
 
Jens979Author Commented:
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
 
KDivadCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.