Speeding up application.filesearch

  • Thread starter Thread starter aarontraffas
  • Start date Start date
A

aarontraffas

Guest
I'm exporting data from a table in which one of the fields will be based on the pictures in a networked directory.

With Application.FileSearch
.LookIn = strPicturePath
.Filename = strICN & "?.JPG"
.SearchSubFolders = False
If .Execute(SortBy:=msoSortByFileName, SortOrder:=msoSortOrderAscending) > 0 Then
intPictures = .FoundFiles.Count
If intPictures = 0 Then
strPictures = strICN & ".JPG"
ElseIf intPictures = 1 Then
strPictures = strICN & ".JPG " & strICN & "A.JPG"
ElseIf intPictures = 2 Then
strPictures = strICN & ".JPG " & strICN & "A.JPG " & strICN & "B.JPG"
.....and so forth

The problem is that for a table with 500+ items, it takes several minutes. Is there a way to speed this up such as storing the results from one filesearch in memory and searching that somehow? How would I access that information?
 
How many ElseIf's do you have in the "....and so forth" portion?

If you do more than about five or six ElseIfs, you really should consider something along the lines of CASE statement.

Next, what isn't clear is whether there is more program structure outside what you showed us. It seems that there has to be, but since you didn't show us what else you were doing, that is hard to tell.

Next, there is the funky little file sort issue. You can do this all in a single pass, perhaps, but it is not clear where you are going with this. In particular, what you are going to do with "strPictures" when you have built that string? As well as where strICN comes from and what it means.

Some random thoughts here...

Your speed problem comes from having to search the directory several times based on the outer ICN stuff. But directory searches can be terribly expensive in time (as you are finding out).

If you sort the returned filenames from the filesearch and made the search based on *.JPG, then you can keep track of the similar names and build the string strPictures on the fly in something more like a one-pass algorithm.

At least presumably, the first name in any sequence is the "base" name and all other names are that base + A, B, C, etc. You've already asked for the names to be sorted.

So start a loop with strPictures empty and create something called strBase. Make it empty, too.

Now step into the .FoundFiles list one file at a time with a "definite count" determined from .FoundFiles.Count.

On the first step, your base is empty. Make the base equal to the file name portion of your returned string. But don't do anything else with that yet.

On the next step, see if the name you have in this file matches strBase + the letter A (or B or C or whatever, you can set up another string thing to predict the next expected name if they are that predictable...) If it does, you just update strPictures.

Now if you see a name that looks like it must be a new "base" name 'cause it doesn't partially match the previous base, you could do whatever you were going to do with strPictures before you add in the new name. Like append it to your table or update something. Then erase the strPictures string and load the new strBase name.

When you reach the end of the loop, if you have not emptied strPictures one last time, you have to remember to do that, but otherwise you are done in only one file search.

My way of thinking is, if you let the table drive the algorithm, the repeated directory search is gonna eat your socks. But if the potential strICN happens to match the strBase contents and is also a key in the table, you could do the loop across the results of a single file search and use strBase to select the record you wanted to update using keys - a much faster item to consider. Not only that, but if I read correctly what you were trying to do, my suggested loop only updates when you find a name not consistent with previous name sequences - so you don't have to touch the DB that often, either. Does that make sense in your context? Even if that is not exactly right, look at rethinking what it is that will drive the outermost loop. You want the fastest thing INSIDE the nested loop structure and the slowest thing OUTSIDE the inner parts of the loop.
 
Icn

Thanks, Doc. Sorry for not being more elaborative...first post for me....

The strICN is merely our inventory number. You can think of it as the primary index for the table. The table is being exported to a text file line by line in a specific order. Each record includes a value not stored in any table, the strPictures variable, which needs to be generated based on the number of files in the networked directory at the time of the export. The speed issue is definately with the filesearch, not the if/then conditions to generate strPictures. What I really need to know is just the number of pictures that exist for each base, as you stated, but it has to be done in the order of the export and not in the order the pictures exist on the server. At least for my thought process it must. I'm still pretty new to this and quite limited in the ability to change any table structure. That's why I thought I could somehow store the filesearch in memory and work with it there rather than doing a filesearch for each item as it's exported. I appreciate any suggestions you might have.
 

Users who are viewing this thread

Back
Top Bottom