最近在整理数据的时候发现,在window系统中不能直接复制、删除路径名超过一定大小的文件。这样在大批量复制数据的时候,就会让人感觉很蛋疼,手动改名字也超麻烦。于是用cpp写了个简单的程序用于拷贝文件。
拷贝文件,要用到的函数,肯定包括 创建文件,创建文件夹,用的分别是MFC提供的CopyFile() and CreateDirectory()。其实在MSDN中都有这些文件操作类函数的说明,以前找不到,附上网址吧。https://msdn.microsoft.com/en-us/library/windows/desktop/aa363851(v=vs.85).aspx
CopyFile() 中关于文件名长度的地方:
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, seeNaming a File.
中文意思大概是,如果想要把文件名长度限制拓展到32767个字符,需要调用这个函数的Unicode版本,同时在路径前面加上 \\?\ 。
通过观察CopyFile()函数的调用和实践,只需要在路径前加好前缀,程序便能自己调用Unicode版本的函数。这样便可以简单地绕过系统关于文件名长度的限制。
上源码,前面部分是为了在控制台程序中调用MFC函数而加的。此外需要在项目工程属性的General中,把Use of MFC 改为Use MFC in a Shared DLL。当然也有其他调用的方法。
#include <afx.h>
#include <iostream>
using namespace std;
void CopyFile_PermitLongName(const string& DstUrl,const string& SrcUrl);
int main()
{ string inputSrc("H:\\CT-SIEMENS\\DEMO_Flash"),
inputDst("E:\\CT-SIEMENS");
string sDstUrl("\\\\?\\"),sSrcUrl("\\\\?\\");
sDstUrl.append(inputDst);
sSrcUrl.append(inputSrc);
CopyFile_PermitLongName(sDstUrl,sSrcUrl);
system("pause");
}
//copy files under path(srcUrl) to path(dstUrl).
//when encountering folders,get into deeper recursion.
void CopyFile_PermitLongName(const string& sDstUrl,const string& sSrcUrl)
{
string sFileCopyFrom(sSrcUrl),sFileCopyTo(sDstUrl); //used as buffer for progressing.
HANDLE hFile;
WIN32_FIND_DATAA wfd;
hFile = FindFirstFile(sSrcUrl.c_str(),&wfd);
if (hFile == INVALID_HANDLE_VALUE)
return;
else
{
if (wfd.cFileName[0] == '.') //encounter upper folder.
return;
else if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) //encounter deeper folder.
{
HANDLE hFileNextFolder;
WIN32_FIND_DATA wfdNext;
string sThingsInSrcUrl(sSrcUrl);
sThingsInSrcUrl.append("\\*.*");
sFileCopyTo.append("\\").append(wfd.cFileName);
if (!CreateDirectory(sFileCopyTo.c_str(),NULL))
{
cout<<"Error happens while creating new directory.Error Number:"<<GetLastError()<<endl;
}
hFileNextFolder = FindFirstFile(sThingsInSrcUrl.c_str(),&wfdNext);
if (hFileNextFolder == INVALID_HANDLE_VALUE) return;
do {
sFileCopyFrom = sSrcUrl;
sFileCopyFrom.append("\\");
sFileCopyFrom.append(wfdNext.cFileName);
CopyFile_PermitLongName(sFileCopyTo,sFileCopyFrom);
}while (FindNextFile(hFileNextFolder,&wfdNext));
}
else //encounter file.
{
sFileCopyTo.append("\\");
sFileCopyTo.append(wfd.cFileName);
if (!CopyFile(sFileCopyFrom.c_str(),sFileCopyTo.c_str(),FALSE))
{
cout<<"Error while copying file."<<endl;
cout<<"FILE PATH: "<<sFileCopyFrom.c_str();
}
}
}
}