现在的位置: 首页 > 综合 > 正文

win_api实现复制目录,删除非空目录

2018年09月17日 ⁄ 综合 ⁄ 共 3979字 ⁄ 字号 评论关闭
这是做软件自动更新的时候写的一些功能函数,虽然一些强大的库都已经实现了这些方法,但是自己写的还是存个档,以备不时只需。
主要是实现了windows下的copy目录,删除非空目录功能。递归遍历目录的方法是从网上copy的,实在是不想自己一点一点翻看msdn如何判断是目录还是文件,删除文件,空文件夹的函数也是网上搜到,根本不知道是win_api,还是C库函数。反正,将文件删除了就是了。
删除非空目录的思路是:将目录下的文件统统删除。然后再进到目录的最底层,由内往外逐层删除目录。
自动更新的思路是:首先是做一个patch放到 server上patch里包括你要更新的内容和一个辅助程序exe Ass.exe主程序下载这个包到临时目录解压,之后运行这个辅助程序Ass.exe 之后自己退出,或者辅助程序结束主进程之后辅助程序拷贝 新的主程序覆盖老的, 再运行主程序退出自己。(3Q leon的自动更新思路)
上代码。
/*
*原本想用C#写的。后来想了想,这么easy的程序,用C#太屈才了。还是C/C++比较好些,反正是后台运行,又不抛头露面,应该说出人头地更合适。自己写些递归,还是比较happy的,好久没有自己写过一些底层的东东了。
*/
#include <iostream>
#include <windows.h>
#include <string>
#include <io.h>
#include <direct.h>
using namespace std;
void charTowchar(const char *chr, wchar_t *wchar, int size)  

{
    MultiByteToWideChar( CP_ACP, 0, chr,strlen(chr)+1, wchar, size/sizeof(wchar[0]) );  

}
void zs_CopyDir(string path,string destDir)
{
    struct _finddata_t   filefind;
    string  curr=path+"\\*.*";
    int   done=0,i,handle;
    if((handle=_findfirst(curr.c_str(),&filefind))==-1)
        return;
    while(!(done=_findnext(handle,&filefind)))
    {
        if(!strcmp(filefind.name,"..")){
            continue;
        }
        if((_A_SUBDIR==filefind.attrib)) //是目录
        {
            string totalDir = path+"
\\"+filefind.name;
            string relativePath = totalDir.substr(path.length(),totalDir.length());
            string currentDir = destDir+relativePath;
            if( _chdir(currentDir.c_str()) == -1 )
                mkdir( currentDir.c_str());
            zs_CopyDir(totalDir,currentDir);
           
        }
        else//不是目录,是文件
        {
            string totalDir = path+"
\\"+filefind.name;         

            string relativePath = totalDir.substr(path.length(),totalDir.length());
            string destfilepath = destDir+relativePath;
            wchar_t* wc_totalpath = new wchar_t[1024];
            memset(wc_totalpath,0,1024);
            charTowchar(totalDir.c_str(),wc_totalpath,(totalDir.length()+1)*2);
            wchar_t* wc_destfilepath = new wchar_t[1024];
            memset(wc_destfilepath,0,1024);
            charTowchar(destfilepath.c_str(),wc_destfilepath,(destfilepath.length()+1)*2);
            if(totalDir.find("zsMainUpdate.exe")==-1)
            {
                CopyFile(wc_totalpath,wc_destfilepath,FALSE);
            }
        }
    }
    _findclose(handle);
}
void zs_RremoveFileInDirctory(string path)
{
    struct _finddata_t   filefind;
    string  curr=path+"\\*.*";
    int   done=0,i,handle;
    if((handle=_findfirst(curr.c_str(),&filefind))==-1)
        return;
    while(!(done=_findnext(handle,&filefind)))
    {
        if(!strcmp(filefind.name,"..")){
            continue;
        }
        if((_A_SUBDIR==filefind.attrib)) //是目录
        {
            string totalDir = path+"
\\"+filefind.name;
            zs_RremoveFileInDirctory(totalDir);
        }
        else//不是目录,是文件
        {
            string totalDir = path+"
\\"+filefind.name;         

            remove(totalDir.c_str());
        }
    }
    _findclose(handle);
}
void zs_RremoveEmptyDirctory(string path)
{
    struct _finddata_t   filefind;
    string  curr=path+"\\*.*";
    int   done=0,i,handle;
    if((handle=_findfirst(curr.c_str(),&filefind))==-1)
        return;
    while(!(done=_findnext(handle,&filefind)))
    {
        if(!strcmp(filefind.name,"..")){
            continue;
        }
        if((_A_SUBDIR==filefind.attrib)) //是目录
        {
            string totalDir = path+"
\\"+filefind.name;
            zs_RremoveEmptyDirctory(totalDir);
            if(totalDir!="")
            {
                _rmdir(totalDir.c_str());
            }
        }
    }
    _findclose(handle);
    _rmdir(path.c_str());//删除根目录
}
void zs_RremoveDirctory(string path)
{
    zs_RremoveFileInDirctory(path);
    zs_RremoveEmptyDirctory(path);
}
int main(int argc,char** argv)
{
    //srcDir是新zip包的解压目录,destDir是当前手机精灵的目录。
    //char* strurl = argv[1];
    string strArgvTotal(argv[1]);
    string srcDir,destDir;
    int index = strArgvTotal.find(',');
    if( index!=-1 )
    {
        srcDir = strArgvTotal.substr(0,index);
        destDir = strArgvTotal.substr(index+1,strArgvTotal.length()-index);
        zs_CopyDir(srcDir,destDir);
        //删除zip包临时目录得交给zsmain主程序
        //zs_RremoveDirctory(srcDir);
    }
    else
    {  
        return -1;
    }
    return 0;
}

抱歉!评论已关闭.