用C++怎么实现批量拷贝文件。。。。。。。。。。急。。。。。。。。。。。。
把指定文件夹下的所有文件都拷贝到另外的一个文件夹里面,用C++怎么实现,谢谢了!!!!!!!!!!!!!!!!!
[解决办法]
最简单的方法:system( "copy c:\\temp\\*.c d:\\temp ");
如果一定要全自己实现,去查findfirst/findnext的帮助文档。
[解决办法]
以下代码将C:\111目录下所有文件拷贝到C:\222中,如果C:\222不存在就提示是否创建。
SHFILEOPSTRUCT op;
String strFrom = "C:\\111\\*.*\0 ", strTo = "C:\\222\0 ";
op.hwnd = NULL;
op.wFunc = FO_COPY;
op.pFrom = strFrom.c_str();
op.pTo = strTo.c_str();
op.fFlags = FOF_WANTNUKEWARNING;
::SHFileOperation(&op);
[解决办法]
弄个循环调用copyfile吧
[解决办法]
如果你知道怎么复制一个文件的话,这个很简单,打开源文件为只读,目标文件为可写,从源文件读出来,马上写入目标文件,这样用一个循环,把某个目录下的文件一个一个的复制到另一个目录就可以了。
[解决办法]
先遍历文件夹,把需要拷贝的文件加到CList <FILELIST, FILELIST&> m_ltFileList 中
然后执行:
iCount = m_ltFileList.GetCount();
while ( iCount > 0 )
{
lpFile = &m_ltFileList.GetTail();
strTemp.Format( "%s ", lpFile-> strFilePath );
hFind=::FindFirstFile( strTemp, &FindFileData );
if ( INVALID_HANDLE_VALUE != hFind )
{
iFileSize = ( __int64)FindFileData.nFileSizeHigh * 4 * 1024 * 1024 * 1024;
iFileSize = iFileSize + FindFileData.nFileSizeLow;
}
if ( lpFile-> iFileSize != iFileSize )
{//文件大小发生变化说明文件还没有拷贝完毕!
m_ltFileList.RemoveTail();
iCount = m_ltFileList.GetCount();
continue;
}
try
{
sprintf( szSource, lpFile-> strFilePath );
sprintf( szTarget, "%s%s ", m_pCurTransferInfo-> strTargetPath,
lpFile-> strFileName );
if (::CopyFile( szSource, szTarget, FALSE) )
{
if ( !::DeleteFile( szSource ) )
{
strTemp.Format( "删除源文件(%s)失败! ", lpFile-> strFilePath );
WriteLog( strTemp, " " );
}
else
{
iTransferFileCount++;
strTemp.Format( "迁移源文件(%s)至(%s)成功! ",
lpFile-> strFilePath, m_pCurTransferInfo-> strTargetPath );
WriteLog( strTemp, " " );
}
}
else
{
strTemp.Format( "复制源文件(%s)至(%s)失败! ",
lpFile-> strFilePath, m_pCurTransferInfo-> strTargetPath );
WriteLog( strTemp, " " );
}
m_ltFileList.RemoveTail();
iCount = m_ltFileList.GetCount();
}
catch (CFileException* pEx)
{
TCHAR szErrMsg[255];
pEx-> GetErrorMessage(szErrMsg, 255);
strTemp.Format( "文件迁移失败,源文件:%s,目标路径:%s,错误信息:%s ",
lpFile-> strFilePath, m_pCurTransferInfo-> strTargetPath, szErrMsg );
m_ltFileList.RemoveTail();
iCount = m_ltFileList.GetCount();
WriteLog( strTemp, " " );
}
}
[解决办法]
给个函数你,LINUX和WIN32
int LoadFileServer::CopyFeeFiles()
{
char content[128] = {0};
#ifndef WIN32
sprintf(content, "cp -f *.txt %s\n ", FTP_FILE_PATH );
system(content);
ACE_OS::sleep( 1 );
sprintf(content, "rm -f *.txt\n ");
system(content);
#else
sprintf(content, "copy *.txt %s\n ", FTP_FILE_PATH );
system(content);
sprintf(content, "del *.txt\n " );
system(content);
#endif
return 0;
}
[解决办法]
Dev C++ 调试通过:
#include <stdio.h>
#include <dirent.h>
#include <string.h>
int main()
{
DIR *directory_pointer;
struct dirent *entry;
struct FileList
{
char filename[64];
struct FileList *next;
}start,*node;
char DIRcopyfrom[40], DIRcopyto[40], command[80];
printf( "Source directoy: ");
gets(DIRcopyfrom);
printf( "Destination: ");
gets(DIRcopyto);
if ((directory_pointer=opendir(DIRcopyfrom))==NULL)
printf( "Error opening %s\n ",DIRcopyfrom);
else
{
start.next=NULL;
node=&start;
while ((entry=readdir(directory_pointer))!=NULL)
{
node-> next=(struct FileList *)malloc(sizeof(struct FileList));
node=node-> next;
strcpy(node-> filename,entry-> d_name);
node-> next=NULL;
}
closedir(directory_pointer);
node=start.next;
while(node)
{
printf( "%s\n ",node-> filename);
sprintf(command, "copy %s\\%s %s\\%s ", DIRcopyfrom,node-> filename, DIRcopyto,node-> filename);
system(command);
node=node-> next;
}
}
system( "pause ");
return 0;
}
[解决办法]
思路就是 遍历文件夹,
将所有文件的文件名保存在一个 链表中。
然后 根据用户输入的两个目录,
构造拷贝命令:
sprintf(command, "copy %s\\%s %s\\%s ", DIRcopyfrom,node-> filename, DIRcopyto,node-> filename);
使用 system(command); 系统调用完成拷贝。
OK。
[解决办法]
晕,直接调用 shell 就行了,居然还有说用 xcopy 的.....
SHFileOperation
[解决办法]
那么把文件遍历部分修改为这个吧:
long handle;
struct _finddata_t filestruct;
char path_search[_MAX_PATH];
handle = _findfirst( "目录 ",&filestruct);
if((handle == -1)) return;
if( ::GetFileAttributes(filestruct.name)& FILE_ATTRIBUTE_DIRECTORY )
{
if( filestruct.name[0] != '. ' )
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
}
else
{
if( !stricmp(filestruct.name, szFilename) )
{
strcat(path_search, "\\ ");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
while(!(_findnext(handle,&filestruct)))
{
if( ::GetFileAttributes(filestruct.name) &FILE_ATTRIBUTE_DIRECTORY )
{
if(*filestruct.name != '. ')
{
_chdir(filestruct.name);
Search_Directory(szFilename);
_chdir( ".. ");
}
else
{
if(!stricmp(filestruct.name,szFilename))
{
_getcwd(path_search,_MAX_PATH);
strcat(path_search, "\\ ");
strcat(path_search,filestruct.name);
MessageBox(path_search);
}
}
}
_findclose(handle);
}
------解决方案--------------------
这种问题居然引来这么多讨论
我来归纳一下以上各种答案
:
1:使用copy
2:使用xcopy
3: 使用系统api遍历文件夹
4:使用shell api: SHFileOperation
这些根据需求不同都可以用啊