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

  • 相关阅读:
    SQL Server, Timeout expired.all pooled connections were in use and max pool size was reached
    javascript 事件调用顺序
    Best Practices for Speeding Up Your Web Site
    C语言程序设计 使用VC6绿色版
    破解SQL Prompt 3.9的几步操作
    Master page Path (MasterPage 路径)
    几个小型数据库的比较
    CSS+DIV 完美实现垂直居中的方法
    由Response.Redirect引发的"Thread was being aborted. "异常的处理方法
    Adsutil.vbs 在脚本攻击中的妙用
  • 原文地址:https://www.cnblogs.com/ike_li/p/3030781.html
Copyright © 2011-2022 走看看