将目录A的内容复制到目录B [英] copy contents of directory A to directory B
本文介绍了将目录A的内容复制到目录B的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将目录A的内容复制到目录B(所有子目录和文件).是否有一个简单的语句可以做到这一点?我在Google上搜索了"copydir"(似乎是个不错的猜测),但没有得到任何看起来像是直接答案的东西.谢谢.
I''d like to copy the contents of directory A to directory B (all sub-directories and files). Is there a simple statement that will do this? I have googled "copydir" (seemed like a good guess) but didn''t get anything that looked like a straight answer. Thanks.
推荐答案
我认为,缺少帮助程序功能,您将不得不逐个处理文件.
值得庆幸的是,CP上有一个辅助功能:):
无需使用SHFileOperation复制,移动和删除文件和目录 [
I think, short of a helper function, you''d have to go one-by-one on the files.
Thankfully, there''s a helper function here on CP :) :
Copy, Move and Delete files and directories without using SHFileOperation[^]
Cheers.
看看 SHFileOperation [ ^ ]/ IFileOperation [ ^ ]
那很容易:
Thats easy:
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
// this: WalkFs
class vWalkFs
{
public: // vWalkFs
virtual int Enter(const TCHAR* full,WIN32_FIND_DATA* fd) = 0;
virtual int Leave(const TCHAR* full,WIN32_FIND_DATA* fd) = 0;
};
unsigned int scopy(TCHAR* dst,const unsigned int cbdst,const unsigned int pos,const TCHAR* src)
{
unsigned int ix,len = _tcslen(src);
if((pos+len)<cbdst)
{
for(ix=0;ix<len;ix++) dst[pos+ix]=src[ix]; dst[pos+ix]=0;
return pos+len;
}
return 0;
}
static void __WalkFs(TCHAR* path,const unsigned int cb,const unsigned int plen,vWalkFs& walk)
{
typedef struct
{
static inline int isdir(WIN32_FIND_DATA& fd)
{
return FILE_ATTRIBUTE_DIRECTORY & fd.dwFileAttributes;
}
static inline int isvalid(WIN32_FIND_DATA& fd)
{
if(!(FILE_ATTRIBUTE_DIRECTORY & fd.dwFileAttributes)) return 2;
if('.'!=fd.cFileName[0]) return 1;
if(0==fd.cFileName[1]) return 0;
if('.'!=fd.cFileName[1]) return 1;
if(0==fd.cFileName[2]) return 0;
return 1;
}
}_;
WIN32_FIND_DATA fd;
HANDLE hf;
unsigned int flen;
scopy(path,cb,plen,__TEXT("\\*.*"));
hf = FindFirstFile(path,&fd);
if(INVALID_HANDLE_VALUE!=hf)
{
do
{
if( _::isvalid(fd) )
{
flen = scopy(path,cb,plen+1,fd.cFileName);
if(walk.Enter(path,&fd))
{
if(_::isdir(fd)) __WalkFs(path,cb,flen,walk);
walk.Leave(path,&fd);
}
}
} while(FindNextFile(hf,&fd));
FindClose(hf);
}
path[plen] = 0;
}
void WalkFs(vWalkFs& walk,const TCHAR* path)
{
TCHAR full[_MAX_PATH+_MAX_FNAME+0x400];
unsigned int cbf = sizeof(full)/sizeof(full[0]);
unsigned int flen = scopy(full,cbf,0,path);
if(flen && ('\\'==full[flen-1])) --flen;
__WalkFs(full,cbf,flen,walk);
}
////////////////////////
// main
int _tmain(int argc, _TCHAR* argv[])
{
TCHAR src[0x1000];
if(2<argc)
{
class w : public vWalkFs
{
public: // vWalkFs
virtual int Enter(const TCHAR* full,WIN32_FIND_DATA* fd)
{
scopy(dst,sizeof(dst)/sizeof(dst[0]),len,full+rel);
if(FILE_ATTRIBUTE_DIRECTORY & fd->dwFileAttributes)
{
CreateDirectory(dst,0);
}
else
{
_tprintf(__TEXT("copy: %s -> %s\r\n"),full,dst);
CopyFile(full,dst,0);
}
return 1;
}
virtual int Leave(const TCHAR* full,WIN32_FIND_DATA* fd)
{
return 1;
}
public:
w(const TCHAR* path)
{
len = scopy(dst,sizeof(dst)/sizeof(dst[0]),0,path);
rel = 0;
CreateDirectory(dst,0);
}
TCHAR dst[0x1000];
unsigned int len;
unsigned int rel;
} walk(argv[2]);
walk.rel = scopy(src,sizeof(src)/sizeof(src[0]),0,argv[1]);
WalkFs(walk,src);
_tprintf(__TEXT("<key>")); _gettch();
}
return 0;
}
祝你好运.
Good luck.
这篇关于将目录A的内容复制到目录B的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文