http://www.awitness.org/delphi_pascal_tutorial/index.html The following routines will batch load numbered filenames, in this example getting only the first filename of the sequence from an OpenDialog and then loading the filenames into a listbox, to give a user visual feedback. A numbered file would be in the following form for this routine to batchload them - file001.jpg, file002.jpg ... file189.jpg, file201.jpg Note that in the example above the routines below will stop loading the filenames at 189 (the search for filename file190.jpg would return false... Filenames can also be composed only of numbers 061.txt, 062.txt, ... 099.txt In this example the batchloading would start at the file numbered 61 Numbers can also be positioned at the beginning of the filename 1000file.bmp, 1001file.bmp ... 2098file.bmp {etc} {*************************************************************************** } {---------------NUMERICAL LOAD -------------------------------} { The following procedure prepares variables and then uses them to call the BatchLoad routine. } procedure TForm1.NumericalLoad; var TruncatedFileName:string; {the file name with no extension} Number, FileText:string; {the number portion of the string (002) and the text (file) ie file002} NumberPos:integer; {the position of the number string 5 in the above example} FileNameAndPath:string; begin FileNameAndPath:=OpenDialog.Files.Strings[0]; {use an open dialog to get the first filename} TruncatedFileName:=RemoveFileNameExtension(ExtractFileName(FileNameAndPath)); Number:=ExtractFileNumberString(TruncatedFileName); if Number='' then MessageDlg('The file is not a valid numbered file', mtError, [mbOk], 0) else begin NumberPos:=Pos(Number, TruncatedFileName); {because of the structure of the code below a number at the beginning of a filename will always take precedence over a number at the end of the file name (assuming they are separated by text) this is true even if the two numbers are identical} FileText:=TextInFileName(TruncatedFileName, Number, NumberPos); {locate the text portion, if any, of the filename ie file002 number string is 002 and the filetext is file the numberpos in this case would be 5 this info and extension will be used to construct new numerical file names to do a batch load of all the numbered files until fileexists is false ie file003, file004 etc} BatchLoadNumberedFiles(FileNameAndPath, TruncatedFileName, Number, FileText, NumberPos); end; {else} end; {**************************************************************************} {NOTE: pass only the filename ie file.txt and not the path in order to get back only the 'truncated file name without the extension to call with a full path and file name use ExtractFileName(string) in the call to this function so that it receives only the filename and not the path} function TForm1.RemoveFileNameExtension(EFileName:string):string; begin Result:=EFileName; if pos('.', Result) <> 0 then Result:=copy(Result, 1, pos('.', Result)-1); end; {*************************************************************************** } {extract the number associated with the file from either the beginning or the tail end of the filename - input filename only - no path, no extension if the file does not either begin or end with a valid number string the function returns a null string} function TForm1.ExtractFileNumberString(EFileName:string):string; var c:char; i:integer; valid:boolean; num, temp:string; begin Result:=''; i:=0; num:=''; Valid:=ValidNumberedFile(EFileName); If Valid then begin c:=EFileName[1]; {code extracts number from the beginning of filename if it exists} if ((ord(c)>47) and (ord(c)<58)) then {0 TO 9} repeat i:=i+1; c:=EFileName[i]; if ((ord(c)>47) and (ord(c)<58)) then num:=num+c; {0 to 9 append onto string} until ((i=length(EFileName)) or ((ord(c)<48) or (ord(c)>57))) {quit when text or end of string} else begin {num at end of string} i:=length(EFileName)+1; repeat i:=i-1; c:=EFileName[i]; if ((ord(c)>47) and (ord(c)<58)) then num:=num+c; {build num string from end of input string} until ((i=1) or ((ord(c)<48) or (ord(c)>57))); temp:=''; {string is backwards since we counted down from end of filename , flip it} for i:=length(num) downto 1 do temp:=temp+num[i]; num:=temp; end; {else} Result:=num; end; {if valid} end; {*************************************************************************** *} {a valid numbered file either has a number in the first or the last position ie. file001.txt or 001file.txt 0=ord(48) and 9=ord(57)} function TForm1.ValidNumberedFile(EFileName:string):boolean; var first, last :char; begin Result:=false; first:=EFileName[1]; last:=EFileName[length(EFileName)]; {avoid type mismatch in ord function by converting strings to chars} {check to see if either the first or the last char in string is a number} if ((ord(first)>47) and (ord(first)<58)) or ((ord(last)>47) and (ord(last)<58)) then Result:=true; end; {*************************************************************************** *} { in a number filename ie 'file034' extract the filename part 'file' to make it available later for appending new number strings ie file + 035 etc} function TForm1.TextInFileName(EFileName, Enum:string; Npos:integer):string; begin Result:=''; if (Npos=1) and (EFileName<>Enum) then Result:=Copy(EFileName, length(Enum)+1, length(EFileName)-length(Enum)) else if EFileName<>Enum then Result:=Copy(EFileName, 1, length(EFileName)-length(Enum)); end; {*************************************************************************** **} {--------- BATCH LOAD NUMBERED FILES---------------------------} procedure TForm1.BatchLoadNumberedFiles(FileNameAndPath, TruncatedFileName, Number, FileText:string; NumberPos:integer); var rootdir, ext, SearchFile:string; found:boolean; begin found:=true; rootdir:=ExtractFilePath(FileNameAndPath); {note - trailing slash exists} {list of files will go into a ListBox} Ext:=ExtractFileExt(ExtractFileName(FileNameAndPath)); SearchFile:=FileNameAndPath; While found do begin ListBox.Items.Append(SearchFile); {add file number one} {retain leading zeros in filenumber ie 001 } Number:=KeepLeadingZeros(Number); {increment the numbered text remembering to ‘carry'} if NumberPos=1 then {filename begins with numbers ie 001file.text or 001.txt} {assemble the filename with the next number in the sequence 001 then 002} SearchFile:=rootdir+Number+FileText+ext {number before file text or only a numbered file name, no text}} else SearchFile:=rootdir+FileText+Number+ext; {else number at end of file text} found:=FileExists(SearchFile); {load files in numerical order until the last in the sequence is found ie load all filenames into the list box between file001 .... file054 or 0001 to 1010 or file5 to file521 or 001file to 092file and so on} end; {while} end; {****************************************************************} function TForm1.KeepLeadingZeros(Num:string):string; var temp, s:string; i, NewNum:integer; begin { number in filename might be '001' keep zeros and inc '1' Converting an int to a string disposes of these leading zeros making a false file name} NewNum:=StrToInt(Num); NewNum:=NewNum+1; Result:=IntToStr(NewNum); i:=1; temp:=''; If Num[i]='0' then begin {if trailing zeros then begin ie 009} While ((num[i]='0') and (i<=length(num))) do begin temp:=temp+num[i]; {store trailing zeros in temp string} i:=i+1; end; {while} if i=length(num)+1 then {check for file number all zeros ie '000'} Result:=Copy(num, 1, length(num)-1)+'1' {drop a zero append '1'= 001 } else begin {save trailing zeros. but remember to carry } {ie. 009 becomes 010 - trailing zeros are in temp} {i contains length of temp zeros plus one} s:=''; s:=Copy(num, i, length(num)-length(temp)); {extract num after zeros} NewNum:=StrToInt(s); NewNum:=NewNum+1; s:=IntToStr(NewNum); If (length(s)+length(temp))>length(num) then {check for carry} Result:=Copy(temp, 1, length(temp)-1)+s {drop a zero} else Result:=temp+s; end; {else} end; {if} end;