Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to determine if a directory exist within a certain path

Posted on 2006-07-13
3
Medium Priority
?
196 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 93

Accepted Solution

by:
Patrick Matthews earned 2000 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 93

Expert Comment

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

Featured Post

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.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
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…
Suggested Courses

715 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