Solved

How to determine if a directory exist within a certain path

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

Technology Partners: 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

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 …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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
Course of the Month8 days, 5 hours left to enroll

617 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