Link to home
Start Free TrialLog in
Avatar of Stephen Roesner
Stephen RoesnerFlag for United States of America

asked on

Find a Specific File to Process

I have a folder that will contain multiple files.
However I want to process 2 specific files first before any others.
These 2 files will start with a standard beginning to their names.
File 1 will always start with the word CED.....
File 2 will always start with the word Group...
if it helps I have this code already

strPathCI="C:\ERPDP\IVista\FileIn\"
strfilenameN0=""
strfilenameN0=Dir(strPathCI & "*.*", vbNormal)
while strfilenameN0 <> ""

which identifies the folder and grabs the first file
and loops thru them but I want to do the first 2 files before any other.

I want to search for file 1 and do its process
then search for file 2 file and do its process
then ill loop for the remaining files and store them form later processing
can anyone help? It would be greatly appreciated.
Avatar of CompProbSolv
CompProbSolv
Flag of United States of America image

This logic might do the trick:
Search for CED*.*
Process the file
Search for Group*.*
Process the file
Loop through all files
If the name <> CED*.* and the name <> Group*.* then process the file

Can you be sure that there will be no more than one file starting with CED and with Group?
Avatar of Stephen Roesner

ASKER

What would be the code to search for a file that's where I'm stuck - I know how to use dir to loop thru file by file but what function or method search's for a specific file?
and yes there will only be 1 ced and group file
Avatar of James Bailey
James Bailey

Have you tried the file search object.  I t could look like this:
With Application.FileSearch
    .NewSearch
    .LookIn = "C:\My Documents"  'search This path for the file or files
    .SearchSubFolderTrueFalse
    .FileName ced*.*"  'Drop  second asterisk if file is workout an extension
    .FileType = msoFileTypeAllFiles
If .Execute() > 0 Then
        MsgBox "There were " & .FoundFiles.Count  & " file(s) found."
        For i = 1 To .FoundFiles.Count
            MsgBox .FoundFiles(i)
        Next i
End With


With Application.FileSearch
    .NewSearch
    .LookIn = "C:\My Documents"
    .SearchSubFolders = True
    .FileName RunGroup*.*"
    .MatchTextExactly = True
    .FileType = msoFileTypeAllFiles
    If .Execute() > 0 Then
            MsgBox "There were " & .FoundFiles.Count & " file(s) found."
            For i = 1 To .FoundFiles.Count
                MsgBox .FoundFiles(i)
            Next i
End With

Open in new window

You can use one or more executions of FileSearch to access other filenames in the folder.

I hope this is some help
ASKER CERTIFIED SOLUTION
Avatar of Gustav Brock
Gustav Brock
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I understood the questioner to have found some fault with DIR.

If you're in love with the specialness of  dir. Well, I,  I must have missed something.
Dir is not faulty, so I read it as the implementation was causing troubles.
It usally is the repeated call:

     strFile = Dir

that causes some confusion.

/gustav
Gustav my old friend, James is correct. I already have code in place that does what you suggest, in fact, I use that code a lot. But in this situation, what I am trying to accomplish, is finding these 2 files first and processing them ---- then going thru the rest of the files after that and processing them. With the code I have and that you suggested it basically process's whatever file is first and goes to last and that defeats my purpose. And Dir is a great function but it just doesn't work in this case. I needed some kind of search and find method. I will try James code Monday morning when I get back to work and see if it works for me. But I am open to all your brilliance if u have any ideas. You have helped me a lot in so many occasions.
That is not true. Did you try the code? What was the debug output?

It was tested successfully here.

/gustav
Gustav my sincere apologies your code works perfectly - and it fits my existing code so I will be using it - simple and to the point like always with your code... thank you. However, on a teaching note... I'm not sure how it works LOL. I tried changing the const back to strings which I had already in my code and it didn't find the files so I went back to the const and changed my code and it worked perfectly. Is the trick to this using the const with the dir command? Just curious ?
What I relief.

The simple secret of Dir is that - when called with no parameters - it uses the last provided parameters and returns the next found item if any. When done, it returns a zero-length string.

It should pose no problem to use string variables - for example:

    Dim strPathCI    As String
    Dim strFileCed   As String
    Dim strFileGrp   As String
    strPathCI = "C:\ERPDP\IVista\FileIn\"
    strFileCed = "CED"
    strFileGrp = "Group"

/gustav
Once again Gustav you gave me an excellent solution. Thank you for your assistance!
You are welcome!

/gustav