Delphi文件操作所涉及的一些函数

//判断文件是否存在 FileExists
//判断文件夹是否存在 DirectoryExists
//删除文件 DeleteFile; Windows.DeleteFile
//删除文件夹 RemoveDir; RemoveDirectory
//获取当前文件夹 GetCurrentDir
//设置当前文件夹 SetCurrentDir; ChDir; SetCurrentDirectory
//获取指定驱动器的当前路径名 GetDir
//文件改名 RenameFile
//建立文件夹 CreateDir; CreateDirectory; ForceDirectories
//删除空文件夹 RemoveDir; RemoveDirectory
//建立新文件 FileCreate
//获取当前文件的版本号 GetFileVersion
//获取磁盘空间 DiskSize; DiskFree
//搜索文件 FindFirst; FindNext; FindClose
//读取与设置文件属性 FileGetAttr; FileSetAttr
//获取文件的创建时间 FileAge; FileDateToDateTime

Delphi代码
  1. //判断文件是否存在 FileExists   
  2. var  
  3.   f: string;   
  4. begin  
  5.   f := 'c:\temp\test.txt';   
  6.   if not FileExists(f) then  
  7.   begin  
  8.    //如果文件不存在   
  9.   end;   
  10. end;   
  11.   
  12. --------------------------------------------------------------------------------   
  13.   
  14.   
  15. //判断文件夹是否存在 DirectoryExists   
  16. var  
  17.   dir: string;   
  18. begin  
  19.   dir := 'c:\temp';   
  20.   if not DirectoryExists(dir) then  
  21.   begin  
  22.    //如果文件夹不存在   
  23.   end;   
  24. end;   
  25.   
  26. --------------------------------------------------------------------------------   
  27.   
  28.   
  29. //删除文件 DeleteFile; Windows.DeleteFile   
  30. var  
  31.   f: string;   
  32. begin  
  33.   f := 'c:\temp\test.txt';   
  34. //DeleteFile(f);  //返回 Boolean   
  35.   
  36. //或者用系统API:   
  37.   Windows.DeleteFile(PChar(f)); //返回 Boolean   
  38. end;   
  39.   
  40. --------------------------------------------------------------------------------   
  41.   
  42.   
  43. //删除文件夹 RemoveDir; RemoveDirectory   
  44. var  
  45.   dir: string;   
  46. begin  
  47.   dir := 'c:\temp';   
  48.   RemoveDir(dir); //返回 Boolean   
  49.   
  50. //或者用系统 API:   
  51.   RemoveDirectory(PChar(dir)); //返回 Boolean   
  52. end;   
  53.   
  54. --------------------------------------------------------------------------------   
  55.   
  56.   
  57. //获取当前文件夹 GetCurrentDir   
  58. var  
  59.   dir: string;   
  60. begin  
  61.   dir := GetCurrentDir;   
  62.   ShowMessage(dir); //C:\Projects   
  63. end;   
  64.   
  65. --------------------------------------------------------------------------------   
  66.   
  67.   
  68. //设置当前文件夹 SetCurrentDir; ChDir; SetCurrentDirectory   
  69. var  
  70.   dir: string;   
  71. begin  
  72.   dir := 'c:\temp';   
  73.   if SetCurrentDir(dir) then  
  74.     ShowMessage(GetCurrentDir); //c:\temp   
  75.   
  76. //或者   
  77.   ChDir(dir); //无返回值   
  78.   
  79. //也可以使用API:   
  80.   SetCurrentDirectory(PChar(Dir)); //返回 Boolean   
  81. end;   
  82.   
  83. --------------------------------------------------------------------------------   
  84.   
  85.   
  86. //获取指定驱动器的当前路径名 GetDir   
  87. var  
  88.   dir: string;   
  89.   b: Byte;   
  90. begin  
  91.   b := 0;   
  92.   GetDir(b,dir);   
  93.   ShowMessage(dir); //   
  94.   
  95. //第一个参数: 1、2、3、4...分别对应: A、B、C、D...   
  96. //0 是缺省驱动器   
  97. end;   
  98.   
  99. --------------------------------------------------------------------------------   
  100.   
  101.   
  102. //文件改名 RenameFile   
  103. var  
  104.   OldName,NewName: string;   
  105. begin  
  106.   OldName := 'c:\temp\Old.txt';   
  107.   NewName := 'c:\temp\New.txt';   
  108.   
  109.   if RenameFile(OldName,NewName) then  
  110.     ShowMessage('改名成功!');   
  111.   
  112. //也可以:   
  113.   SetCurrentDir('c:\temp');   
  114.   OldName := 'Old.txt';   
  115.   NewName := 'New.txt';   
  116.   
  117.   if RenameFile(OldName,NewName) then  
  118.     ShowMessage('改名成功!');   
  119. end;   
  120.   
  121. --------------------------------------------------------------------------------   
  122.   
  123.   
  124. //建立文件夹 CreateDir; CreateDirectory; ForceDirectories   
  125. var  
  126.   dir: string;   
  127. begin  
  128.   dir := 'c:\temp\delphi';   
  129.   if not DirectoryExists(dir) then  
  130.     CreateDir(dir); //返回 Boolean   
  131.   
  132. //也可以直接用API:   
  133.   CreateDirectory(PChar(dir),nil); //返回 Boolean   
  134.   
  135. //如果缺少上层目录将自动补齐:   
  136.   dir := 'c:\temp\CodeGear\Delphi\2007\万一';   
  137.   ForceDirectories(dir); //返回 Boolean   
  138. end;   
  139.   
  140. --------------------------------------------------------------------------------   
  141.   
  142.   
  143. //删除空文件夹 RemoveDir; RemoveDirectory   
  144. var  
  145.   dir: string;   
  146. begin  
  147.   dir := 'c:\temp\delphi';   
  148.   RemoveDir(dir); //返回 Boolean   
  149.   
  150. //也可以直接用API:   
  151.   RemoveDirectory(PChar(dir)); //返回 Boolean   
  152. end;   
  153.   
  154. --------------------------------------------------------------------------------   
  155.   
  156.   
  157. //建立新文件 FileCreate   
  158. var  
  159.   FileName: string;   
  160.   i: Integer;   
  161. begin  
  162.   FileName := 'c:\temp\test.dat';   
  163.   i := FileCreate(FileName);   
  164.   
  165.   if i>0 then  
  166.     ShowMessage('新文件的句柄是: ' + IntToStr(i))   
  167.   else  
  168.     ShowMessage('创建失败!');   
  169. end;   
  170.   
  171. --------------------------------------------------------------------------------   
  172.   
  173.   
  174. //获取当前文件的版本号 GetFileVersion   
  175. var  
  176.   s: string;   
  177.   i: Integer;   
  178. begin  
  179.   s := 'C:\WINDOWS\notepad.exe';   
  180.   i := GetFileVersion(s); //如果没有版本号返回 -1   
  181.   ShowMessage(IntToStr(i)); //327681 这是当前记事本的版本号(还应该再转换一下)   
  182. end;   
  183.   
  184. --------------------------------------------------------------------------------   
  185.   
  186.   
  187. //获取磁盘空间 DiskSize; DiskFree   
  188. var  
  189.   r: Real;   
  190.   s: string;   
  191. begin  
  192.   r := DiskSize(3); //获取C:总空间, 单位是字节   
  193.   r := r/1024/1024/1024;   
  194.   Str(r:0:2,s); //格式为保留两位小数的字符串   
  195.   s := 'C盘总空间是: ' + s + ' GB';   
  196.   ShowMessage(s); //xx.xx GB   
  197.   
  198.   r := DiskFree(3); //获取C:可用空间   
  199.   r := r/1024/1024/1024;   
  200.   Str(r:0:2,s);   
  201.   s := 'C盘可用空间是: ' + s + ' GB';   
  202.   ShowMessage(s); //xx.xx GB   
  203. end;   
  204.   
  205. //查找一个文件 FileSearch   
  206. var  
  207.   FileName,Dir,s: string;   
  208. begin  
  209.   FileName := 'notepad.exe';   
  210.   Dir := 'c:\windows';   
  211.   s := FileSearch(FileName,Dir);   
  212.   
  213.   if s<>'' then  
  214.     ShowMessage(s) //c:\windows\notepad.exe   
  215.   else  
  216.     ShowMessage('没找到');   
  217. end;   
  218.   
  219. --------------------------------------------------------------------------------   
  220.   
  221.   
  222. //搜索文件 FindFirst; FindNext; FindClose   
  223. var  
  224.   sr: TSearchRec;    //定义 TSearchRec 结构变量   
  225.   Attr: Integer;     //文件属性   
  226.   s: string;         //要搜索的内容   
  227.   List: TStringList; //存放搜索结果   
  228. begin  
  229.   s := 'c:\windows\*.txt';   
  230.   Attr := faAnyFile;             //文件属性值faAnyFile表示是所有文件   
  231.   List := TStringList.Create;    //List建立   
  232.   
  233.   if FindFirst(s,Attr,sr)=0 then //开始搜索,并给 sr 赋予信息, 返回0表示找到第一个   
  234.   begin  
  235.     repeat                       //如果有第一个就继续找   
  236.       List.Add(sr.Name);         //用List记下结果   
  237.     until(FindNext(sr)<>0);      //因为sr已经有了搜索信息, FindNext只要这一个参数, 返回0表示找到   
  238.   end;   
  239.   FindClose(sr);                 //需要结束搜索, 搜索是内含句柄的   
  240.   
  241.   ShowMessage(List.Text);        //显示搜索结果   
  242.   List.Free;                     //释放List   
  243.   
  244. //更多注释:   
  245. //TSearchRec 结构是内涵文件大小、名称、属性与时间等信息   
  246. //TSearchRec 中的属性是一个整数值, 可能的值有:   
  247. //faReadOnly  1   只读文件   
  248. //faHidden    2   隐藏文件   
  249. //faSysFile   4   系统文件   
  250. //faVolumeID  8   卷标文件   
  251. //faDirectory 16  目录文件   
  252. //faArchive   32  归档文件   
  253. //faSymLink   64  链接文件   
  254. //faAnyFile   63  任意文件   
  255.   
  256. //s 的值也可以使用?通配符,好像只支持7个?, 如果没有条件就是*, 譬如: C:\*   
  257. //实际使用中还应该在 repeat 中提些条件, 譬如判断如果是文件夹就递归搜索等等   
  258. end;   
  259.   
  260. --------------------------------------------------------------------------------   
  261.   
  262.   
  263. //读取与设置文件属性 FileGetAttr; FileSetAttr   
  264. var  
  265.   FileName: string;   
  266.   Attr: Integer; //属性值是一个整数   
  267. begin  
  268.   FileName := 'c:\temp\Test.txt';   
  269.   Attr := FileGetAttr(FileName);   
  270.   ShowMessage(IntToStr(Attr)); //32, 存档文件   
  271.   
  272. //设置为隐藏和只读文件:   
  273.   Attr := FILE_ATTRIBUTE_READONLY or FILE_ATTRIBUTE_HIDDEN;   
  274.   if FileSetAttr(FileName,Attr)=0 then //返回0表示成功   
  275.     ShowMessage('设置成功!');   
  276.   
  277. //属性可选值(有些用不着):   
  278. //FILE_ATTRIBUTE_READONLY = 1; 只读   
  279. //FILE_ATTRIBUTE_HIDDEN = 2; 隐藏   
  280. //FILE_ATTRIBUTE_SYSTEM = 4; 系统   
  281. //FILE_ATTRIBUTE_DIRECTORY = 16   
  282. //FILE_ATTRIBUTE_ARCHIVE = 32; 存档   
  283. //FILE_ATTRIBUTE_DEVICE = 64   
  284. //FILE_ATTRIBUTE_NORMAL = 128; 一般   
  285. //FILE_ATTRIBUTE_TEMPORARY = 256   
  286. //FILE_ATTRIBUTE_SPARSE_FILE = 512   
  287. //FILE_ATTRIBUTE_REPARSE_POINT = 1204   
  288. //FILE_ATTRIBUTE_COMPRESSED = 2048; 压缩   
  289. //FILE_ATTRIBUTE_OFFLINE = 4096   
  290. //FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192; 不被索引   
  291. //FILE_ATTRIBUTE_ENCRYPTED = 16384   
  292. end;   
  293.   
  294. --------------------------------------------------------------------------------   
  295.   
  296.   
  297. //获取文件的创建时间 FileAge; FileDateToDateTime   
  298. var  
  299.   FileName: string;   
  300.   ti: Integer;   
  301.   dt: TDateTime;   
  302. begin  
  303.   FileName := 'c:\temp\Test.txt';   
  304.   ti := FileAge(FileName);   
  305.   ShowMessage(IntToStr(ti)); //返回: 931951472, 需要转换   
  306.   
  307.   dt := FileDateToDateTime(ti); //转换   
  308.   ShowMessage(DateTimeToStr(dt)); //2007-12-12 14:27:32   
  309. end;  


