?
Solved

Its been ages, so simple ive forgotten

Posted on 2004-12-01
7
Medium Priority
?
206 Views
Last Modified: 2010-05-02
Hey experts - not exactly a hard one but its been so long i cant remember how to do this.

If a directory, for example C:\Program Files\Directory, does not exist then make it, and if it does exist then delete it and make it again.

So -    Dir exist?         Yes - Delete and remake
                                No - Make

I know its something to do with mkdir, rmdir but i cant remember how to see if it exists or not.  ive been playing around with

If ("C:\Program Files\Directory") = "" Then    ..... but to no avail.

Thanks Experts, not very challanging i know but its still points :-)

0
Comment
Question by:cofneverlivetotell
7 Comments
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 12719599
hi
try this
'Dont forget to add referece
'Project Menu->References
'   Microsoft Scripting Runtime
'--------------------------------------------------------------------------------------------
Private Sub MakeDirectory(sFolderName As String, Optional ByVal Overwrite As Boolean)
Dim objFso As New FileSystemObject
On Error GoTo Er:
    If Trim(sFolderName) = "" Then
        MsgBox "Invalid Folder Name"
        Exit Sub
    End If
    If objFso.FolderExists(sFolderName) Then
        If Overwrite Then
            objFso.DeleteFolder (sFolderName)
            objFso.CreateFolder (sFolderName)
        End If
    Else
        objFso.CreateFolder (sFolderName)
    End If
    Exit Sub
Er:
    MsgBox "Unable to Create/Delete Folder : " & Err.Description
    Err.Clear

End Sub
'Usage
'Private Sub Command1_Click()
'    Call MakeDirectory("d:\test", True)
'End Sub
'------------------------------------------
;-)
Shiju
0
 
LVL 2

Author Comment

by:cofneverlivetotell
ID: 12719923
:-|   isnt that a *little* overcomplicated?  lol i just need to see if a dir exists or not and make a decision, a few lines like i have will do it but i cant remember how.  like you can see if a string is empty and act upon  it with

If strTeststring = "" then
codehere
End if

Get my thinking?  Ive got a perfect app apart from this one little thing (this is an updater for sophos antivirus, ive made a program to download and unzip the virus identities to the ide folder, but i need to delete another folder if it exists and remake it, or just make if it doesnt exist)

Cof
0
 
LVL 35

Accepted Solution

by:
mvidas earned 400 total points
ID: 12720018
Cof,

Are you thinking of something like

 Dim dPath As String
 dPath = "C:\Program Files\Directory"
 On Error Resume Next
 MkDir dPath
 If Err.Number <> 0 Then
  Kill dPath & "\*.*"
  RmDir dPath
  MkDir dPath
 End If
 On Error GoTo 0

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

Author Comment

by:cofneverlivetotell
ID: 12720098
Yup, that does it great.  Thanks mvidas
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 12732597
I think you were looking for something like this:

If Dir("C:\Program Files\Directory", vbDirectory) <> "" Then
    MsgBox "Directory exists"
Else
    MsgBox "Directory Does Not Exist"
End If
0
 
LVL 2

Author Comment

by:cofneverlivetotell
ID: 12738522
Yeah, that was basicly what i was looking for, but mvidas gave another working and reasonably simple answer... If it wasnt accepted i would give you the points for simplicity, but no can do :-(

Sorry
Cof
0
 
LVL 28

Expert Comment

by:vinnyd79
ID: 12744856
I realized the answer was already accepted but it's not all about the points.I thought the info might help you as in my opinion it is a better solution then error trapping and from your comments it sounded as if it was what you were looking for.

Vinny
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

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…

578 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