天气与日历 切换到窄版

 找回密码
 立即注册

QQ登录

只需一步,快速开始

此广告位出租
查看: 505|回复: 0

[Delphi教程] delphi 文件的操作:重命名、复制、移动、删除 | 文件

[复制链接]

3188

主题

4

回帖

3290

积分

管理员

积分
3290
发表于 2024-2-26 21:36:51 | 显示全部楼层 |阅读模式
delphi 文件的操作:重命名、复制、移动、删除 | 文件
第一种方法:

RenameFile('Oldname', 'Newname');CopyFile(PChar('Oldname'), PChar('Newname'), False);MoveFile(PChar('Oldname'), PChar('Newname'));DeleteFile(文件名);



第二种方法:
在Delphi可以使用ShellApi单元中的函数SHFileOperation来实现消息上传控件的制作,
SHFileOperation函数可以实现各种文件操作,只需将文件操作命令(拷贝、剪切、删除、重命名)发送给
它,它就会实现Windows资源管理器那样的文件操作功能。该函数的声明如下:
function SHFileOperation(constract lpFileOp : LPSHFILEOPSTRUCT): Integer;stdcall;LPSHFILEOPSTRUCT的结构类型:typedef struct _SHFILEOPSTRUCT{HWND hwnd; // 显示对话框的句柄UINT wFunc; // 指明操作类型,支持4种操作:FO_COPY拷贝、FO_MOVE剪切、FO_DELETE删除、FO_RENAME重命名。LPCSTR pFrom; // 源文件路径,可以是多个文件LPCSTR pTo; // 目标路径,可以是路径或文件名,FO_DELETE时,该参数不起作用FILEOP_FLAGS fFlags; // 标志,附加的风格选项BOOL fAnyOperationsAborted; // 是否可被中断LPVOID hNameMappings; // 文件映射名字,可在其它 Shell 函数中使用LPCSTR lpszProgressTitle; // 只在 FOF_SIMPLEPROGRESS 时,指定对话框的标题。}SHFILEOPSTRUCT;




例如:
  1. uses ShellAPI;
  2. type
  3. TFileCommand=(fcCopy,fcMove,fcDelete,fcRename);
  4. procedure TForm1.FileOperation(aCommand: FileCommand; var aFromFile, aToFile: String);

  5. var

  6. FileOp: TSHFileOPStruct;

  7. begin

  8. ZeroMemory(@FileOp, sizeof(FileOp));

  9. FileOp.Wnd := Form1.Handle;

  10. //显示一个进度对话框,但不显示文件名。

  11. FileOp.fFlags := FOF_SimpleProgress;

  12. //String类型转换到PAnsiChar类型,需要经过AnsiString类型

  13. FileOp.pFrom := PAnsiChar( AnsiString(aFromFile));

  14. FileOp.pTo := PAnsiChar( AnsiString(aToFile));

  15. case aCommand of

  16. fcCopy: FileOp.wFunc := FO_COPY; // 复制文件

  17. fcMove: FileOp.wFunc := FO_MOVE; // 移动文件

  18. fcDelete: FileOp.wFunc := FO_DELETE; // 删除文件

  19. fcRename: FileOp.wFunc := FO_RENAME; // 重命名文件

  20. end;

  21. SHFileOperation(FileOp);

  22. end;
复制代码
Delphi 判断文件是否存在,是否正在使用
  1. function IsFileInUse(fName: string): boolean;
  2. var
  3. HFileRes: HFILE;
  4. begin
  5. Result := false;
  6. if not FileExists(fName) then //如果文件不存在
  7. exit;
  8. HFileRes := CreateFile(pchar(fName), GENERIC_READ or GENERIC_WRITE,

  9. {this is the trick!}, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, );
  10. Result := (HFileRes = INVALID_HANDLE_VALUE);
  11. if not Result then
  12. CloseHandle(HFileRes);
  13. end;
  14. 调用
  15. procedure TForm1.Button1Click(Sender: TObject);
  16. begin
  17. if OpenDialog1.Execute then
  18. begin
  19. if IsFileInUse(OpenDialog1.FileName) = true then
  20. showmessage('文件正在使用')
  21. else
  22. showmessage('文件没有使用');
  23. end;
  24. end;
复制代码
Delphi删除或移动正在使用的文件
Delphi删除文件容易,但删除正在使用的文件,那就需要手段了,因为正在使用的文件是不允许被删除的,看代码:
  1. unit Unit1;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  5. StdCtrls, ExtCtrls;
  6. const
  7. FILE_DELETE=;
  8. FILE_RENAME=;
  9. type
  10. TForm1 = class(TForm)
  11. Button1: TButton;
  12. Label1: TLabel;
  13. Label2: TLabel;
  14. RadioGroup1: TRadioGroup;
  15. Edit1: TEdit;
  16. Edit2: TEdit;
  17. Button2: TButton;
  18. Button3: TButton;
  19. OpenDialog1: TOpenDialog;
  20. procedure Button2Click(Sender: TObject);
  21. procedure Button3Click(Sender: TObject);
  22. procedure Button1Click(Sender: TObject);
  23. procedure Edit2Change(Sender: TObject);
  24. procedure RadioGroup1Click(Sender: TObject);
  25. private
  26. { Private declarations }
  27. public
  28. { Public declarations }
  29. end;
  30. var
  31. Form1: TForm1;
  32. implementation
  33. {$R *.DFM}
  34. function DeleteRenameFileAfterBoot(lpFileNameToSrc,lpFileNameToDes: PChar;flag:Uint): Boolean;
  35. var
  36. WindowsDirs: array [..MAX_PATH + ] of Char;
  37. lpDirSrc,lpDirDes: array [..MAX_PATH + ] of Char;
  38. VerPlatForm: TOSVersionInfoA;
  39. StrLstDelte: TStrings;
  40. filename,s :String;
  41. i:integer;
  42. begin
  43. Result := FALSE;
  44. ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
  45. VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
  46. GetVersionEx(VerPlatForm);
  47. if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32s then
  48. begin
  49. SetLastError(ERROR_NOT_SUPPORTED);
  50. Exit;
  51. end
  52. else if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_NT then
  53. begin
  54. if flag=FILE_DELETE then
  55. Result := MoveFileEx(PChar(lpFileNameToSrc), nil,
  56. MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT)
  57. else if (flag=FILE_RENAME) then
  58. Result := MoveFileEx(lpFileNameToSrc, lpFileNameToDes,
  59. MOVEFILE_REPLACE_EXISTING + MOVEFILE_DELAY_UNTIL_REBOOT);
  60. end
  61. else begin
  62. StrLstDelte := TStringList.Create;
  63. GetWindowsDirectory(WindowsDirs, MAX_PATH + );
  64. filename:=WindowsDirs;
  65. if filename[length(filename)]<>'\' then filename:=filename+'\';
  66. filename:=filename+'wininit.ini';
  67. if FileExists(filename) then
  68. StrLstDelte.LoadFromFile(filename);
  69. if StrLstDelte.IndexOf('[rename]') = - then
  70. StrLstDelte.Add('[rename]');
  71. GetShortPathName(lpFileNameToSrc, lpDirSrc, MAX_PATH + );
  72. if fileexists(lpFileNameToDes) then
  73. GetShortPathName(lpFileNameToDes, lpDirDes, MAX_PATH + )
  74. else begin
  75. s:=extractfilename(lpFileNameToDes);
  76. i:=pos('.',s);
  77. if (i=) then
  78. begin
  79. if length(s)> then raise exception.create('不是有效的短文件名(8+3格式)!');
  80. end
  81. else begin
  82. if (i->)or(length(s)-i>) then raise exception.create('不是有效的短文件名(8+3格式)!');
  83. end;
  84. strcopy(lpDirDes,lpFileNameToDes);
  85. end;
  86. if (flag=FILE_DELETE) then {删除}
  87. StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + , 'NUL='+string(lpDirSrc))
  88. else if (flag=FILE_RENAME) then {改名}
  89. StrLstDelte.Insert(StrLstDelte.IndexOf('[rename]') + , string(lpDirDes)+'='+string(lpDirSrc));

  90. StrLstDelte.SaveToFile(filename);
  91. Result := TRUE;
  92. StrLstDelte.Free;
  93. end;
  94. end;
  95. procedure TForm1.Button2Click(Sender: TObject);
  96. begin
  97. if OpenDialog1.Execute then
  98. edit1.text:=OpenDialog1.FileName;
  99. end;

  100. procedure TForm1.Button3Click(Sender: TObject);
  101. begin
  102. if OpenDialog1.Execute then
  103. edit2.text:=OpenDialog1.FileName;
  104. end;
  105. procedure TForm1.Button1Click(Sender: TObject);
  106. var
  107. i:uint;
  108. begin
  109. if RadioGroup1.ItemIndex= then i:=FILE_DELETE
  110. else i:=FILE_RENAME;
  111. if edit1.text='' then raise exception.create('源文件为空!');
  112. if (i=FILE_RENAME)and(edit2.text='') then raise exception.create('目标文件为空!');
  113. if not DeleteRenameFileAfterBoot(pchar(edit1.text),pchar(edit2.text),i) then
  114. showmessage('出错了')
  115. else showmessage('操作完成');
  116. end;

  117. procedure TForm1.Edit2Change(Sender: TObject);
  118. var
  119. VerPlatForm: TOSVersionInfoA;
  120. buf: array [..MAX_PATH + ] of Char;
  121. begin
  122. if not fileexists(edit2.text) then exit;
  123. ZeroMemory(@VerPlatForm, SizeOf(VerPlatForm));
  124. VerPlatForm.dwOSVersionInfoSize := SizeOf(VerPlatForm);
  125. GetVersionEx(VerPlatForm);
  126. if VerPlatForm.dwPlatformId = VER_PLATFORM_WIN32_WINDOWS then
  127. begin
  128. GetShortPathName(pchar(edit2.text), buf, MAX_PATH + );
  129. edit2.text:=buf;
  130. end;
  131. end;
  132. procedure TForm1.RadioGroup1Click(Sender: TObject);
  133. begin
  134. edit2.Enabled:=RadioGroup1.ItemIndex=;
  135. button2.Enabled:=RadioGroup1.ItemIndex=;
  136. end;
  137. end.