DELPHI文本整理器 样式像记事本 // 字符串处理功能 unit StringFunctions; interface uses Windows, Messages, SysUtils, Variants, Classes, Forms, Dialogs, StdCtrls, Commctrl; type TStringFunction = class(TObject) private function IsUpper(ch: char): boolean; function IsLower(ch: char): boolean; function ToUpper(ch: char): char; function ToLower(ch: char): char; public procedure ReplaceSelText(Edit: TCustomEdit; const s: String); procedure UpperSelText(Edit: TCustomEdit); procedure LowerSelText(Edit: TCustomEdit); function UpperFistLetter(Memo: TMemo): string; procedure ClearBlankLine(Memo: TMemo); procedure ClearBlankSpace(Memo: TMemo); procedure ClearNum(Memo: TMemo); procedure ClearLetter(Memo: TMemo); procedure InsertNumber(Memo: TMemo); procedure InsertComment(Memo: TMemo); procedure BatchReplaceString(Memo: TMemo); procedure JustOneLine(Memo: TMemo); procedure ReLine(Memo: TMemo; n: Integer); procedure TextToHtml(sTextFile, sHtmlFile: string); function Proper(const s: string): string; function CNWordsCount(text: string): Integer; function ENWordsCount(text: string): Integer; end; var StrFunction: TStringFunction; implementation // 让代码设置Memo后可以让memo在Ctrl+Z撤销有效 procedure TStringFunction.ReplaceSelText(Edit: TCustomEdit; const s: String); begin SendMessage(Edit.Handle, EM_REPLACESEL, 1, LPARAM(PChar(s))); // Edit.Perform(EM_REPLACESEL, 1, LPARAM(PChar(s))); end; // Edit显示行号 // ------------------------------------------------------------------------------ // 去除空行 // Memo1.Text := StringReplace(Memo1.Text, #13#10#13#10, #13#10, [rfReplaceAll]); { //无法撤销 //空行的去掉 //本行只有空格的也去掉 //全选 //复制到剪切板上 } procedure TStringFunction.ClearBlankLine(Memo: TMemo); var i: Integer; list: TStringList; begin with Memo do begin if Lines.Count > 0 then begin list := TStringList.Create; for i := 0 to Lines.Count - 1 do if (Trim(Lines[i]) <> '') then list.Add(Lines[i]); SelectAll; ReplaceSelText(Memo, list.text); list.Free; end; end; end; // 去除空格 // 将 空格替换为空 procedure TStringFunction.ClearBlankSpace(Memo: TMemo); var s: string; begin s := StringReplace(Memo.Lines.text, ' ', '', [rfReplaceAll]); Memo.SelectAll; ReplaceSelText(Memo, s); end; // 去除一字符串中的所有的数字 procedure TStringFunction.ClearNum(Memo: TMemo); var str: string; i: Integer; begin str := '1234567890'; for i := 0 to Length(str) do Memo.text := StringReplace(Memo.Lines.text, str[i], '', [rfReplaceAll]); { rfReplaceAll TReplaceFlags = set of (rfReplaceAll, rfIgnoreCase); } end; // 去除一字符串中的所有的字母 procedure TStringFunction.ClearLetter(Memo: TMemo); var str: string; i: Integer; begin str := 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; for i := 0 to Length(str) do Memo.text := StringReplace(Memo.Lines.text, str[i], '', [rfReplaceAll]); end; // 批量替换关键字 procedure TStringFunction.BatchReplaceString(Memo: TMemo); var i: Integer; begin for i := 0 to Length(Memo.Lines.text) do Memo.text := StringReplace(Memo.Lines.text, Memo.Lines[i], '', [rfReplaceAll]); ClearBlankSpace(Memo); end; // ------------------------------------------------------------------------------ // 全角转半角 // 符号有哪些 procedure ConvertQtoB; begin end; // 半角转换全角 procedure ConvertBtoQ; begin end; { 转换选中的文本大写 } procedure TStringFunction.UpperSelText(Edit: TCustomEdit); var x, y: Integer; begin With Edit do begin x := SelStart; y := SelLength; if SelText <> '' then begin ReplaceSelText(Edit, UpperCase(SelText)); SelStart := x; SelLength := y; end else begin Edit.SelectAll; ReplaceSelText(Edit, UpperCase(Edit.text)); end; end; end; { 转换选中的文本小写 } procedure TStringFunction.LowerSelText(Edit: TCustomEdit); var x, y: Integer; begin With Edit do begin x := SelStart; y := SelLength; if SelText <> '' then begin ReplaceSelText(Edit, LowerCase(SelText)); SelStart := x; SelLength := y; end else begin Edit.SelectAll; ReplaceSelText(Edit, LowerCase(Edit.text)); end; end; end; { 判断字符是否是大写字符 } function TStringFunction.IsUpper(ch: char): boolean; begin Result := ch in ['A' .. 'Z']; end; { 判断字符是否是小写字符 } function TStringFunction.IsLower(ch: char): boolean; begin Result := ch in ['a' .. 'z']; end; { 转换为大写字符 } function TStringFunction.ToUpper(ch: char): char; begin Result := chr(ord(ch) and $DF); end; { 转换为小写字符 } function TStringFunction.ToLower(ch: char): char; begin Result := chr(ord(ch) or $20); end; { Capitalizes First Letter Of Every Word In S 单语首字母大写 } function TStringFunction.Proper(const s: string): string; var i: Integer; CapitalizeNextLetter: boolean; begin Result := LowerCase(s); CapitalizeNextLetter := True; for i := 1 to Length(Result) do begin if CapitalizeNextLetter and IsLower(Result[i]) then Result[i] := ToUpper(Result[i]); CapitalizeNextLetter := Result[i] = ' '; end; end; { Memo选中的首字母大写 } function TStringFunction.UpperFistLetter(Memo: TMemo): string; var i, j: Integer; begin with Memo do begin i := SelStart; j := SelLength; // SelText := Proper(SelText); ReplaceSelText(Memo, Proper(SelText)); SelStart := i; SelLength := j; end; end; // ------------------------------------------------------------------------------ procedure TStringFunction.InsertNumber(Memo: TMemo); var i: Integer; str: String; begin for i := 0 to Memo.Lines.Count do begin str := Format('%.4d. %s', [i, Memo.Lines[i]]); Memo.Lines[i] := str; Application.ProcessMessages; end; end; // 注释和取消注释 // 获得选中的文本的起始行和结束行 procedure TStringFunction.InsertComment(Memo: TMemo); var str: string; x, y: Integer; begin str := Memo.SelText; x := Memo.SelStart; y := Memo.SelLength; if str = '' then Exit; // Memo.SetSelText('//' +str); Memo.SelText := '//' + str; Memo.SelStart := x + 2; Memo.SelLength := y + 2; end; // ------------------------------------------------------------------------------ // 合并成一行 procedure TStringFunction.JustOneLine(Memo: TMemo); var s: string; i: Integer; begin for i := 0 to Memo.Lines.Count - 1 do s := s + Memo.Lines[i]; Memo.SelectAll; ReplaceSelText(Memo, s); end; // ------------------------------------------------------------------------------ // 重新分行 { var n: Integer; begin n := StrToInt(InputBox('重新分行', '每行几个字符', '8')); ReLine(Memo1, n); end; } procedure TStringFunction.ReLine(Memo: TMemo; n: Integer); var s: string; i, j, k: Integer; L: TStringList; begin L := TStringList.Create; j := 1; for k := 0 to Memo.Lines.Count - 1 do s := s + Memo.Lines[k]; if Trim(s) <> '' then begin for i := 0 to (Length(s) div n) do // 几行 begin j := j + n; L.Add(Copy(s, j - n, n)); // COPY 的第一位不是0是1 // 每行的字符 end; end; Memo.SelectAll; ReplaceSelText(Memo, L.text); L.Free; end; // ------------------------------------------------------------------------------ // 获得汉字字符个数 function TStringFunction.CNWordsCount(text: string): Integer; var i, sum, c: Integer; begin Result := 0; c := 0; sum := Length(text); if sum = 0 then Exit; for i := 0 to sum do begin if ord(text[i]) >= 127 then begin Inc(c); end; end; Result := c; end; // 获得非汉字字符个数 function TStringFunction.ENWordsCount(text: string): Integer; var i, sum, e: Integer; begin Result := 0; e := 0; sum := Length(text); if sum = 0 then Exit; for i := 0 to sum do begin if (ord(text[i]) >= 33) and (ord(text[i]) <= 126) then begin Inc(e); end; end; Result := e; end; { TextToHtml('C:\1.txt','c:\2.htm'); } procedure TStringFunction.TextToHtml(sTextFile, sHtmlFile: string); var aText: TStringList; aHtml: TStringList; i: Integer; begin aText := TStringList.Create; try aText.LoadFromFile(sTextFile); aHtml := TStringList.Create; try aHtml.Clear; aHtml.Add('<html>'); aHtml.Add('<body>'); for i := 0 to aText.Count - 1 do aHtml.Add(aText.Strings[i] + '<br>'); aHtml.Add('</body>'); aHtml.Add('</html>'); aHtml.SaveToFile(sHtmlFile); finally aHtml.Free; end; finally aText.Free; end; end; Initialization StrFunction := TStringFunction.Create; Finalization StrFunction.Free; end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值