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

C

c mateland

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
 
C

c mateland

c said:
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. :)

Okay, this seems kind of a whacky solution, but it works. Could you
take a glance at it and give me some commentary? Thanks.

(Just came out of a FileSearch where iPpaMainFiles = FoundFiles.Count
of the "main" ppa files. I now want to find the highest version of the
ppa and delete the rest. A file's title would be like this:
"a^main0001.ppa")

If iPpaMainFiles > 1 Then 'Too many "main" ppa's
Dim sMainPpa As String, sLastVerNm As String
Dim iThisVer As Integer, iHighVer As Integer
'Get iteration's 1st file's name
sMainPpa = Dir(sPpaFolder & "\a^main*.ppa")
'Iterate "main" files for highest version
Do While sMainPpa <> ""
'Get version number from its title
iThisVer = Mid(sMainPpa, Len(sMainPpa) - 7, 4)
'Compare ver numbers and maybe delete
If iThisVer > iHighVer Then 'found higher ver
'Delete older file
If sLastVerNm <> "" Then
On Error Resume Next
Kill sPpaFolder & "\" & sLastVerNm
DoEvents
On Error GoTo 0
End If
'Set new high ver number and name
iHighVer = iThisVer
sLastVerNm = sMainPpa
End If
'Compare to next "main" file
sMainPpa = Dir()
Loop
End If
 
C

c mateland

Steve said:
Kind of like doing the sort w/o ending up with a sorted array.
W/o stepping through all the details, this should work.

Thanks, Steve, for all your input and validation. It meant a lot to me.


I demo'ed the system to IT directors yesterday and everything worked
well, and what's more, IT loved it and recognized the high value and
low impact (no outside programs). So, they're onboard and so impressed
is global HQ that they want this implamented worldwide. We just have to
work out tech-details for users around the globe who are remotely
connected or on other networks. (That's easy compared to what I went
through conceptualizing and developing this thing.) Also, this may very
well wind up being a model program for other template-based tasks
within our corporation.

So, it now seems the reward was well worth the trouble. I admit,
however, I thought all along it would be because I truly believed in
the value of this concept.

Once again I thank you for all your help. It's wonderful to come here
and get aid from a generous and experienced expert such as yourself.

-Charles Mateland
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top