在Delphi中实现对目录拷贝、删除和搬移的操作 1、拷贝目录 为了能拷贝目录下带有子目录的情况,先定义一个辅助的拷贝函数,它是递归执行的,直到把目录下的所有文件和子目录都拷贝完。 1.1拷贝目录的递归辅助函数:DoCopyDir function DoCopyDir(sDirName:String;sToDirName:String):Boolean; var hFindFile:Cardinal; t,tfile:String; sCurDir:String[255]; FindFileData:WIN32_FIND_DATA; begin sCurDir:=GetCurrentDir; //先保存当前目录 ChDir(sDirName); hFindFile:=FindFirstFile('*.*',FindFileData); if hFindFile< > INVALID_HANDLE_VALUE then begin if not DirectoryExists(sToDirName) then ForceDirectories(sToDirName); repeat tfile:=FindFileData.cFileName; if (tfile='.') or (tfile='..') then Continue; if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then begin t:=sToDirName+'\'+tfile; if not DirectoryExists(t) then ForceDirectories(t); if sDirName[Length(sDirName)]< > '\' then DoCopyDir(sDirName+'\'+tfile,t) else DoCopyDir(sDirName+tfile,sToDirName+tfile); end else begin t:=sToDirName+'\'+tFile; CopyFile(PChar(tfile),PChar(t),True); end; until FindNextFile(hFindFile,FindFileData)=false; FindClose(hFindFile); end else begin ChDir(sCurDir); result:=false; exit; end; ChDir(sCurDir); //回到原来的目录下 result:=true; end; 1.2拷贝目录的函数:CopyDir function CopyDir(sDirName:String;sToDirName:string):Boolean; begin if Length(sDirName)< =0 then exit; Result:=DoCopyDir(sDirName,sToDirName); //拷贝... end;
procedure TForm1.Button1Click(Sender: TObject); var Mytxt : TSearchRec; FindResult : integer; begin //str是你知道的路径,如果路径后有“\”就不用加上'\' FindResult := FindFirst(str + '\*.txt', faAnyFile, Mytxt); //查找文本文件 faAnyFile 表示任意文件,其他类型看一看help while FindResult = 0 do begin ListBox.Items.Add(str +'\'+Mytxt.Name);//找到就加入ListBox中 FindResult := FindNext(Mytxt);//找下一个 end; FindClose(Mytxt);//用完后要记住关闭 end;
var
hFindFile:Cardinal;
t,tfile:String;
sCurDir:String[255];
FindFileData:WIN32_FIND_DATA;
begin
sCurDir:=GetCurrentDir; //先保存当前目录
ChDir(sDirName);
hFindFile:=FindFirstFile('*.*',FindFileData);
if hFindFile< > INVALID_HANDLE_VALUE then
begin
if not DirectoryExists(sToDirName) then
ForceDirectories(sToDirName);
repeat
tfile:=FindFileData.cFileName;
if (tfile='.') or (tfile='..') then Continue;
if FindFileData.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY then
begin
t:=sToDirName+'\'+tfile;
if not DirectoryExists(t) then ForceDirectories(t);
if sDirName[Length(sDirName)]< > '\' then
DoCopyDir(sDirName+'\'+tfile,t)
else
DoCopyDir(sDirName+tfile,sToDirName+tfile);
end
else
begin
t:=sToDirName+'\'+tFile;
CopyFile(PChar(tfile),PChar(t),True);
end;
until FindNextFile(hFindFile,FindFileData)=false;
FindClose(hFindFile);
end
else
begin
ChDir(sCurDir);
result:=false;
exit;
end;
ChDir(sCurDir); //回到原来的目录下
result:=true;
end;
1.2拷贝目录的函数:CopyDir function CopyDir(sDirName:String;sToDirName:string):Boolean;
begin
if Length(sDirName)< =0 then exit;
Result:=DoCopyDir(sDirName,sToDirName); //拷贝...
end;
var
Mytxt : TSearchRec;
FindResult : integer;
begin
//str是你知道的路径,如果路径后有“\”就不用加上'\'
FindResult := FindFirst(str + '\*.txt', faAnyFile, Mytxt);
//查找文本文件 faAnyFile 表示任意文件,其他类型看一看help
while FindResult = 0 do
begin
ListBox.Items.Add(str +'\'+Mytxt.Name);//找到就加入ListBox中
FindResult := FindNext(Mytxt);//找下一个
end;
FindClose(Mytxt);//用完后要记住关闭
end;