作者:admin 来源: 日期:2019/11/3 10:42:42 人气: 标签:
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; Edit1: TEdit; Button2: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} procedure TForm1.Button1Click(Sender: TObject);var LFindHandle: THandle; LFindData: TWin32FindData; LSearchFullPath, LFileName: String; LFileList: TStringList; LLines: TStringList; I: Integer; LFile: TextFile; LLine, LLastLine: string;begin // 对于大文件,速度很慢 LFileList := TStringList.Create; LLines := TStringList.Create; LSearchFullPath := Edit1.Text; FillChar(LFindData, SizeOf(LFindData), 0); LFindHandle := FindFirstFile(PChar(LSearchFullPath), LFindData); if LFindHandle <> INVALID_HANDLE_VALUE then begin try repeat if LFindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then begin Continue; end; if LFindData.dwFileAttributes and FILE_ATTRIBUTE_HIDDEN <> 0 then Continue; LFileName := LFindData.cFileName; LFileList.Add(LFileName); until not FindNextFile(LFindHandle, LFindData); finally Windows.FindClose(LFindHandle); end; end; for I := 0 to LFileList.Count - 1 do begin AssignFile(LFile, LFileList.Strings[I]); Reset(LFile); LLastLine := ''; while not Eof(LFile) do begin Readln(LFile, LLine); if Length(LLine) > 0 then begin LLastLine := LLine; end; end; if Length(LLastLine) > 0 then LLines.Add(LLastLine); end; LLines.SaveToFile('1.dat'); LFileList.Free; LLines.Free;end; function TextSeek(var f: Text; position: Int64; FromPos: DWORD = FILE_BEGIN): Int64;var pos64: Int64Rec absolute position; resHi: cardinal; resLow: Cardinal;begin result := 0; with TTextRec(f) do begin if mode<>fmInput then exit; resHi := pos64.Hi; resLow := SetFilePointer(handle,pos64.Lo,@resHi,FromPos); BufEnd := 0; // flush internal reading buffer BufPos := 0; // 返回当前文件的位置 if (resHi = INVALID_HANDLE_VALUE) and (resLow = INVALID_HANDLE_VALUE) then Result := -1 else result := resHi shl 32 + resLow; end;end; procedure TForm1.Button2Click(Sender: TObject);var LFindHandle: THandle; LFindData: TWin32FindData; LSearchFullPath, LFileName: String; LFileList: TStringList; LLines: TStringList; I: Integer; LEndIndex: Integer; LFile: TextFile; LLine, LLastLine: string;begin LFileList := TStringList.Create; LLines := TStringList.Create; LSearchFullPath := Edit1.Text; FillChar(LFindData, SizeOf(LFindData), 0); LFindHandle := FindFirstFile(PChar(LSearchFullPath), LFindData); if LFindHandle <> INVALID_HANDLE_VALUE then begin try repeat if LFindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY <> 0 then begin Continue; end; if LFindData.dwFileAttributes and FILE_ATTRIBUTE_HIDDEN <> 0 then Continue; LFileName := LFindData.cFileName; LFileList.Add(LFileName); until not FindNextFile(LFindHandle, LFindData); finally Windows.FindClose(LFindHandle); end; end; for I := 0 to LFileList.Count - 1 do begin AssignFile(LFile, LFileList.Strings[I]); Reset(LFile); LLastLine := ''; // 先去掉最后的空行 LEndIndex := 1; while True do begin if TextSeek(LFile, -LEndIndex, FILE_END) < 0 then Break; Readln(LFile, LLine); if Length(LLine) > 0 then Break; Inc(LEndIndex); end; // 读取最后一行 Inc(LEndIndex); while True do begin if TextSeek(LFile, -LEndIndex, FILE_END) < 0 then Break; Readln(LFile, LLine); if Length(LLine) = 0 then Break; LLastLine := LLine; Inc(LEndIndex); end; if Length(LLastLine) > 0 then LLines.Add(LLastLine); CloseFile(LFile); end; LLines.SaveToFile('1.dat'); LFileList.Free; LLines.Free;end; end.
来源:https://blog.csdn.net/xiuzhentianting/article/details/48472969