Now let's say you didn't know how many files are in this sequence, and needed to know how long it is. Or, let's say you knew a section in the middle of the sequence had been deleted, and needed to find the location of the gap.D:\my_folder\imgseqframe_000001.jpg
D:\my_folder\imgseqframe_000002.jpg
D:\my_folder\imgseqframe_000003.jpg
...
D:\my_folder\imgseqframe_100000.jpg
Ordinarily what you would have to do is count up from 1, and check if each file exists. This is very slow. The code here does it much faster by moving upwards but in decreasing increments, to get ever more precision until it identifies the first non-existent file. It then returns the number immediately below that.
The test folder I have been using contains 200,000 files, with a gap of 1000 after #111875. Execution time is 2600ms doing it the ordinary way, or 3ms using this code.
It can be an extremely fast solution. However, this fastness depends on the minimum size of gap. If you know that the gap is just one or two files, this code probably won't be much use.
Since the gap is going to be at least 1000 files, we can afford to jump around in increments of 995, before getting more precise. The maxjumpsize variable must be smaller than the gap can possibly be.
Finally, you will need to adapt this code for the file-name format involved. I have written it to work for the example given above. I dare say a more universal solution could be created using RegEx.
Code: Select all
Procedure.i FindLastFileByCountingUpFrom1(folder.s,pfx.s,ext.s,fillzeroes.i)
If Left(ext,1)<>"." : ext="."+ext : EndIf
highest_exist_n.i = 0
Repeat
highest_exist_n+1
testfn.s = folder+pfx+RSet(Str(highest_exist_n),fillzeroes,"0")+ext
If FileSize(testfn) = -1
ProcedureReturn highest_exist_n-1
EndIf
ForEver
EndProcedure
Procedure.i FindLastFileByCountingUpIncrementallyFrom1(folder.s,pfx.s,ext.s,fillzeroes.i,maxjumpsize.i)
If Left(ext,1)<>"." : ext="."+ext : EndIf
If FileSize(folder+pfx+RSet(Str(1),fillzeroes,"0")+ext)=-1
ProcedureReturn 0
EndIf
highest_exist_n.i = 1
Repeat
n.i = highest_exist_n+maxjumpsize
testfn.s = folder+pfx+RSet(Str(n),fillzeroes,"0")+ext
If FileSize(testfn) = -1
Break
EndIf
highest_exist_n = n
ForEver
jumpsize = maxjumpsize
Repeat
jumpsize / 2
;Debug "JUMPSIZE: "+Str(jumpsize)
If jumpsize<1 : Break : EndIf
n = highest_exist_n+jumpsize
testfn.s = folder+pfx+RSet(Str(n),fillzeroes,"0")+ext
If FileSize(testfn) = -1
Continue
EndIf
highest_exist_n = n
ForEver
Repeat
testfn.s = folder+pfx+RSet(Str(highest_exist_n+1),fillzeroes,"0")+ext
If FileSize(testfn) = -1
Break
EndIf
highest_exist_n+1
ForEver
ProcedureReturn highest_exist_n
EndProcedure
frmfolder.s = "D:\my_folder\"
pfx.s = "imgseqframe_"
fillZeroes.i = 6
ext.s = "jpg"
starttime.d = ElapsedMilliseconds()
n.i = FindLastFileByCountingUpFrom1(frmfolder,pfx,ext,fillZeroes)
Debug Str(n)+" ("+StrD(ElapsedMilliseconds()-starttime)+" ms)"
starttime.d = ElapsedMilliseconds()
n.i = FindLastFileByCountingUpIncrementallyFrom1(frmfolder,pfx,ext,fillZeroes,995)
Debug Str(n)+" ("+StrD(ElapsedMilliseconds()-starttime)+" ms)"