Mega Code Archive

 
Categories / Delphi / Examples
 

Editfilename

function TResultsForm.EditFileName(tempFName: String): String; {edit a filename such that if the filename-minus-extension is maxFNameChars or less long, then remove the extension and pad the rest of the string with blanks up to the maxFNameChars place if necessary. If the filename-minus-extension is longer than this, truncate it down to maxFNameChars...} const maxFNameChars = 12; maxPaddedLen = 14; var i: Integer; stringLen: Integer; tempArray: array[0..maxStringLen] of Char; padOK: Boolean; begin padOK := False; stringLen := Length(tempFName); {copy tempFName into an array...} for i := 1 to stringLen do begin tempArray[i - 1] := tempFName[i]; end; {and then get rid of the extension, if there is an extension...} for i := 1 to maxPaddedLen + 1 do begin if ((tempArray[i - 1] = '.') or (i >= maxFNameChars)) then begin padOK := True; end; if (padOK = True) then tempArray[i - 1] := ' '; end; {at this point we have an array string with no extension, padded with spaces to the maxPaddedLen index position, but we may need to tidy it up a bit...} padOK := False; for i := 1 to (maxPaddedLen - 1) do begin if not(tempArray[i - 1] in validFNameChars) then padOK := True; if (padOK = True) then tempArray[i - 1] := ' ' end; tempArray[maxPaddedLen] := #0; tempFName := String(tempArray); Result := tempFName; end;