?
Solved

Deleting entire directory structure MSDN tip46

Posted on 2000-02-26
13
Medium Priority
?
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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
 

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Question has a verified solution.

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

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
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 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…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

765 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