zoukankan      html  css  js  c++  java
  • MyFile 类 vc++

    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");

  • 相关阅读:
    面向对象设计大作业第二阶段:图书馆系统
    OO之接口-DAO模式代码阅读及应用
    OO设计-有理数类的设计
    DS博客作业05--查找
    DS博客作业04--图
    DS博客作业03--树
    DS博客作业02--栈和队列
    DS01——线性表
    c博客06-结构体&文件
    C语言博客作业05——指针
  • 原文地址:https://www.cnblogs.com/ike_li/p/3030781.html
Copyright © 2011-2022 走看看