MyFile 類 c++

來源:互聯網
上載者:User

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);    static void MyDeleteDirectory(string fileDir);};

MyFile.cpp

#include "StdAfx.h"#include "MyFile.h"LPCWSTR StringToLPCWSTR(string orig);void CopyFileRealize(string filePath,string destPath,string sourcePathT);void FindAllFile(string sourcePath,string destPath,string sourcePathT);bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin);CMyFile::CMyFile(void){    }CMyFile::~CMyFile(void){}/////sourceFilePath 要複製的檔案//destFilePaht 目標檔案///void CMyFile::MyCopyFile(string sourceFilePath,string destFilePath){    FindAllFile(sourceFilePath,destFilePath,sourceFilePath);}void CMyFile::MyDeleteDirectory(string fileDir){    LPCWSTR wFilePath=StringToLPCWSTR(fileDir);    DeleteDirectory(wFilePath,true);}void CopyFileRealize(string filePath,string destPath,string sourcePathT){    string newFilePath=filePath;        int replaceLength=sourcePathT.find_last_of("\\");    newFilePath.replace(0,replaceLength,destPath);        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);      }bool DeleteDirectory(LPCTSTR lpszDir, bool noRecycleBin = true){    int len = _tcslen(lpszDir);    TCHAR *pszFrom = new TCHAR[len+2];    _tcscpy(pszFrom, lpszDir);    pszFrom[len] = 0;    pszFrom[len+1] = 0;    SHFILEOPSTRUCT fileop;    fileop.hwnd   = NULL;    // no status display    fileop.wFunc  = FO_DELETE;  // delete operation    fileop.pFrom  = pszFrom;  // source file name as double null terminated string    fileop.pTo    = NULL;    // no destination needed    fileop.fFlags = FOF_NOCONFIRMATION|FOF_SILENT;  // do not prompt the user    if(!noRecycleBin)        fileop.fFlags |= FOF_ALLOWUNDO;    fileop.fAnyOperationsAborted = FALSE;    fileop.lpszProgressTitle     = NULL;    fileop.hNameMappings         = NULL;    int ret = SHFileOperation(&fileop);    delete [] pszFrom;      return (ret == 0);}void FindAllFile(string sourcePath,string destPath,string sourcePathT){        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,sourcePathT);            }                    }        else        {            string filePath=sourcePath+"\\"+FileInfo.name;            CopyFileRealize(filePath,destPath,sourcePathT);                        }    }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::MyDeleteDirectory("C:\\Messer_2.0changshi\\db");
CMyFile::MyCopyFile("C:\\Program Files\\MDB\\db","C:\\Messer_2.0changshi");

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.