delphi清理txt檔案多餘的空格

我是狂草發表於2024-04-24

PDF檔案轉存為文字,多了一堆不需要的空格,寫個小程式處理一下,沒邏輯,直接上程式碼。

delphi用的是XE11.3

unit UnitSmallMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.Memo.Types, FMX.ScrollBox,
  FMX.Memo;

type
  TForm4 = class(TForm)
    btn1: TSpeedButton;
    Memo1: TMemo;
    procedure btn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form4: TForm4;

implementation



{$R *.fmx}

uses
  System.IOUtils;

function GetAllFile(FilePath: string; const FileType: string): TStringList;
var
  FDir, FFile: TSearchRec;
  I, J: Integer;
  AllPath: TStringList;
begin
  AllPath := TStringList.Create;
  Result := TStringList.Create;
  try
    AllPath.Append(FilePath);
    repeat
      Application.ProcessMessages;
      FilePath := AllPath.Strings[0];
      AllPath.Delete(0);
      I := FindFirst(FilePath + FileType, faAnyFile, FFile);
      while I = 0 do
      begin
        //  Result.Append(FilePath + FFile.Name+','+ExtractFileExt(FFile.Name)+','+inttostr(FFile.Size));
        Result.Append(FilePath + FFile.Name); //路徑+檔名

        //Result.Append(FFile.Name); //只獲取檔名
        I := FindNext(FFile);
      end;
      FindClose(FFile);
      J := FindFirst(FilePath + '*', faAnyFile, FDir);
      while J = 0 do
      begin
        if ((FDir.Attr and faDirectory) = faDirectory) and (FDir.Name <> '.') and (FDir.Name <> '..') then
          AllPath.Append(FilePath + FDir.Name + '\');
        J := FindNext(FDir);
      end;
      FindClose(FDir);
    until AllPath.Count = 0;
  finally
    AllPath.Free;
  end;
end;

function AppPath: string;
begin
  Result := TPath.GetLibraryPath;  //(ExtractFilePath(Application.Name));

//   TPath.GetDirectoryName(Application.GetNamePath);
  // ExtractFilePath(Application.ExeName);
end;

procedure TForm4.btn1Click(Sender: TObject);
var
  sl: TStringList;
  I: Integer;
  sname, snewName: string;
  slTemp: TStringList;
  j: Integer;
  sb: TStringBuilder;
begin
try


  sl := GetAllFile(AppPath, '*.txt');

  sb := TStringBuilder.Create;

  slTemp := TStringList.Create;
  try


  for I := 0 to sl.Count - 1 do
  begin
    sname := sl[I];
    sb.Clear;

    slTemp.LoadFromFile(sname, TEncoding.UTF8);

    for j := 0 to slTemp.Count - 1 do
    begin
      sb.Append(TrimRight(slTemp[j]));
    end;
    slTemp.Text := sb.ToString;
    snewName := AppPath + 'new\' + TPath.GetFileNameWithoutExtension(sname) + '.txt';

    slTemp.SaveToFile(snewName);

    Memo1.Lines.Add(snewName + '儲存完成!');

  end;

  except on e:Exception do
  begin
    Memo1.Lines.Add(e.Message);
  end;


  end;

  finally
  sb.Free;
  sl.Free;
  slTemp.Free;
end;
end;

end.

窗體程式碼

object Form4: TForm4
  Left = 810
  Top = 330
  Caption = 'Form4'
  ClientHeight = 480
  ClientWidth = 640
  Position = Designed
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  Left = 810
  Top = 330
  DesignerMasterStyle = 0
  object btn1: TSpeedButton
    Position.X = 480.000000000000000000
    Position.Y = 288.000000000000000000
    Text = 'btn1'
    OnClick = btn1Click
  end
  object Memo1: TMemo
    Touch.InteractiveGestures = [Pan, LongTap, DoubleTap]
    DataDetectorTypes = []
    Position.X = 8.000000000000000000
    Position.Y = 33.000000000000000000
    Size.Width = 385.000000000000000000
    Size.Height = 384.000000000000000000
    Size.PlatformDefault = False
    TabOrder = 1
    Viewport.Width = 381.000000000000000000
    Viewport.Height = 380.000000000000000000
  end
end

文章來源於天涯神貼

之後

相關文章