请问如果我想利用下面的程序备份文件,怎么改可以实现如果文件存在就覆盖或者不拷贝呢?(100分)

  • 主题发起人 主题发起人 moonwang
  • 开始时间 开始时间
M

moonwang

Unregistered / Unconfirmed
GUEST, unregistred user!
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_SILENT ;
//or fof_filesonly
fAnyOperationsAborted:= false;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
try
ShFileOperation(OpStruc );
CopyDirectory:=true;
except
CopyDirectory:=false;
exit;
end;
end;
 
用FILEEXISTS函数先判断有无文件存在。
 
请问应该将这句判断加在程序的那个地方?谢谢!
 
问题: 关于SHFileOperation的详细用法(100分) ( 积分: 100 )
分类: Windows API

来自: luyear, 时间: 2001-07-09 13:45:00, ID: 584812
下面是Fyx的帖子!!!!!!!!!!!我还想知道的详细些!!!!!
来自:Fyx, 时间:00-11-18 9:11:00, ID:397396
以下代码可以把C盘下的文件夹a复制成文件夹b:
uses ..., ShellAPI

procedure TForm1.Button1Click(Sender: TObject);
var
OpStruc: TSHFileOpStruct;
frombuf, tobuf: Array [0..128] of Char;
Begin
FillChar( frombuf, Sizeof(frombuf), 0 );
FillChar( tobuf, Sizeof(tobuf), 0 );
StrPCopy( frombuf, 'c:/a' );
StrPCopy( tobuf, 'c:/b' );
With OpStruc DO Begin
Wnd:= Handle;
wFunc:= FO_COPY;
pFrom:= @frombuf;
pTo:=@tobuf;
fFlags:= FOF_NOCONFIRMATION or FOF_RENAMEONCOLLISION;
fAnyOperationsAborted:= False;
hNameMappings:= Nil;
lpszProgressTitle:= Nil;
end;
ShFileOperation( OpStruc );
end;

来自: honghs, 时间: 2001-07-09 14:33:00, ID: 584841
当然是查msdn啦
SHFILEOPSTRUCT


typedef struct _SHFILEOPSTRUCT{
HWND hwnd;
UINT wFunc;
LPCSTR pFrom;
LPCSTR pTo;
FILEOP_FLAGS fFlags;
BOOL fAnyOperationsAborted;
LPVOID hNameMappings;
LPCSTR lpszProgressTitle;
} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;

Contains information that the SHFileOperation function uses to perform file operations.

hwnd
Window handle to the dialog box to display information about the status of the file operation.
wFunc
Operation to perform. This member can be one of the following values: FO_COPY Copies the files specified in the pFrom member to the location specified in the pTo member.
FO_DELET Deletes the files specified in pFrom. (pTo is ignored.)
FO_MOVE Moves the files specified in pFrom to the location specified in pTo.
FO_RENAME Renames the files specified in pFrom.

pFrom
Address of a buffer to specify one or more source file names. Multiple names must be null-separated. The list of names must be double null-terminated.
pTo
Address of a buffer to contain the name of the destination file or directory. The buffer can contain multiple destination file names if the fFlags member specifies FOF_MULTIDESTFILES. Multiple names must be null-separated. The list of names must be double null-terminated.
fFlags
Flags that control the file operation. This member can be a combination of the following flags: FOF_ALLOWUNDO Preserve Undo information, if possible. If pFrom does not contain fully qualified path and filenames, this flag is ignored.
FOF_CONFIRMMOUSE Not currently implemented.
FOF_FILESONLY Perform the operation on files only if a wildcard file name (*.*) is specified.
FOF_MULTIDESTFILES The pTo member specifies multiple destination files (one for each source file) rather than one directory where all source files are to be deposited.
FOF_NOCONFIRMATION Respond with Yes to All for any dialog box that is displayed.
FOF_NOCONFIRMMKDIR Does not confirm the creation of a new directory if the operation requires one to be created.
FOF_NOCOPYSECURITYATTRIBS Version 4.71. Microsoft® Windows NT® only. The security attributes of the file will not be copied.
FOF_NOERRORUI No user interface will be displayed if an error occurs.
FOF_RENAMEONCOLLISION Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
FOF_SILENT Does not display a progress dialog box.
FOF_SIMPLEPROGRESS Displays a progress dialog box but does not show the file names.
FOF_WANTMAPPINGHANDLE If FOF_RENAMEONCOLLISION is specified, the hNameMappings member will be filled in if any files were renamed.

fAnyOperationsAborted
Value that receives TRUE if the user aborted any file operations before they were completed, or FALSE otherwise.
hNameMappings
Handle to a file name mapping object that contains an array of SHNAMEMAPPING structures. Each structure contains the old and new path names for each file that was moved, copied, or renamed. This member is used only if the fFlags member includes the FOF_WANTMAPPINGHANDLE flag. The handle must be freed by using the SHFreeNameMappings function.
lpszProgressTitle
Address of a string to use as the title of a progress dialog box. This member is used only if fFlags includes the FOF_SIMPLEPROGRESS flag.
If the pFrom or pTo members are unqualified names, the current directories are taken from the global current drive and directory settings as managed by theGetCurrentDirectory andSetCurrentDirectory functions.

