zoukankan      html  css  js  c++  java
  • VC++ 复制整个文件夹

    CopyFile只能复制一个文件,要想复制整个文件夹的所有文件,需要遍历文件夹里面的文件。

    MyFile.h

    #pragma once
    #include <string>
    #include <Windows.h>
    #include <iostream>
    #include <stdio.h>
    #include <io.h>
    #include <string>
    #include <Shlwapi.h>
    using namespace std;
    class CMyFile
    {
    public:
    	explicit CMyFile(void);
    	~CMyFile(void);
        static void MyCopyFile(string sourceFilePath,string destFilePath);
    };
    

     MyFile.cpp

    #include "StdAfx.h"
    #include "MyFile.h"
    LPCWSTR StringToLPCWSTR(string orig);
    void CopyFileRealize(string filePath,string destPath);
    void FindAllFile(string sourcePath,string destPath);
    CMyFile::CMyFile(void)
    {
    	/* void MyCopyFile(string sourceFilePath,string destFilePath);
    	FindAllFile(sourceFilePath,destFilePath);*/
    }
    CMyFile::~CMyFile(void)
    {
    }
    void CMyFile::MyCopyFile(string sourceFilePath,string destFilePath)
    {
    	FindAllFile(sourceFilePath,destFilePath);
    }
    void CopyFileRealize(string filePath,string destPath)
    {
    	string newFilePath=destPath+filePath.substr(2,filePath.length());	
    	int lastIndex=newFilePath.find_last_of("\\");
    	string destDir=newFilePath.substr(0,lastIndex);
    	string destFileName=newFilePath.substr(lastIndex);
    	LPCWSTR wFilePath=StringToLPCWSTR(filePath);
    	LPCWSTR wDestDir=StringToLPCWSTR(destDir);
    	LPCWSTR wDestFilePath=StringToLPCWSTR(destDir+destFileName);
    	if(_access(destDir.c_str(),0)==-1)
    	{
    		CreateDirectory(wDestDir,NULL);
    	}	
    	CopyFile(wFilePath,wDestFilePath,FALSE);   
    }
    void FindAllFile(string sourcePath,string destPath)
    {
    	
    	struct _finddata_t FileInfo;
    	string strFind=sourcePath+"\\*";	
    
    	long Handle=_findfirst(strFind.c_str(),&FileInfo);
    	if(Handle==-1L)
    	{
    		cerr << "can not match the folder path" << endl;        
    		exit(-1);
    	}
    
    	do{
    		//判断是否有子目录
    		if((FileInfo.attrib&_A_SUBDIR)==_A_SUBDIR)
    		{
    			if((strcmp(FileInfo.name,".")!=0)&&(strcmp(FileInfo.name,"..")!=0))
    			{				
    				string newPath=sourcePath+"\\"+FileInfo.name;				
    				FindAllFile(newPath,destPath);
    			}
    
    		}
    		else
    		{
    			string filePath=sourcePath+"\\"+FileInfo.name;
    			CopyFileRealize(filePath,destPath);	
    			
    		}
    
    	}while(_findnext(Handle,&FileInfo)==0);
    	_findclose(Handle);
    	// fout.close();
    }
    LPCWSTR StringToLPCWSTR(string orig)
    {
    	size_t origsize = orig.length() + 1;
    	const size_t newsize = 100;
    	size_t convertedChars = 0;
    	wchar_t *wcstring = (wchar_t *)malloc(sizeof(wchar_t)*(orig.length()-1));
    	mbstowcs_s(&convertedChars, wcstring, origsize, orig.c_str(), _TRUNCATE);
    	return wcstring;
    }
    

     使用

    CMyFile::MyCopyFile("E:\\Test1","E:\\T2");

  • 相关阅读:
    嵌入式Linux系统的构成和启动过程
    Linux 设备驱动之字符设备
    Linux的inode的理解
    flannel流程解析
    http2协议的理解
    多线程和单线程的理解
    User Token简单总结
    前端组件开发方法论
    Electron踩坑记录
    2020年工作总结
  • 原文地址:https://www.cnblogs.com/ike_li/p/2891892.html
Copyright © 2011-2022 走看看