baidu_38411250 2018-10-16 09:18 采纳率: 33.3%
浏览 1945

怎么将一个文件拷贝到指定目录下的所有子文件夹中

假设有一个文件a.html,需要将它发送到a目录下面的子文件夹下,a目录文件名不变,子文件夹名字每次开机会改变名字,小白一个,希望给个完整源码:能将a.html发送到指定目录下的所有子文件夹中(不是MFC的,是控制台的)

  • 写回答

1条回答

  • threenewbee 2018-10-17 03:20
    关注
    // Q702781.cpp : Defines the entry point for the console application.
    //
    
    #include "stdafx.h"
    
    //参考了 https://blog.csdn.net/liigo/article/details/4548577
    
    #include "windows.h"
    #include "stdio.h"
    
    typedef BOOL (WINAPI *EnumerateFunc) (LPCSTR lpFileOrPath, void* pUserData);
    
    void doFileEnumeration(LPSTR lpPath, BOOL bRecursion, BOOL bEnumFiles, EnumerateFunc pFunc, void* pUserData)
    {
    
        static BOOL s_bUserBreak = FALSE;
        try{
            //-------------------------------------------------------------------------
            if(s_bUserBreak) return;
    
            int len = strlen(lpPath);
            if(lpPath==NULL || len<=0) return;
    
            //NotifySys(NRS_DO_EVENTS, 0,0);
    
            char path[MAX_PATH];
            strcpy(path, lpPath);
            if(lpPath[len-1] != '//') strcat(path, "//");
            strcat(path, "*");
    
            WIN32_FIND_DATA fd;
            HANDLE hFindFile = FindFirstFile(path, &fd);
            if(hFindFile == INVALID_HANDLE_VALUE)
            {
                ::FindClose(hFindFile); return;
            }
    
            char tempPath[MAX_PATH]; BOOL bUserReturn=TRUE; BOOL bIsDirectory;
    
            BOOL bFinish = FALSE;
            while(!bFinish)
            {
                strcpy(tempPath, lpPath);
                if(lpPath[len-1] != '//') strcat(tempPath, "//");
                strcat(tempPath, fd.cFileName);
    
                bIsDirectory = ((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0);
    
                if( bIsDirectory
                    && (strcmp(fd.cFileName, ".")==0 || strcmp(fd.cFileName, "..")==0)) 
                {       
                    bFinish = (FindNextFile(hFindFile, &fd) == FALSE);
                    continue;
                }
    
                if(pFunc && bEnumFiles!=bIsDirectory)
                {
                    bUserReturn = TRUE; // pFunc(tempPath, pUserData);
                    if(bUserReturn==FALSE)
                    {
                        s_bUserBreak = TRUE; ::FindClose(hFindFile); return;
                    }
                }
    
                //NotifySys(NRS_DO_EVENTS, 0,0);
    
                if(bIsDirectory && bRecursion) 
                {
                    pFunc(tempPath, pUserData);
                    doFileEnumeration(tempPath, bRecursion, bEnumFiles, pFunc, pUserData);
                }
    
                bFinish = (FindNextFile(hFindFile, &fd) == FALSE);
            }
    
            ::FindClose(hFindFile);
    
            //-------------------------------------------------------------------------
        }catch(...){ return; }
    }
    
    char dirtocopy[] = "C:\\填写你的目录";
    char filetocopy[] = "C:\\填写你的文件带路径\\a.html";
    char filename[] = "\\填写你的文件名不带路径.html";
    
    BOOL WINAPI myEnumerateFunc(LPCSTR lpFileOrPath, void* pUserData)
    {
        //char* pdot;
        //if((pdot = strrchr(lpFileOrPath, '.')) && stricmp(pdot, ".mp3") == 0)
        //{
        //  printf("%s\n", lpFileOrPath);
        //}
        printf("%s\n", lpFileOrPath);
        char filenametocopy[400];
        strcpy(filenametocopy, lpFileOrPath);
        strcat(filenametocopy, filename);
        CopyFile(filetocopy, filenametocopy, TRUE);
        return TRUE;
    }
    
    int main(int argc, char* argv[])
    {
        doFileEnumeration(dirtocopy, TRUE, TRUE, myEnumerateFunc, NULL);
        return 0;
    }
    
    
    

    完整代码:https://download.csdn.net/download/caozhy/10726127

    评论

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作