复制代码
其实就是利用Windows重启的瞬间来删除或移动文件。
文件,文件夹删除移动和拷贝
  1. function WinErasefile(Owner: Integer; WichFiles: string; SendToRecycleBin, Confirm: Boolean): Boolean;
  2. //用于将文件直接删除或移动到回收站
  3. var
  4. Struct : TSHFileOpStructA;
  5. begin
  6. FillChar(Struct, SizeOf(Struct), );
  7. While pos(';', WichFiles)> do
  8. WichFiles[pos(';', WichFiles)] := #;
  9. WichFiles := WichFiles + ##;
  10. with Struct do
  11. begin
  12. wnd := Owner;
  13. wFunc := FO_Delete;
  14. pFrom := PChar(WichFiles);
  15. pTo := nil;
  16. If not Confirm then fFlags := FOF_NOCONFIRMATION;
  17. If SendToRecycleBin then fFLags := fFlags or FOF_ALLOWUNDO or FOF_FILESONLY
  18. else fFlags := fFlags or  or FOF_FILESONLY;
  19. hNameMappings := nil;
  20. lpszProgressTitle := nil;
  21. end;
  22. result := (SHFileOperationA(Struct)=) and (not Struct.fAnyOperationsAborted);
  23. end;

  24. function WinErasepath(Owner: Integer; WichFiles: string; SendToRecycleBin, Confirm: Boolean): Boolean;
  25. //用于将目录直接删除或移动到回收站
  26. var
  27. Struct : TSHFileOpStructA;
  28. begin
  29. FillChar(Struct, SizeOf(Struct), );
  30. While pos(';', WichFiles)> do
  31. WichFiles[pos(';', WichFiles)] := #;
  32. WichFiles := WichFiles + ##;
  33. with Struct do
  34. begin
  35. wnd := Owner;
  36. wFunc := FO_Delete;
  37. pFrom := PChar(WichFiles);
  38. pTo := nil;
  39. If not Confirm then fFlags := FOF_NOCONFIRMATION;
  40. If SendToRecycleBin then fFLags := fFlags or FOF_ALLOWUNDO
  41. else fFlags := fFlags or  or FOF_FILESONLY;
  42. hNameMappings := nil;
  43. lpszProgressTitle := nil;
  44. end;
  45. result := (SHFileOperationA(Struct)=) and (not Struct.fAnyOperationsAborted);
  46. end;

  47. function WinMovepath(Owner:Integer;FromFile, Tofile:string;ReNameOnCollision, Confirm:Boolean):Boolean;
  48. //用于将目录进行移动
  49. var
  50. Struct : TSHFileOpStructA;
  51. MultDest: Boolean;
  52. begin
  53. FillChar(Struct, SizeOf(Struct), );
  54. MultDest := pos(';', ToFile)>;
  55. While pos(';', FromFile)> do
  56. FromFile[pos(';', FromFile)] := #;
  57. While pos(';', ToFile)> do
  58. ToFile[pos(';', ToFile)] := #;
  59. FromFile := FromFile + ##;
  60. ToFile := ToFile + ##;
  61. with Struct do
  62. begin
  63. wnd := Owner;
  64. wFunc := FO_Move;
  65. pFrom := PChar(FromFile);
  66. pTo := PChar(ToFile);
  67. fFlags := FOF_ALLOWUNDO;
  68. If MultDest then fFLags := fFlags or FOF_MULTIDESTFILES;
  69. If ReNameOnCollision then fFLags := fFlags or FOF_RENameONCOLLISION;
  70. If Confirm then fFLags := fFlags or FOF_NOCONFIRMATION;
  71. hNameMappings := nil;
  72. lpszProgressTitle := nil;
  73. end;
  74. result := (SHFileOperationA(Struct)=) and (not Struct.fAnyOperationsAborted);
  75. end;

  76. function WinMovefile(Owner:Integer;FromFile, Tofile:string;ReNameOnCollision, Confirm:Boolean):Boolean;
  77. //用于将文件进行移动
  78. var
  79. Struct : TSHFileOpStructA;
  80. MultDest: Boolean;
  81. begin
  82. FillChar(Struct, SizeOf(Struct), );
  83. MultDest := pos(';', ToFile)>;
  84. While pos(';', FromFile)> do
  85. FromFile[pos(';', FromFile)] := #;
  86. While pos(';', ToFile)> do
  87. ToFile[pos(';', ToFile)] := #;
  88. FromFile := FromFile + ##;
  89. ToFile := ToFile + ##;
  90. with Struct do
  91. begin
  92. wnd := Owner;
  93. wFunc := FO_Move;
  94. pFrom := PChar(FromFile);
  95. pTo := PChar(ToFile);
  96. fFlags := FOF_ALLOWUNDO or FOF_FILESONLY;
  97. If MultDest then fFLags := fFlags or FOF_MULTIDESTFILES;
  98. If ReNameOnCollision then fFLags := fFlags or FOF_RENameONCOLLISION;
  99. If Confirm then fFLags := fFlags or FOF_NOCONFIRMATION;
  100. hNameMappings := nil;
  101. lpszProgressTitle := nil;
  102. end;
  103. result := (SHFileOperationA(Struct)=) and (not Struct.fAnyOperationsAborted);
  104. end;

  105. function WinCopypath(Owner: Integer; FromFile, Tofile: string;ReNameOnCollision, Confirm: Boolean): Boolean;
  106. //拷贝目录
  107. var
  108. Struct : TSHFileOpStructA;
  109. MultDest: Boolean;
  110. begin
  111. FillChar(Struct, SizeOf(Struct), ); MultDest := pos(';', ToFile)>;
  112. While pos(';', FromFile)> do
  113. FromFile[pos(';', FromFile)] := #;
  114. While pos(';', ToFile)> do
  115. ToFile[pos(';', ToFile)] := #;
  116. FromFile := FromFile + ##;
  117. ToFile := ToFile + ##;
  118. with Struct do
  119. begin
  120. wnd := Owner;
  121. wFunc := FO_Copy;
  122. pFrom := PChar(FromFile);
  123. pTo := PChar(ToFile);
  124. fFlags := FOF_ALLOWUNDO;
  125. If MultDest then
  126. fFLags := fFlags or FOF_MULTIDESTFILES;
  127. If ReNameOnCollision then
  128. fFLags := fFlags or FOF_RENameONCOLLISION;
  129. If not Confirm then
  130. begin
  131. fFLags := fFlags or FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
  132. end;
  133. hNameMappings := nil;
  134. lpszProgressTitle := nil;
  135. end;
  136. result := (SHFileOperationA(Struct)=) and (not Struct.fAnyOperationsAborted);
  137. end;

  138. function WinCopyfile(Owner: Integer; FromFile, Tofile: string;ReNameOnCollision, Confirm: Boolean): Boolean;
  139. //拷贝文件
  140. var
  141. Struct : TSHFileOpStructA;
  142. MultDest: Boolean;
  143. begin
  144. FillChar(Struct, SizeOf(Struct), ); MultDest := pos(';', ToFile)>;
  145. While pos(';', FromFile)> do
  146. FromFile[pos(';', FromFile)] := #;
  147. While pos(';', ToFile)> do
  148. ToFile[pos(';', ToFile)] := #;
  149. FromFile := FromFile + ##;
  150. ToFile := ToFile + ##;
  151. with Struct do
  152. begin
  153. wnd := Owner;
  154. wFunc := FO_Copy;
  155. pFrom := PChar(FromFile);
  156. pTo := PChar(ToFile);
  157. fFlags := FOF_ALLOWUNDO or FOF_FILESONLY;
  158. If MultDest then
  159. fFLags := fFlags or FOF_MULTIDESTFILES;
  160. If ReNameOnCollision then
  161. fFLags := fFlags or FOF_RENameONCOLLISION;
  162. If not Confirm then
  163. begin
  164. fFLags := fFlags or FOF_NOCONFIRMATION or FOF_NOCONFIRMMKDIR;
  165. end;
  166. hNameMappings := nil;
  167. lpszProgressTitle := nil;
  168. end;
  169. result := (SHFileOperationA(Struct)=) and (not Struct.fAnyOperationsAborted);
  170. end;
