巨菜的问题:如何实现文件的拷贝,移动,删除;目录(子目录)的创建、删除?(20分)

  • 主题发起人 主题发起人 johnrain
  • 开始时间 开始时间
J

johnrain

Unregistered / Unconfirmed
GUEST, unregistred user!
我刚刚接触delphi,想实现上述操作。请别笑话我。
 
MkDir(str);
ChDir(str);
GetDir(DriveID,str);
SetCurrentDir(str);
IOResult --上面几个过程调用成功即返回0值
{This way uses a File stream.}
Procedure FileCopy( Const sourcefilename, targetfilename: String );
Var
S, T: TFileStream;
begin
S := TFileStream.Create( sourcefilename, fmOpenRead );
try
T := TFileStream.Create( targetfilename,
fmOpenWrite or fmCreate );
try
T.CopyFrom(S, S.Size ) ;
finally
T.Free;
end;
finally
S.Free;
end;
end;

{This way uses memory blocks for read/write.}
procedure FileCopy(const FromFile, ToFile: string);
var
FromF, ToF: file;
NumRead, NumWritten: Word;
Buf: array[1..2048] of Char;
begin
AssignFile(FromF, FromFile);
Reset(FromF, 1);
{ Record size = 1 }
AssignFile(ToF, ToFile);
{ Open output file }
Rewrite(ToF, 1);
{ Record size = 1 }
repeat
BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
BlockWrite(ToF, Buf, NumRead, NumWritten);
until (NumRead = 0) or (NumWritten <> NumRead);
CloseFile(FromF);
CloseFile(ToF);
end;

{This one uses LZCopy, which USES LZExpand.}
procedure CopyFile(FromFileName, ToFileName: string);
var
FromFile, ToFile: File;
begin
AssignFile(FromFile, FromFileName);
{ Assign FromFile to FromFileName }
AssignFile(ToFile, ToFileName);
{ Assign ToFile to ToFileName }
Reset(FromFile);
{ Open file for input }
try
Rewrite(ToFile);
{ Create file for output }
try
{ copy the file an if a negative value is returned }
{ raise an exception }
if LZCopy(TFileRec(FromFile).Handle, TFileRec(ToFile).Handle) < 0
then
raise EInOutError.Create('Error using LZCopy')
finally
CloseFile(ToFile);
{ Close ToFile }
end;
finally
CloseFile(FromFile);
{ Close FromFile }
end;
end;
 
你上述的几个操作,都可以利用WINDOW API 函数来解决。你可以在DELPHI HELP 中的
WINDOWS SDK 中找到相应的函数,用法都挺简单的。
 
删除目录:
function DelDirectory(const Source:string): boolean;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fodo
begin
Wnd := 0;
wFunc := FO_DELETE;
pFrom := PChar(source+#0);
pTo := #0#0;
fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
复制目录:
function CopyDirectory(const Source, Dest: string): boolean;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fodo
begin
Wnd := 0;
wFunc := FO_COPY;
pFrom := PChar(source+#0);
pTo := PChar(Dest+#0);
fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR ;
end;
Result := (SHFileOperation(fo) = 0);
end;

重新命名:
用MoveFile()或者下面的函数也可以。
RenameFile('c:/a','c:/b')好想也可以?Win2K。
//RenDirectory('d:/wt2','d:/bcde');
function RenDirectory(const OldName,NewName:string): boolean;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fodo
begin
Wnd := 0;
wFunc := FO_RENAME;
pFrom := PChar(OldName+#0);
pTo := pchar(NewName+#0);
fFlags := FOF_NOCONFIRMATION+FOF_SILENT;
end;
Result := (SHFileOperation(fo) = 0);
end;
//Copy 多个文件的处理:
function CopyFiles(const Source,Dest: string): boolean;
var
fo: TSHFILEOPSTRUCT;
begin
FillChar(fo, SizeOf(fo), 0);
with fodo
begin
Wnd := 0;
wFunc := FO_COPY;
pFrom := @source[1];
pTo :=pchar(dest);
fFlags := FOF_NOCONFIRMATION+FOF_NOCONFIRMMKDIR ;
end;
Result := (SHFileOperation(fo) = 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
str:string;
i:integer;
begin
if opendialog1.Execute then
begin
for i:=0 to OpenDialog1.Files.Count-1do
str:=str+OpenDialog1.Files.strings+#0;
showmessage(str);
str:=str+#0;
CopyFiles(str,'d:/temp');
end;
end;
 
多人接受答案了。
 
后退
顶部