Solved

Deleting entire directory structure MSDN tip46

Posted on 2000-02-26
13
242 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

760 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

18 Experts available now in Live!

Get 1:1 Help Now