Re: (Steve) Iterate files to determine latest and then kill others

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



I wouldn't trust FileSearch.

Instead, you could do something like this:

Declare an array to hold filenames

Filename = Dir$(filespec)
While Filename <> ""
add the filename to the array
redim array(1 to ubound(array+1) as string
Filename = Dir$
Wend

Sort the array - I think a string sort should work in this case
If not, declare the array as long instead and stuff it with the rightmost
digits of the filenames found coerced to long

Step through the sorted array from 1 to ubound - 1 and kill each file found in
the array, leaving the last one only.


In article <1123549588.915288.316770@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>, C mateland
wrote:
> Steve, thanks ever so much for the ideas on the Kill statement. This
> question is related to the same project. I'm working on a solution, but
> if you have one, feel free to throw it out there. :-)
>
> With the code below, I'm in the middle of a FileSearch where I verify
> the quantity of ppa files the user has in a folder that start with
> "a^main". If he has one, great, I move on. If he has none, no problem,
> got that handled, too.
>
> But if he has more than one, for example he has "a^main0002.ppa" and
> "a^main0014.ppa" and "a^main0022.ppa", I want to delete all but
> "a^main0022.ppa". (I don't know how multiple files could get in like
> that, but it will cause a problem. I have to whittle it down to the one
> highest "main" ppa before I can move on, you see.)
>
> So, I'm trying to code where, when more than one such file is found, it
> will examine the right 4 digits of the names. FWIW, when only one
> "main" file is found, I use the following code elsewhere that
> successfully grabs the name and the version (as integer). Maybe I can
> use like code in the iterations to find the latest of multiple "main"
> files, but I'm having brain-drain trying (although I'll stay at it).
>
> 'Get name of main ppa file
> sPpaMainName = Dir(.FoundFiles(1))
> 'Get version number of main ppa file
> iPpaMainVer = Mid(sPpaMainName, _
> Len(sPpaMainName) - 7, 4)
>
> Here is the file search I'm in where, if >1 "main" files are found,
> I'll have to iterate and keep only the latest.
>
> With Application.FileSearch
> .NewSearch
> .LookIn = sPpaFolder
> .SearchSubFolders = False
> .FileName = "a^main*.ppa"
> .Execute
> iPpaMainQty = .FoundFiles.Count
>
> If iPpaMainQty > 1 Then
> 'CODE: Keep newest file and kill others ???
> End If
>

-----------------------------------------
Steve Rindsberg, PPT MVP
PPT FAQ: www.pptfaq.com
PPTools: www.pptools.com
================================================


.



Relevant Pages

  • Re: Help end the testing tedium please
    ... When you call it from Consolidate ... Please assume that both the filename and the ... worksheet name are sequential. ... the file really were test1 thru test60, would the array statement need ...
    (microsoft.public.excel.programming)
  • Re: Help end the testing tedium please
    ... When you call it from Consolidate ... Please assume that both the filename and the ... the file really were test1 thru test60, would the array statement need ... With Selection ...
    (microsoft.public.excel.programming)
  • Re: How to find files with the same name -- different case?
    ... This awk program stores the converted filenames that it reads from stdin ... in lowercase and prints any new filename that matches case-insensitive... ... fis an associative array access with key abc; ... The increment will actually happen after the whole ...
    (comp.unix.shell)
  • Re: request for code review
    ... > changing the size of the array. ... char large enough to hold the LONGEST FILENAME STRING that the ...
    (comp.lang.c)
  • Re: request for code review
    ... > changing the size of the array. ... char large enough to hold the LONGEST FILENAME STRING that the ...
    (comp.lang.c)