Solved

How to determine if a directory exist within a certain path

Posted on 2006-07-13
3
190 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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

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…
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…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 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