首页 > 其他分享 >delphi清理txt文件多余的空格

delphi清理txt文件多余的空格

时间:2024-04-24 23:11:46浏览次数:27  
标签:AllPath end FMX FilePath delphi 空格 FDir FFile txt

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

 

文章来源于天涯神贴

 

 之后

 

标签:AllPath,end,FMX,FilePath,delphi,空格,FDir,FFile,txt
From: https://www.cnblogs.com/wskc/p/18156572

相关文章

  • delphi Unix时间戳 转yyyy-mm-dd hh:mm:ss 格式字符串
    functionUnixTimeStampToDateTimeStr(UnixTimeStamp:Int64):string;varDateTimeValue:TDateTime;begin//第二个参数默认为true,设置为false,会默认以本地时区来+8小时,因为mysql里村的utc时间秒数DateTimeValue:=UnixToDateTime(UnixTimeStampdiv1000,False......
  • linux 中删除空行(包括空行、空格、制表符)
     001、测试数据[root@pc1test02]#lsa.txt[root@pc1test02]#cata.txt010203040506070809101112[root@pc1test02]#cat-Aa.txt##测试数据中包括什么也无、空格、制表符、空格+制表符的几种情况010203$$040506$$070809$^I^I$1011......
  • delphi模板引擎
    开源地址:https://github.com/sempare/sempare-delphi-template-engineprogramExample;usesSempare.Template;typeTInformation=recordname:string;favourite_sport:string;count:integer;end;beginvartpl:=Template.Parse(......
  • Delphi10.3如何将ListBox1项,拖动 到 右边Memo1当前输入项
    01]ListBox1的DragMode设置为dmAutomatic02】Memo1的OnGragOver事件和OnDragDrop事件 procedureTForm8.Memo1DragOver(Sender,Source:TObject;X,Y:Integer;State:TDragState;varAccept:Boolean);beginifTListBox(Source)=ListBox1thenAccept:=true;end;......
  • Delphi10.3开发的SQLite3的图形操作小软件
    链接:https://pan.baidu.com/s/1Glye61WgYd_wC0uOfx9ZoQ提取码:dqd9关键部份FDConnection1.GetTableNames('','','',ListBox1.Items);StringGrid1.Cells[1,i+1]:=FDMetaInfoQuery1.FieldByName('COLUMN_NAME').AsString;......
  • python 获取文件夹下所有fbx文件的名字并保存到txt文件中
    代码:importosdefget_fbx_files_and_write_to_txt(folder_path,output_file_path):fbx_files=[]#遍历指定文件夹中的所有文件foriteminos.listdir(folder_path):item_path=os.path.join(folder_path,item)#检查是否为文件,并且......
  • delphi TApplicationEvents控件,响应程序级快捷键
    在窗体中插入TApplicationEvents控件,然后设置它的onMessage事件即可 procedureTForm1.ApplicationEventsMessage(varMsg:tagMSG;varHandled:Boolean);varAktywnaForma:TForm;begin//检查是否按下CTRL键并同时按下F7键if(Msg.wParam=VK_F7)and((......
  • delphi中Memo1下拉ListBox1选择完成文本填充
    delphi中Memo1下拉ListBox2选择完成文本填充拖一个Memo1下拉ListBox2到界面上,假设ListBox2,里已加载列名最终效果:Memo1里输入.点号,带出ListBox2,选择好列名,回车,将点号和列名输入到Memo1原位置 ......
  • DELPHI cxgrid 统计主从表中,从表的记录数量
    效果如图所示:核心代码:procedureTForm1.Button1Click(Sender:TObject);varAIndex,I,ATotalSelectedCount,ASelectedRecordsCount:integer;AMasterView:TcxGridDBTableView;ADetailDataController:TcxCustomDataController;beginATotalSelectedCount:=......
  • 一串数字不输出末尾空格
    题目中通常要求输出的n个数字,两个数字之间用一个空格分隔,开头结尾没有空格。如果直接printf("%d",n)的话,则会在末尾多出一个空格,本文讲述几种不留空格的方式和一个误区。不留空格的方式方法一:数列长度不确定的情况。设置标记,输出第一个数的时候后边不带空格,输出2~n个数......