复制代码
遍历目录查找文件中的字符并替换
  1. public
  2. { Public declarations }
  3. function replaceStr(sT:string;nSt:string;file1:string):integer;
  4. function findStr(st:string;file1:string):integer;
  5. function CheckExt(allExt:string;file1:string):integer;
  6. procedure getdirlist(dir: string;isrep:integer);
  7. function findStrandRep(st:string;nSt:string;file1:string):integer;
  8. function ReadDirectoryNames(const ParentDirectory: string;
  9. dirList: TStringList; filelist: TStringList): Integer;
  10. end;

  11. var
  12. Form1: TForm1;

  13. implementation

  14. {$R *.dfm}

  15. procedure TForm1.btSingleRepClick(Sender: TObject);
  16. var
  17. file1:string;
  18. begin
  19. if edit1.text='' then begin
  20. showmessage('没有需要替换的字符。');
  21. exit;
  22. end;
  23. if MessageDlg('你确定要替换所有文件中的字符:'+#+'" '+edit1.text+'" 替换成:"'+edit2.text+'" 吗?',
  24. mtWarning, [mbYes, mbNo], ) = mrNo then
  25. begin
  26. exit;
  27. end;
  28. memo1.Lines.Clear;
  29. file1:=FileListBox1.FileName;
  30. if file1='' then exit;
  31. if checkExt(edExt.Text,file1) =  then
  32. if findstr(edit1.Text,file1)= then replaceStr(edit1.text,edit2.text,file1)
  33. else showmessage('没有找到匹配!');

  34. end;
  35. //查找字符
  36. function TForm1.findStr(st:string;file1:string):integer;
  37. var
  38. sl:TStringList;
  39. i,j:integer;
  40. begin
  41. result:=;
  42. try
  43. sl:=TStringList.Create;
  44. sl.LoadFromFile(file1);
  45. j:=sl.Count;

  46. for i:= to j- do begin
  47. if Pos(st,sl.Strings[i])> then
  48. result:=

  49. end;
  50. sl.Free;
  51. except

  52. end;

  53. end;
  54. //查找字符并且替换
  55. function TForm1.findStrandRep(st:string;nSt:string;file1:string):integer;
  56. var
  57. sl:TStringList;
  58. i,j:integer;
  59. begin
  60. result:=;
  61. try
  62. sl:=TStringList.Create;
  63. sl.LoadFromFile(file1);
  64. j:=sl.Count;

  65. for i:= to j- do begin
  66. if Pos(st,sl.Strings[i])> then begin
  67. result:=;
  68. replaceStr(st,nst,file1);
  69. end;
  70. end;
  71. sl.Free;
  72. except

  73. end;

  74. end;
  75. // 替换字符
  76. function TForm1.replaceStr(sT:string;nSt:string;file1:string):integer;
  77. var
  78. a:TStringList;
  79. sNew,sOld:String;
  80. i:integer;
  81. begin
  82. try
  83. a:=TStringList.Create;
  84. a.LoadFromFile(file1);
  85. sNew:=a.text;
  86. sOld:=a.text;
  87. sNew:=StringReplace(sNew,sT,nSt,[rfReplaceAll]);
  88. a.text:=sNew;

  89. i := CompareStr(sNew,sOld);
  90. if i <>  then begin
  91. memo1.Lines.Add('修改了文件:'+file1);
  92. end;

  93. a.savetofile(file1);
  94. a.Free;

  95. for i:= to  do begin
  96. ProgressBar1.Position:=i;
  97. end;
  98. except
  99. result:=;
  100. exit;
  101. end;
  102. result:=;
  103. end;

  104. procedure TForm1.DirectoryListBox2Change(Sender: TObject);
  105. begin
  106. DirectoryListBox2.Drive:=DriveComboBox1.Drive;
  107. fileListBox1.Directory:=DirectoryListBox2.Directory;
  108. end;

  109. procedure TForm1.DriveComboBox1Change(Sender: TObject);
  110. begin
  111. DirectoryListBox2.Drive:=DriveComboBox1.Drive;
  112. end;

  113. procedure TForm1.btFindClick(Sender: TObject);
  114. var sDrive:string;
  115. begin
  116. Memo1.Lines.Clear;
  117. sDrive:= DriveComboBox1.Drive+':';
  118. //0 不替换1替换
  119. getdirList(sDrive,);
  120. showmessage('查找结束!');
  121. end;
  122. //检查扩展名
  123. function Tform1.CheckExt(allExt:string;file1:string):integer;
  124. var
  125. ext:string;
  126. i:integer;
  127. begin
  128. ext:=file1;
  129. i:=pos('.',ext);
  130. while i> do begin
  131. i:=pos('.',ext);
  132. ext:=copy(ext,i+,length(ext)-i+);
  133. end;
  134. if pos(ext,allExt)> then result:= else result:=;
  135. end;
  136. //获得目录列表
  137. procedure TForm1.getdirlist(dir: string;isrep:integer);

  138. var
  139. i: integer;
  140. thedir: TstringList;
  141. thefiles: TstringList;
  142. begin
  143. thedir := TstringList.Create;
  144. thefiles := TstringList.create;
  145. ReadDirectoryNames(dir, thedir, thefiles);
  146. ProgressBar1.Max:=thefiles.Count;

  147. for i :=  to thefiles.Count -  do
  148. begin
  149. if checkExt(edExt.Text,thefiles[i]) =  then begin
  150. if findstr(edit1.Text,dir + '\' + thefiles[i])= then begin
  151. //0 不替换1替换
  152. if isrep= then
  153. replaceStr(edit1.text,edit2.text,dir + '\' + thefiles[i])
  154. else
  155. Memo1.Lines.Add(dir + '\' + thefiles[i]);

  156. ProgressBar1.Position:=i;
  157. end else begin
  158. ProgressBar1.Position:=i;
  159. end;

  160. end;

  161. end;
  162. if thedir.count >  then
  163. begin
  164. for i :=  to thedir.Count -  do
  165. begin
  166. getdirlist(dir + '\' + thedir[i],isrep);

  167. //执行递归调用
  168. end;
  169. end;
  170. thedir.free;
  171. end;
  172. //读目录
  173. function TForm1.ReadDirectoryNames(const ParentDirectory: string;
  174. dirList: TStringList; filelist: TStringList): Integer;
  175. var
  176. Status: Integer;
  177. SearchRec: TSearchRec;
  178. function SlashSep(const Path, S: string): string;
  179. begin
  180. if AnsiLastChar(Path)^ <> '\' then
  181. Result := Path + '\' + S
  182. else
  183. Result := Path + S;
  184. end;
  185. begin
  186. Result := ;
  187. Status := FindFirst(SlashSep(ParentDirectory, '*.*'), faDirectory, SearchRec);
  188. try
  189. while Status =  do
  190. begin
  191. if (SearchRec.Attr and faDirectory = faDirectory) then
  192. begin
  193. if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  194. begin
  195. dirlist.Add(SearchRec.Name);
  196. Memo2.Lines.Add('查找目录:'+SearchRec.Name);
  197. Inc(Result);
  198. end;
  199. end
  200. else
  201. begin
  202. if (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  203. begin
  204. filelist.Add(SearchRec.Name);
  205. Inc(Result);
  206. end;
  207. end;
  208. Status := FindNext(SearchRec);
  209. end;
  210. finally
  211. FindClose(SearchRec);
  212. end;
  213. end;
  214. procedure TForm1.btReplaceClick(Sender: TObject);
  215. var sDrive:string;
  216. begin
  217. if edit1.text='' then begin
  218. showmessage('没有需要替换的字符。');
  219. exit;
  220. end;
  221. if MessageDlg('你确定要替换所有文件中的字符:'+#+'" '+edit1.text+'" 替换成:"'+edit2.text+'" 吗?',
  222. mtWarning, [mbYes, mbNo], ) = mrNo then
  223. begin
  224. exit;
  225. end;
  226. Memo1.Lines.Clear;
  227. sDrive:= DriveComboBox1.Drive+':';
  228. //0 不替换1替换
  229. getdirList(sDrive,);
  230. showmessage('查找结束!');
  231. end;

  232. procedure TForm1.Button4Click(Sender: TObject);
  233. var s,file1:string;
  234. begin
  235. edit2.text:=filtercb.Filter;
  236. end;
  237. procedure TForm1.Button1Click(Sender: TObject);
  238. begin
  239. Memo1.Lines.Clear;
  240. Edit3.Text:=DirectoryListBox2.Directory;
  241. getdirList(DirectoryListBox2.Directory,);
  242. showmessage('查找结束!');

  243. end;

  244. procedure TForm1.Button2Click(Sender: TObject);
  245. begin
  246. if edit1.text='' then begin
  247. showmessage('没有需要替换的字符。');
  248. exit;
  249. end;
  250. if MessageDlg('你确定要替换所有文件中的字符:'+#+'" '+edit1.text+'" 替换成:"'+edit2.text+'" 吗?',
  251. mtWarning, [mbYes, mbNo], ) = mrNo then
  252. begin
  253. exit;
  254. end;
  255. Edit3.Text:=DirectoryListBox2.Directory;
  256. Memo1.Lines.Clear;
  257. getdirList(DirectoryListBox2.Directory,);
  258. showmessage('查找结束!');

  259. end;
  260. procedure TForm1.FileListBox1Click(Sender: TObject);
  261. begin
  262. Edit3.Text:=FilelistBox1.FileName;
  263. end;
  264. procedure TForm1.FileListBox1DblClick(Sender: TObject);
  265. var filename:string;
  266. begin
  267. fileName:=FileListBox1.FileName;
  268. if FileExists(FileName) then
  269. ShellExecute(handle, 'open', PChar(FileName), nil,nil, SW_SHOWNORMAL)
  270. else Showmessage(' 对不起,您打开!');
  271. end;

  272. procedure TForm1.Button3Click(Sender: TObject);
  273. begin
  274. close;
  275. end;
复制代码


扫码关注微信公众号,及时获取最新资源信息!下载附件优惠VIP会员5折;永久VIP免费
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

免责声明:
1、本站提供的所有资源仅供参考学习使用,版权归原著所有,禁止下载本站资源参与商业和非法行为,请在24小时之内自行删除!
2、本站所有内容均由互联网收集整理、网友上传,并且以计算机技术研究交流为目的,仅供大家参考、学习,请勿任何商业目的与商业用途。
3、若您需要商业运营或用于其他商业活动,请您购买正版授权并合法使用。
4、论坛的所有内容都不保证其准确性,完整性,有效性,由于源码具有复制性,一经售出,概不退换。阅读本站内容因误导等因素而造成的损失本站不承担连带责任。
5、用户使用本网站必须遵守适用的法律法规,对于用户违法使用本站非法运营而引起的一切责任,由用户自行承担
6、本站所有资源来自互联网转载,版权归原著所有,用户访问和使用本站的条件是必须接受本站“免责声明”,如果不遵守,请勿访问或使用本网站
7、本站使用者因为违反本声明的规定而触犯中华人民共和国法律的,一切后果自己负责,本站不承担任何责任。
8、凡以任何方式登陆本网站或直接、间接使用本网站资料者,视为自愿接受本网站声明的约束。
9、本站以《2013 中华人民共和国计算机软件保护条例》第二章 “软件著作权” 第十七条为原则:为了学习和研究软件内含的设计思想和原理,通过安装、显示、传输或者存储软件等方式使用软件的,可以不经软件著作权人许可,不向其支付报酬。若有学员需要商用本站资源,请务必联系版权方购买正版授权!
10、本网站如无意中侵犯了某个企业或个人的知识产权,请来信【站长信箱312337667@qq.com】告之,本站将立即删除。
郑重声明:
本站所有资源仅供用户本地电脑学习源代码的内含设计思想和原理,禁止任何其他用途!
本站所有资源、教程来自互联网转载,仅供学习交流,不得商业运营资源,不确保资源完整性,图片和资源仅供参考,不提供任何技术服务。
本站资源仅供本地编辑研究学习参考,禁止未经资源商正版授权参与任何商业行为,违法行为!如需商业请购买各资源商正版授权
本站仅收集资源,提供用户自学研究使用,本站不存在私自接受协助用户架设游戏或资源,非法运营资源行为。
快速回复 返回顶部 返回列表