Solved

Deleting entire directory structure MSDN tip46

Posted on 2000-02-26
13
248 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

717 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