想用c或c++程序实现功能,将文件或文件夹(包括文件夹中的文件)从原路径拷贝或剪切到目标路径。

pzytony 2003-09-04 05:19:16
想用c或c++程序实现功能,将文件或文件夹(包括文件夹中的文件)从原路径拷贝或剪切到目标路径。注意:不使用systen之类的函数。此外,有没有方法直接删除文件夹及其文件夹中的文件。
谢谢。

...全文
257 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Skt32 2003-09-04
  • 打赏
  • 举报
回复
删除目录及目录下所有文件与子目录


删除目录及目录下所有文件与子目录


VC++只提供了删除一个空目录的函数,而用往往希望删除其下有很多子目录与
文件的目录。为了实现这一功能,我编写了DeleteDirectory 函数,它可以实现
这一功能。

函数原型:BOOL DeleteDirectory(char *DirName);
返回值:成功删除时返回TRUE,否则返回FALSE
参数DirName为要删除的目录名,必须为绝对路径名,如“c:\\temp"。
函数定义如下:
BOOL DeleteDirectory(char *DirName)
{
CFileFind tempFind;
char tempFileFind[200];
sprintf(tempFileFind,"%s\\*.*",DirName);
BOOL IsFinded=(BOOL)tempFind.FindFile(tempFileFind);
while(IsFinded)
{
IsFinded=(BOOL)tempFind.FindNextFile();
if(!tempFind.IsDots())
{
char foundFileName[200];
strcpy(foundFileName,tempFind.GetFileName().GetBuffer(200));
if(tempFind.IsDirectory())
{
char tempDir[200];
sprintf(tempDir,"%s\\%s",DirName,foundFileName);
DeleteDirectory(tempDir);
}
else
{
char tempFileName[200];
sprintf(tempFileName,"%s\\%s",DirName,foundFileName);
DeleteFile(tempFileName);
}
}
}
tempFind.Close();
if(!RemovwDirctory(DirName))
{
MessageBox(0,"删除目录失败!","警告信息",MK_OK);
return FALSE;
}
return TRUE;
}

Skt32 2003-09-04
  • 打赏
  • 举报
回复
msdn:


Platform SDK: Files and I/O
CopyFile
The CopyFile function copies an existing file to a new file.

The CopyFileEx function provides two additional capabilities. CopyFileEx can call a specified callback function each time a portion of the copy operation is completed, and CopyFileEx can be canceled during the copy operation.

BOOL CopyFile(
LPCTSTR lpExistingFileName, // name of an existing file
LPCTSTR lpNewFileName, // name of new file
BOOL bFailIfExists // operation if file exists
);
Parameters
lpExistingFileName
[in] Pointer to a null-terminated string that specifies the name of an existing file.
Windows NT/2000: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see File Name Conventions.

Windows 95/98: This string must not exceed MAX_PATH characters.

lpNewFileName
[in] Pointer to a null-terminated string that specifies the name of the new file.
Windows NT/2000: In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to nearly 32,000 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see File Name Conventions.

Windows 95/98: This string must not exceed MAX_PATH characters.

bFailIfExists
[in] Specifies how this operation is to proceed if a file of the same name as that specified by lpNewFileName already exists. If this parameter is TRUE and the new file already exists, the function fails. If this parameter is FALSE and the new file already exists, the function overwrites the existing file and succeeds.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, call GetLastError.

Remarks
Security attributes for the existing file are not copied to the new file.

File attributes for the existing file are copied to the new file. For example, if an existing file has the FILE_ATTRIBUTE_READONLY file attribute, a copy created through a call to CopyFile will also have the FILE_ATTRIBUTE_READONLY file attribute.

MAPI: For more information, see Syntax and Limitations for Win32 Functions Useful in MAPI Development.

Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.

See Also
File I/O Overview, File I/O Functions, CopyFileEx, CreateFile, MoveFile

Built on Friday, May 12, 2000Requirements
Windows NT/2000: Requires Windows NT 3.1 or later.
Windows 95/98: Requires Windows 95 or later.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT/2000.
See Also
File I/O Overview, File I/O Functions, CopyFileEx, CreateFile, MoveFile
Skt32 2003-09-04
  • 打赏
  • 举报
回复

使用C++语言,如何将一个文件从一个目录复制到另一个目录中

编号:QA004504
建立日期: 2001年12月23日 最后修改日期:2001年12月23日
所属类别:

C/C++ - 磁盘、文件和目录

y_zp:
使用系统:WIN2000
使用软件:VC6++
问题是:使用C++语言,如何将一个文件从一个目录复制到另一个目录中?
水平:初级

回答:

你可以调用API函数CopyFile,如:
CopyFile("C:\\MyFile.txt", "D:\\MyDir\MyFile.txt", FALSE);
你也可以使用SHFileOperation来实现更复杂的操作。

此问题由李海回答。

65,208

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