delphi 目录操作函数
2006-10-19 16:53
363 查看
简单实用的复制整个目录的方法
function CopyDirectory(const Hwd : LongWord; const SourcePath, DestPath : string): SmallInt;
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
Result := -1;
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy( frombuf, SourcePath);
StrPCopy( tobuf, DestPath);
With OpStruc DO Begin
Wnd:= Hwd;
wFunc:= FO_COPY;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
if ShFileOperation(OpStruc) = 0 then
Result := 0;
end;
Function Copydirectory(Source:string;pDirectory:string;pFilter:string):boolean;
//目录拷贝source :源目录 directory:目标目录 pFilter:文件类型筛选'/*.*'或'/*.???'
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy(frombuf, Source+pFilter);
StrPCopy( tobuf, pdirectory);
if not directoryexists(pdirectory) then
if not createdir(pdirectory) then
begin
showmessage('不能创建目录'+pdirectory+chr(13)+chr(10)+'或无该目录权限');
exit;
end;
With OpStruc DO Begin
Wnd:= 0;
wFunc:= FO_Copy;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION or fof_filesonly;
fAnyOperationsAborted:= false;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
try
ShFileOperation(OpStruc );
CopyDirectory:=true;
except
CopyDirectory:=false;
exit;
end;
end;
为了能拷贝目录下带有子目录的情况,先定义一个辅助的拷贝函数,
它是递归执行的,直到把目录下的所有文件和子目录都拷贝完。
---- 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;
=============================================================================
uses ShellAPI;
....
ShellExecute(handle,'open', 'cmd.exe', 'copy c:/abc c:/def /e', '', sw_Normal);
function CopyDirectory(const Hwd : LongWord; const SourcePath, DestPath : string): SmallInt;
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
Result := -1;
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy( frombuf, SourcePath);
StrPCopy( tobuf, DestPath);
With OpStruc DO Begin
Wnd:= Hwd;
wFunc:= FO_COPY;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
if ShFileOperation(OpStruc) = 0 then
Result := 0;
end;
Function Copydirectory(Source:string;pDirectory:string;pFilter:string):boolean;
//目录拷贝source :源目录 directory:目标目录 pFilter:文件类型筛选'/*.*'或'/*.???'
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy(frombuf, Source+pFilter);
StrPCopy( tobuf, pdirectory);
if not directoryexists(pdirectory) then
if not createdir(pdirectory) then
begin
showmessage('不能创建目录'+pdirectory+chr(13)+chr(10)+'或无该目录权限');
exit;
end;
With OpStruc DO Begin
Wnd:= 0;
wFunc:= FO_Copy;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION or fof_filesonly;
fAnyOperationsAborted:= false;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
try
ShFileOperation(OpStruc );
CopyDirectory:=true;
except
CopyDirectory:=false;
exit;
end;
end;
为了能拷贝目录下带有子目录的情况,先定义一个辅助的拷贝函数,
它是递归执行的,直到把目录下的所有文件和子目录都拷贝完。
---- 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;
=============================================================================
uses ShellAPI;
....
ShellExecute(handle,'open', 'cmd.exe', 'copy c:/abc c:/def /e', '', sw_Normal);
相关文章推荐
- Delphi中关于文件、目录操作的函数
- Delphi学习之函数 ④扩展的文件及目录操作函数
- Delphi 文件、目录等常用操作函数 - delphi - gliethttp
- Delphi 文件、目录等常用操作函数 - delphi - gliethttp
- Delphi中使用API将目录删除函数
- 目录和文件中的操作函数
- 文件/目录管理(3)---文件其他操作函数及目录操作函数
- DELPHI目录操作示例
- 两个delphi下遍历指定目录下指定类型文件的函数
- Linux文件与目录操作函数
- Delphi文件操作所涉及的一些函数 附例子
- php 文件、目录操作函数
- 文件目录操作函数
- DELPHI中通过SHBrowseForFolder函数实现选择目录
- 学习了目录操作函数 编写了一个源…
- 总结文件操作函数-目录(三)-C语言
- php目录操作函数之获取目录与文件的类型
- Delphi中的操作二进制文件的两个重要函数
- *Delphi字符串的基本操作与常用函数
- C#文件、目录操作相关函数