来自: luyear, 时间: 2001-07-09 15:31:00, ID: 584860
MSDN说的是VC里面的用法,而且是英文,不好看
你的回答顶多可以得到20分

来自: sonie, 时间: 2001-07-09 19:11:00, ID: 584929
sigh,好吧,我试着把他译出来(只能说个大概,但相信够用)
ShFileOperation只有一个参数是LPSHFILEOPSTRUCT型的相当于delphi中的TSHFileOpStruct;
c语言定义为:
typedef struct _SHFILEOPSTRUCT{
HWND hwnd;
UINT wFunc;
LPCSTR pFrom;
LPCSTR pTo;
FILEOP_FLAGS fFlags;
BOOL fAnyOperationsAborted;
LPVOID hNameMappings;
LPCSTR lpszProgressTitle;
} SHFILEOPSTRUCT, FAR *LPSHFILEOPSTRUCT;

相应的pascal就是:
type
_SHFILEOPSTRUCTA = packed record
Wnd: HWND;
wFunc: UINT;
pFrom: PAnsiChar;
pTo: PAnsiChar;
fFlags: FILEOP_FLAGS;
fAnyOperationsAborted: BOOL;
hNameMappings: Pointer;
lpszProgressTitle: PAnsiChar; { only used if FOF_SIMPLEPROGRESS }
end;
hwnd:用来显示操作状态的对话框句柄。 例中是form1的句柄
wFunc:执行的操作。可以是以下各值:(例中是FO_COPY)
FO_COPY:拷贝pfrom域中指定的(目录,例中是'c:/a')到pto中指定的位置(例中为'c:/b')
FO_DELET:删除pfrom中指定的文件. (pTo不用)
FO_MOVE:移动PFrom中指定的文件到pto中指定的位置。
FO_RENAME:给PFrom中指定的文件改名。
pFrom:指定一个或多个源文件名的缓冲区地址。多个名字必须用NULL分隔。名字列表必须用两个NULL(nil,'/0')来结束。
pTo:目标文件或目录名缓冲区地址。 如果fFlags域指定FOF_MULTIDESTFILES,缓冲区可以包含多个目标文件名。多个名字必须用NULL分隔。名字列表必须用两个NULL(nil,'/0')
fFlags :控制操作的标志,可以是以下各值组合:
FOF_ALLOWUNDO:保留Undo信息, 如果pFrom没有包含全的绝对的路径或文件名此值忽略。
FOF_CONFIRMMOUSE:没有实现.
FOF_FILESONLY:只有文件名使用通配符时(*.*)才对文件操作。
FOF_MULTIDESTFILES: pTo域指一定了多个目标文件.(一个对就一个源文件) 而不是指定一个目录来存放所有源文件
FOF_NOCONFIRMATION:所有显示的对话框全部选择yes to all
FOF_NOCONFIRMMKDIR: 如果需要创建一个新目录不确认。
FOF_NOCOPYSECURITYATTRIBS: 4.71. Microsoft® Windows NT® only. 安全属性不复制.
FOF_NOERRORUI:发生错误时不提供用户接口。
FOF_RENAMEONCOLLISION: move,copy,rename操作时如目标文件存在,给操作的文件另起一个名字。
FOF_SILENT:不显示进度对话框
FOF_SIMPLEPROGRESS:显示进度对话框但不显示文件名。
FOF_WANTMAPPINGHANDLE:如果指定了FOF_RENAMEONCOLLISION 当任何文件改名时将填写hNameMappings 域
fAnyOperationsAborted:当用户在完成前取消任何文件操作时赋值TRUE,否则FALSE.
hNameMappings:一个包含SHNAMEMAPPING结构数组的文件名映射对象句柄. 每一个(SHNAMEMAPPING)结构包括一个旧的或新的目录名为了每一个移动的复制的改名的文件。这个域仅在fFlags域包括FOF_WANTMAPPINGHANDLES标志时使用。句柄必须使用SHFreeNameMappings来释放(用完后)
lpszProgressTitle :进程对话框的标题串地址。仅在fFlags中包括FOF_SIMPLEPROGRESS标志时使用。

如果pFrom和pTo不是一个绝对目录时,当前目录从全局当前盘符和当前目录中取得,同时目录设置由GetCurrentDirectory 和SetCurrentDirectory 函数维护.

来自: luyear, 时间: 2001-07-10 7:40:00, ID: 585108
回答的好极了!!
如果没有更精彩的补充,你至少可以得70分

来自: 慕容乾坤, 时间: 2001-07-13 17:29:00, ID: 586985
我看楼上写的太详细了,根本不需要补充了。
我只是想强调两句,顺便得一点分。 :-)
如果要对多个文件进行操作,在PFROM和PTO两项中的文件名列表中,文件名
要用#0隔开,最后要用#0#0(双#0)终止,切记。

来自: luyear, 时间: 2001-07-22 3:06:00, ID: 589098
多人接受答案了。

得分大富翁: honghs-10,sonie-80,慕容乾坤-10,
 
多人接受答案了。
 
后退
顶部