如题:
例如我在C:\Temp这个目录下有2个文件夹名字分别为File1 File2, File1目录下面还有其他文件夹....我想要的就是查找出所有文件夹并且在其他位置(比如D盘)重新创建出这些文件夹。
有谁知道怎么用Delphi写的告诉我下,非常感谢!最好有源代码。
例如我在C:\Temp这个目录下有2个文件夹名字分别为File1 File2, File1目录下面还有其他文件夹....我想要的就是查找出所有文件夹并且在其他位置(比如D盘)重新创建出这些文件夹。
有谁知道怎么用Delphi写的告诉我下,非常感谢!最好有源代码。
procedure TForm1.CopyDir(OldDir:string;NewDir:string);
var
sour_path: string;
FileRec: TSearchrec;
tmpstr: string;
Newtmpstr:string;
begin
sour_path := trim(OldDir);
Newtmpstr := Trim(NewDir);
if Newtmpstr[Length(Newtmpstr)] <> '\' then
Newtmpstr := Newtmpstr + '\';
if sour_path[Length(sour_path)] <> '\' then
sour_path := sour_path + '\'; if not DirectoryExists(sour_path) then
begin
ShowMessage('源文件夹不存在');
exit;
end; if not DirectoryExists(Newtmpstr) then
ForceDirectories(Newtmpstr);
tmpstr := sour_path;
if FindFirst(sour_path + '*.*', faAnyfile, FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name <> '.') and (FileRec.Name <> '..')) then
begin
Newtmpstr := Newtmpstr + FileRec.Name + '\';
if not DirectoryExists(Newtmpstr) then
ForceDirectories(Newtmpstr);
CopyDir(sour_path + FileRec.Name + '\',Newtmpstr);
tmpstr := '';
Newtmpstr := '';
end;
end
else if ((FileRec.Attr and faDirectory) = 0) then
begin
tmpstr := tmpstr + FileRec.Name;
if FileExists(Newtmpstr) then
CopyFile(PChar(tmpstr),PChar(Newtmpstr),true)
else
CopyFile(PChar(tmpstr),PChar(Newtmpstr + FileRec.Name),False);
end; until FindNext(FileRec) <> 0; FindClose(FileRec);
end;
CopyDir('c:\Temp','d:\');
我只是想在新目录创建文件夹不想复制文件夹里面的内容,Thank you!
else if ((FileRec.Attr and faDirectory) = 0) then
begin
tmpstr := tmpstr + FileRec.Name;
if FileExists(Newtmpstr) then
CopyFile(PChar(tmpstr),PChar(Newtmpstr),true)
else
CopyFile(PChar(tmpstr),PChar(Newtmpstr + FileRec.Name),False);
end;
执行到这句时出错:ForceDirectories(Newtmpstr);
提示:Unable to create directory
Newtmpstr路径不对。
var
sour_path: string;
FileRec: TSearchrec;
tmpstr: string;
Newtmpstr:string;
begin
sour_path := trim(OldDir);
Newtmpstr := Trim(NewDir);
if Newtmpstr[Length(Newtmpstr)] <> '\' then
Newtmpstr := Newtmpstr + '\';
if sour_path[Length(sour_path)] <> '\' then
sour_path := sour_path + '\'; if not DirectoryExists(sour_path) then
begin
ShowMessage('源文件夹不存在');
exit;
end; if not DirectoryExists(Newtmpstr) then
ForceDirectories(Newtmpstr);
tmpstr := sour_path;
if FindFirst(sour_path + '*.*', faAnyfile, FileRec) = 0 then
repeat
if ((FileRec.Attr and faDirectory) <> 0) then
begin
if ((FileRec.Name <> '.') and (FileRec.Name <> '..')) then
begin
Newtmpstr := Newtmpstr + FileRec.Name + '\';
if not DirectoryExists(Newtmpstr) then
ForceDirectories(Newtmpstr);
CopyDir(sour_path + FileRec.Name + '\',Newtmpstr);
Newtmpstr := Copy(Newtmpstr,1,Length(Newtmpstr) - Length(FileRec.Name)-1);
// tmpstr := '';
// Newtmpstr := '';
end;
end ;
{ else if ((FileRec.Attr and faDirectory) = 0) then
begin
tmpstr := tmpstr + FileRec.Name;
if FileExists(Newtmpstr) then
CopyFile(PChar(tmpstr),PChar(Newtmpstr),true)
else
CopyFile(PChar(tmpstr),PChar(Newtmpstr + FileRec.Name),False);
end;} until FindNext(FileRec) <> 0; FindClose(FileRec);
end;//现在好了
//老大,如果再有问题,你自己单步调试一下
非常感谢您的帮助,谢谢!我现在也正努力学习呵呵。