Solved

How to determine if a directory exist within a certain path

Posted on 2006-07-13
3
193 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
[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
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
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…

751 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