Solved

How to determine if a directory exist within a certain path

Posted on 2006-07-13
3
188 Views
Last Modified: 2010-04-30
I'm trying to create a program in VB6 that will look in a certain path and determine if a directory exist.  The directory ends in .EMS, but the beginning characters of the directory name are randomly created.  In other words, it could have any of the following names:

ABCXYZ.EMS, HKLMP.EMS, WXLPV.EMS, etc.

Even though they have a "." in the directory name, they are directories, not files.  I want to locate the directory and copy a file into the directory.  The program would looks something like this.

Do
   Look at file
   If file ends in .EMS
      copy \\server\share\file.ini to c:\MyFolder\<directory just found that ends in EMS>\
   End If
While there are still directories within folder c:\MyFolder or if EMS directory was found

I'm not sure if FSO would be the root to go?  There will be only one directory within this folder that ends in EMS.  I thought maybe I should look at the last 3 characters of each directory name and when I find one that ends in EMS, then copy my file into the directory.

My skills are very limited in VB so please help!
Thanks,
BPL
0
Comment
Question by:bpl5000
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
ID: 17105174
Hi bpl5000,

Add this function to your project.


Public Function FindMyFolder(StartFolder As String, SearchName As String) As String

' based on a submission by angelIII in Experts Exchange post:
' http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21918709.html

' Looks for a subfolder within a starting folder and its "descendants".  When the function
' finds the first matching subfolder, it returns the path to that subfolder.  If there is no
' match, it returns an empty string

    Static fso As Object 'scripting.filesystemobject
   
    If fso Is Nothing Then Set fso = CreateObject("scripting.FileSystemObject")

    Dim folder As Object 'scripting.folder
    Dim Result As String

    Set folder = fso.GetFolder(StartFolder)

    Dim subfolder As Object 'scripting.folder
   
    For Each subfolder In folder.SubFolders
        If UCase(subfolder.Name) Like "*" & UCase(SearchName) Then
            Result = subfolder.Path
            GoTo Done
        Else
            Result = FindMyFolder(subfolder.Path, SearchName)
            If Result <> Empty Then GoTo Done
        End If
    Next

Done:
    FindMyFolder = Result
   
End Function




Then call it like this from your sub:


DestPath = FindMyFolder("c:\MyFolder", ".EMS")
If DestPath = "" Then
    'what happens if the folder is not found?
Else
    'assumes you have an object variable of type Scripting.File, named fil
    fil.Copy DestPath, True
End If


Regards,

Patrick
0
 
LVL 5

Author Comment

by:bpl5000
ID: 17105744
Thanks Patrick... that was quick.  Works great!
0
 
LVL 92

Expert Comment

by:Patrick Matthews
ID: 17107818
Glad to help :)
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
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…

759 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

20 Experts available now in Live!

Get 1:1 Help Now