如何复制文件和文件夹?

KevinCEC 2005-09-28 10:24:18
不用SHFILEOPSTRUCT
怎么样将一个文件夹下面的所有文件、子文件夹里面的文件,都复制到另外一个文件夹下面?

给个具体点方法,不胜感激
...全文
170 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
布学无数 2005-09-28
  • 打赏
  • 举报
回复
写段伪代码给你,回去改改就成了:
typedef struct CopyFileInfo{
BOOL fIsDir;
char szFileOrDir[MAX_PATH];
}CFINFO;

CList pDynList;

BOOL IsDirectory(LPSTR pszDir)
{
DWORD dwAttr = GetFileAttribute(pszDir);
if(dwAttr == -1L)return FALSE;
if(dwAttr & FILE_ATTRIBUTE_DIRECTORY)return TRUE;
return FALSE;
}

// 遍历源文件夹,并创建一个文件列表
// pszDir: 当前遍历的文件夹
// pszBeginDir: 开始遍历的文件夹,创建文件列表时需要根据该参数来确定相对路径
// 例如:CreateCopyFileList("c:\\windows\\font","c:\\windows");
// 当找到c:\windows\font\hello.fon文件时,即可计算出相对路径为:font\hello.fon
BOOL CreateCopyFileList(LPSTR pszDir,LPSTR pszBeginDir)
{
if(!IsDirectory(pszDir))return FALSE;
CFINFO *pCFInf;
HFIND hfind;
FIND_DATA fd;
hfind = FindFirstFile("*.*",&fd);
while(hfind)
{
if(fd.filename == "." || fd.filename == ".."){}
{
pCFInf = new CFINFO[1];
lstrcpy(pCFInf->szFileOrDir,(pszDir + "\\" + fd.filename) + lstrlen(pszBeginDir) + 1);
if(IsDirectory(pszDir + "\\" + fd.filename))
{
pCFInf->fIsDir = TRUE;
pDynList->add(pCFInf);
CreateCopyFileList(pszDir + "\\" + fd.filename,pszBeginDir);
}
else
{
pCFInfo->fIsDir = FALSE;
pDynList->add(pCFInf);
}
}
}
CloseFind(hfind);
return TRUE;
}

// 根据文件列表复制文件或创建文件夹
BOOL CopyFilesAndDirectories(LPSTR pszTargetDir,LPSTR pszSourceDir)
{
int i;
CFINFO *pCFInf;
for(i = 0;i < pDynList.count;i ++)
{
pCFInf = (CPINFO *)pDynList.item(i);
if(pCFInf->fIsDir)CreateDirectory(pszTargetDir + "\\" + pCFInf->szFileOrDir);
else CopyFile(pszTargetDir + "\\" + pCFInf->szFileOrDir,pszSourceDir + "\\" + pCFInf->szFileOrDir);
}
return ReleaseList();
}

// 释放列表中的指针空间
BOOL ReleaseList()
{
for(int i = 0;i < pDynList->count;i ++)delete [] pDynList->item(i);
return TRUE;
}
nkwesley 2005-09-28
  • 打赏
  • 举报
回复
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,
LPCTSTR lpNewFileName,
BOOL bFailIfExists
);

Parameters
lpExistingFileName
[in] Pointer to a null-terminated string that specifies the name of an existing file.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path. For more information, see Naming a File.


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


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


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


bFailIfExists
[in] If this parameter is TRUE and the new file specified by lpNewFileName 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. To copy security attributes, use the SHFileOperation function.

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. For more information, see Retrieving and Changing File Attributes.

This function fails with ERROR_ACCESS_DENIED if the destination file already exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_READONLY attribute set.


When CopyFile is used to copy an encrypted file, it attempts to encrypt the destination file with the keys used in the encryption of the source file. If this cannot be done, this function attempts to encrypt the destination file with default keys, as in Windows 2000. If both of these methods cannot be done, CopyFile fails with an ERROR_ENCRYPTION_FAILED error code.


Windows 2000: When CopyFile is used to copy an encrypted file, the function attempts to encrypt the destination file with the default keys. No attempt is made to encrypt the destination file with the keys used in the encryption of the source file. If it cannot be encrypted, CopyFile completes the copy operation without encrypting the destination file.





Windows Me/98/95: CopyFileW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.


Example Code
For an example, see Searching for Files and Changing File Attributes.

Requirements
Client: Included in Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, and Windows 95.
Server: Included in Windows Server 2003, Windows 2000 Server, and Windows NT Server.
Unicode: Implemented as Unicode and ANSI versions. Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.
Header: Declared in Winbase.h; include Windows.h.
Library: Use Kernel32.lib.


16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

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