拷贝?(200分)

  • 主题发起人 主题发起人 金少
  • 开始时间 开始时间

金少

Unregistered / Unconfirmed
GUEST, unregistred user!
1. 如何将一个文件夹的所有文件都拷到另一个文件夹?
2. 如何在拷贝前, 先检测目的磁盘的空间是否够, 如不够则提示, 否则进行下去?

请高手指教!
 
Performs a copy, move, rename, or delete operation on a file system object.

WINSHELLAPI int WINAPI SHFileOperation(

LPSHFILEOPSTRUCT lpFileOp
);


Parameters

lpFileOp

Pointer to an SHFILEOPSTRUCT structure that contains information the function needs to carry out the operation.
Return Values
Returns zero if successful or nonzero if an error occurs.

 
//能检查,有进度窗口
============================================================
ShFileOperation; (文件操作函数)

具体用法参见Win32 API

实例如下

Uses ShellAPI;

function Win_XCOPY(fFROM,fTO:String):boolean;
var FData : TShFileOpStruct;
begin
Fdata.pFrom := PChar(fFrom+#0#0); //设立数据源路径和文件、目录名,支持(?, *通配符)
fdata.pTo := PChar(fTo+#0#0); //设立目标路径
fdata.wFunc := FO_COPY ; //设立操作方式
//FO_COPY 拷贝操作 相当于 XCOPY
//FO_MOVE 移动操作 相当于 MOVE
//FO_DELETE 删除操作 相当于 DELTREE
FData.Wnd := Application.Handle ;
fData.lpszProgressTitle := 'Wait'; //设立提示窗标题
fData.fFlags := FOF_ALLOWUNDO OR FOF_NOCONFIRMMKDIR; //设定操作选项
result:=ShFileOperation( FData ) = 0 ;
end;

做 FO_DELETE 操作时
fFrom是被删除目标的路径,
fTO参数被忽略, 你可以随便赋一个值,
from 和 to 实际上是“多个null结束的字符串”,可以带多个文件名同时做。
比如: 'f1.txt'#0'f2.txt'#0'f3.txt'#0#0 可以同时做 f1.txt和f2.txt和f3.txt
3个文件, 最后要多一个0表示全部结束了
============================================================
 
1.SHFileOperation() //-- Winapi
2.TFileSteam : LoadFrom, CopyFrom.
3.TMemoryStream: LoadFrom ,saveto
3.fopen(),fread();fWrite();
4.FileCreate,FIleOpen,FileWrite
5.LZCopy().FileOpen,FileCreate.//-- Winapi
 
拷贝的方法有很多种,我就不说了,就说2吧
function GetDirectorySize(const ADirectory: string): Integer; //得到目录大小
var
Dir: TSearchRec;
Ret: integer;
Path: string;
begin
Result := 0;
Path := ExtractFilePath(ADirectory);
Ret := Sysutils.FindFirst(ADirectory, faAnyFile, Dir);
if Ret<>NO_ERROR then
exit;
try
while ret=NO_ERROR do
begin
inc(Result, Dir.Size);
if (Dir.Attr in [faDirectory]) and (Dir.Name[1]<>'.') then
Inc(Result, GetDirectorySize(Path + Dir.Name + '/*.*'));
Ret := Sysutils.FindNext(Dir);
end;
finally
Sysutils.FindClose(Dir);
end;
end;
然后用diskfree看看磁盘还剩多少
 
//开发人员指南的方法拷贝给你
function GetDirectory: String;
begin
if not SelectDirectory(Result, [sdAllowCreate, sdPerformCreate, sdPrompt], 0) then
Result := EmptyStr;
end;

procedure CopyDirectoryTree(AHandle: THandle; const AFromDirectory, AToDirectory: String);
var
SHFileOpStruct: TSHFileOpStruct;
FromDir: PChar;
ToDir: PChar;
begin

GetMem(FromDir, Length(AFromDirectory)+2);
try
GetMem(ToDir, Length(AToDirectory)+2);
try

FillChar(FromDir^, Length(AFromDirectory)+2, 0);
FillChar(ToDir^, Length(AToDirectory)+2, 0);

StrCopy(FromDir, PChar(AFromDirectory));
StrCopy(ToDir, PChar(AToDirectory));

with SHFileOpStruct do
begin
Wnd := AHandle; // Assign the window handle
wFunc := FO_COPY; // Specify a file copy
pFrom := FromDir;
pTo := ToDir;
fFlags := FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted := False;
hNameMappings := nil;
lpszProgressTitle := nil;
if SHFileOperation(SHFileOpStruct) <> 0 then
RaiseLastWin32Error;
end;
finally
FreeMem(ToDir, Length(AToDirectory)+2);
end;
finally
FreeMem(FromDir, Length(AFromDirectory)+2);
end;
end;

procedure ToRecycle(AHandle: THandle; const ADirName: String);
var
SHFileOpStruct: TSHFileOpStruct;
DirName: PChar;
BufferSize: Cardinal;
begin
BufferSize := Length(ADirName) +1 +1;
GetMem(DirName, BufferSize);
try
FillChar(DirName^, BufferSize, 0);
StrCopy(DirName, PChar(ADirName));

with SHFileOpStruct do
begin
Wnd := AHandle;
wFunc := FO_DELETE;
pFrom := DirName;
pTo := nil;
fFlags := FOF_ALLOWUNDO;

fAnyOperationsAborted := False;
hNameMappings := nil;
lpszProgressTitle := nil;
end;

if SHFileOperation(SHFileOpStruct) <> 0 then
RaiseLastWin32Error;
finally
FreeMem(DirName, BufferSize);
end;
end;

procedure TMainForm.spbtnGetFromDirClick(Sender: TObject);
begin
edtFromDir.Text := GetDirectory;
end;

procedure TMainForm.spbtnGetToDirClick(Sender: TObject);
begin
edtToDir.Text := GetDirectory;
end;

procedure TMainForm.btnCopyDirectoryClick(Sender: TObject);
begin
CopyDirectoryTree(Handle, edtFromDir.Text, edtToDir.Text);
end;

procedure TMainForm.spbtnRecycleBinClick(Sender: TObject);
begin
edtRecycleDir.Text := GetDirectory;
end;

procedure TMainForm.btnRecycleDirClick(Sender: TObject);
begin
ToRecycle(0, edtRecycleDir.Text);
end;

procedure TMainForm.btnCloseClick(Sender: TObject);
begin
Close;
end;
 
如果我想将一学生档案的某些学生资料(包含Bmp或Jpg 图像数据)的paradox的DB及图像备份到另外的文件夹(或者软盘)!
共有两个文件夹一为DB(存放学生资料的表), 另外是image文件夹(存放学生图像) 我现在具体做法是将DB内的所有表及image内的图片同时拷贝到另外的路径的DB和image中!
存在的问题是学生的图像有的是bmp, 有的是jpg, 无办法知道一张软盘或其它盘可以备份学生资料!
请高手多多指教!
 
把你的两目录放在一个目录下如C:/Data

if Win_XCOPY('C:/Data','C:/Backup/Data') then
showmessage('BackUp Succ')
else ...
 
多人接受答案了。
 
后退
顶部