Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Deleting directory--50pts

Posted on 1999-07-20
17
Medium Priority
?
130 Views
Last Modified: 2012-05-04
How can I delete a directory from a dir list box in VB5?  I have a popup menu created so I can just right-click and "delete folder" but I can't seem to make it delete the selected folder.  Thanks in advance!
0
Comment
Question by:neymeyer
[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
  • 10
  • 5
  • 2
17 Comments
 

Author Comment

by:neymeyer
ID: 1526212
Edited text of question.
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1526213
what code are you using to delete it?
private sub mnuDelete_click
kill dir1.path
end sub
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1526214
if the previous is what your using, you need to loop through and delet all the files in the directory ie:

strpath = dir(dir1.path & "*.*")
while strpath <> ""
    if strpath <> "." and strpath <> ".." then
        kill strpath
    end if
wend
kill dir1.path
0
Independent Software Vendors: 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 2

Expert Comment

by:BeedleGuis
ID: 1526215
oops forgot to dir() again:
strpath = dir(dir1.path & "*.*")
while strpath <> ""
    if strpath <> "." and strpath <> ".." then
        kill strpath
    end if
    strpath = dir()
wend
kill dir1.path

0
 
LVL 2

Accepted Solution

by:
BeedleGuis earned 200 total points
ID: 1526216
Public Declare Function RemoveDirectory Lib "kernel32" Alias "RemoveDirectoryA" (ByVal lpPathName As String) As Long

strpath = dir(dir1.path & "*.*")
while strpath <> ""
    if strpath <> "." and strpath <> ".." then
        kill strpath
    end if
    strpath = dir()
wend
removedirectory dir1.path

0
 

Author Comment

by:neymeyer
ID: 1526217
I think that is what I am after.  That will delete the directory I have selected?  If it is, answer and collect the points!
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1526218
the directory must be empty before calling the api function,  if the directory has subfolders, you'll need to empty and remove those first
0
 

Author Comment

by:neymeyer
ID: 1526219
oops....I guess you already did!  sorry!
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1526220
either way should work as long as the directory is empty.
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1526221
also be aware, dir1.path reflects the 'openfolder' in it's list.  put this code in your form to see what I mean:
Private Sub Dir1_Change()
MsgBox Dir1.Path
End Sub

Private Sub Dir1_Click()
    MsgBox Dir1.ListIndex
End Sub

0
 
LVL 5

Expert Comment

by:mayhew
ID: 1526222
I always use the following:

RmDir Dir1.List(Dir1.ListIndex)
Dir1.Refresh

0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1526223
dir1.listindex is not a valid path...
0
 
LVL 5

Expert Comment

by:mayhew
ID: 1526224
Yes, I know.

Read what I wrote.
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1526225
your right though,  you need rmdir instead of kill for dir's.  Or the api call will work too
0
 

Author Comment

by:neymeyer
ID: 1526226
The only problem I am still having is that running this on WinNT, if I remember right, I can't use the "." and ".." becuase NT doesn't use DOS and that is DOS directory notation.  It tries to delete files in the directory even when there are none, and this produces an error.  I suppose I could just trap the error and go around it, but I would rather do it right.  Any suggestions?
0
 
LVL 2

Expert Comment

by:BeedleGuis
ID: 1526227
what's the error description?
0
 

Author Comment

by:neymeyer
ID: 1526228
I have it now.  Here is the code if anyone is interested.  Thanks to all for the help!

Private Sub mnuDirectoryRem_Click()
    strpath = File1.Path & "\" & "*.*"
    File_Exists (strpath)
    If File_Exists(strpath) Then
        Kill strpath
    End If
    strpath = File1.Path
    RmDir strpath
    File1.Path = strpath & "\" & ".."
    Dir1.Path = File1.Path
    Dir1.Refresh
    Dir2.Refresh
End Sub

Function File_Exists(ByVal PathName As String) As Boolean
    File_Exists = (Dir$(PathName) <> "")
End Function

0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
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

688 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