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

x
?
Solved

Finding and Deleting a Directory

Posted on 1999-06-26
8
Medium Priority
?
152 Views
Last Modified: 2010-05-02
How can you search to see if a particular directory exists in a directory list box (Dir1), in VB6?

0
Comment
Question by:slimbx
[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
8 Comments
 
LVL 10

Accepted Solution

by:
viktornet earned 0 total points
ID: 1519969
here, try this...

function DirectoryExists(Name as string) as Boolean
  dim Code as Integer
  Code = GetFileAttributes(Name)
  DirectoryExists = (Code <> -1) and (FILE_ATTRIBUTE_DIRECTORY and Code <> 0)
end function

Hope this helps....

..-=ViKtOr=-..
0
 

Author Comment

by:slimbx
ID: 1519970
What I am trying to do is when you choose to delete a directory, to first search and see whether or not the directory even exists. If it does, then allow the user to delete it; if not, kick them out the sub

I am not sure whether or not your code will work, because I am unsure on how to implement it. Could you help me out again?


Dim direct As String
Dim check
       
    'deletes a directory
               
    direct = InputBox("Please insert in the exact name of the directory to permanently delete.", "Deleting a Directory")
    If direct = "" Then Exit Sub
   
    'searches for the directory name.
    Call DirectoryExists(direct)
   
   
    check = MsgBox("Do you want to delete the directory " & direct & "?", vbYesNo + vbQuestion, "Deleting a Directory")
    If check = vbNo Then Exit Sub
       
    '2x check
    check = MsgBox("Are you sure you want to permanently delete the directory " & direct & "?", vbYesNo + vbQuestion, "Deleting a Directory")
    If check = vbNo Then Exit Sub
           
    RmDir (direct)
    MsgBox ("The directory " & direct & " was successfully deleted."), vbOKOnly + vbInformation, "Directory Deleted Successfully"
    Form1.Refresh
End Sub
0
 
LVL 5

Expert Comment

by:KDivad
ID: 1519971
If you are going to have the user input a folder name and you want to make sure it exists, you might be better off using a dirlist control. It would be easier for them to find the folder, and you would be more certain that it exists in the first place.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 10

Expert Comment

by:viktornet
ID: 1519972



     Dim direct As String
     Dim check
             
         'deletes a directory
                     
         direct = InputBox("Please insert in the exact name of the directory to
     permanently delete.", "Deleting a Directory")
         If direct = "" Then Exit Sub
         
         'searches for the directory name.
if DirectoryExists(direct) then
               
         check = MsgBox("Do you want to delete the directory " & direct & "?",
     vbYesNo + vbQuestion, "Deleting a Directory")
         If check = vbNo Then Exit Sub
             
         '2x check
         check = MsgBox("Are you sure you want to permanently delete the directory "
     & direct & "?", vbYesNo + vbQuestion, "Deleting a Directory")
         If check = vbNo Then Exit Sub
                 
         RmDir (direct)
         MsgBox ("The directory " & direct & " was successfully deleted."), vbOKOnly
     + vbInformation, "Directory Deleted Successfully"
         Form1.Refresh
else
  MsgBox "The directory " & direct & "does not exist. Please try again."
end if

End Sub

Let me know if you're still having any problems...

btw- GetFileAttributes() is a WIn API so you have to declare it.. the same way as ShellExecute() was in your previous question ;-))

..-=ViKtOr=-..
0
 

Author Comment

by:slimbx
ID: 1519973
Okay, I am not sure if it is working ornot. When I compiled and ran the program, when I chose to delete a directory and the function was called, I recieved a Variable not defined. The variable was the FILE_ATTRIBUTE_DIRECTORY. So I defined it and ran the program as below (I had it uncommented when I ran it), and it did not find the directory that I wanted to delete which is at C:\Ward. Do you have nay other suggestions?

Oh yeah, I declared the GetFileAttributes in general declarations of the form
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1519974
ok, here is what I tried and it worked just fine....

========MODEULE1.BAS===========
Option Explicit

Public Const FILE_ATTRIBUTE_DIRECTORY = &H10

Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long

==========FORM1.FRM==========

Option Explicit

Function DirectoryExists(Name As String) As Boolean
        Dim Code As Integer
        Code = GetFileAttributes(Name)
        DirectoryExists = (Code <> -1) And (FILE_ATTRIBUTE_DIRECTORY And Code <> 0)
      End Function
     
Private Sub Command1_Click()
Dim direct As String
           Dim check
                     
               'deletes a directory
                           
               direct = InputBox("Please insert in the exact name of the directory to permanently delete.", "Deleting a Directory")
               If direct = "" Then Exit Sub
                 
               'searches for the directory name.
      If DirectoryExists(direct) Then
                     
               check = MsgBox("Do you want to delete the directory " & direct & "?", vbYesNo + vbQuestion, "Deleting a Directory")
               If check = vbNo Then Exit Sub
                     
               '2x check
               check = MsgBox("Are you sure you want to permanently delete the directory " & direct & "?", vbYesNo + vbQuestion, "Deleting a Directory")
               If check = vbNo Then Exit Sub
                         
               RmDir (direct)
               MsgBox ("The directory " & direct & " was successfully deleted."), vbOKOnly + vbInformation, "Directory Deleted Successfully"
               Form1.Refresh
      Else
        MsgBox "The directory " & direct & " does not exist. Please try again."
      End If
End Sub


here is what I tried and it worked just fine...

c:\windows\desktop\new
c:\windows\desktop\NEW
c:\windows\desktop\new\
c:/windows/desktop/new

the target directory was "new" w/o the quotes of course ;)

all these worked just fine...

could you copy and paste the code above and try it once again? be careful for the spelling of the directory, and be sure that it's really there...

..-=ViKtOr=-..
0
 

Author Comment

by:slimbx
ID: 1519975
Thank you very much, worked like a charm!


-slimbx-
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1519976
No Problem!!
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

730 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