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.
Stephen RoesnerAnalysisAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CompProbSolvCommented:
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?
Stephen RoesnerAnalysisAuthor Commented:
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?
Stephen RoesnerAnalysisAuthor Commented:
and yes there will only be 1 ced and group file
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

James BaileyProject PlannerCommented:
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
Gustav BrockCIOCommented:
Dir is great, fast, and simple but a little special.
Here is how you could do:
Public Function ProcessFiles()

    Const cstrPathCI    As String = "C:\ERPDP\IVista\FileIn\"
    Const cstrFileCed   As String = "CED"
    Const cstrFileGrp   As String = "Group"
    
    Dim strFile         As String
    
    strFile = Dir(cstrPathCI & cstrFileCed & "*.*", vbNormal)
    If strFile <> "" Then
        Debug.Print "+", cstrPathCI & strFile
        ' Process CED file.
    End If
    strFile = Dir(cstrPathCI & cstrFileGrp & "*.*", vbNormal)
    If strFile <> "" Then
        Debug.Print "+", cstrPathCI & strFile
        ' Process Group file.
    End If
    
    strFile = Dir(cstrPathCI, vbNormal)
    Do Until strFile = ""
        If Left(strFile, Len(cstrFileCed)) = cstrFileCed Then
            ' Ignore CED file.
            Debug.Print "-", cstrPathCI & strFile
        ElseIf Left(strFile, Len(cstrFileGrp)) = cstrFileGrp Then
            ' Ignore Group file.
            Debug.Print "-", cstrPathCI & strFile
        Else
            Debug.Print "+", cstrPathCI & strFile
            ' Process other file.
        End If
        ' Get next other file if any.
        strFile = Dir
    Loop
    
End Function

Open in new window

/gustav

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
James BaileyProject PlannerCommented:
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.
Gustav BrockCIOCommented:
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
Stephen RoesnerAnalysisAuthor Commented:
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.
Gustav BrockCIOCommented:
That is not true. Did you try the code? What was the debug output?

It was tested successfully here.

/gustav
Stephen RoesnerAnalysisAuthor Commented:
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 ?
Gustav BrockCIOCommented:
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
Stephen RoesnerAnalysisAuthor Commented:
Once again Gustav you gave me an excellent solution. Thank you for your assistance!
Gustav BrockCIOCommented:
You are welcome!

/gustav
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.