c語言+linux系統調用匯聚總目錄下(含子目錄)的所有文件到一個目錄

1、使用方法
將main.c拷貝到總目錄下,編譯成a.out,運行a.out即可在總目錄下的dstPath文件夾下匯聚總目錄下(含子目錄)的所有文件(. .. main.c a.out dstPath已排除)。
2、匯聚可以修改代碼來決定是mv還是cp。
3、文件名有特殊字符的情況,有些字符代碼已經轉義,如果還有其他的轉義即可。

main.c:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <unistd.h>

void geditFileName(char *name, int n)
{
    char newName[1000];
    char* ptrNewName = newName;
    char* ptrOldName = name;

    if(n < 1 || name == NULL)
    {
        return ;
    }

    memset(newName,'\0',sizeof(newName));
    while((*ptrNewName = *ptrOldName) != '\0')
    {
        if(*ptrNewName == ' ')
        {
            *ptrNewName = '\\';
            ptrNewName++;
            *ptrNewName = ' ';
            n++;
        }
        if(*ptrNewName == '=')
        {
            *ptrNewName = '\\';
            ptrNewName++;
            *ptrNewName = '=';
            n++;
        }
        if(*ptrNewName == '&')
        {
            *ptrNewName = '\\';
            ptrNewName++;
            *ptrNewName = '&';
            n++;
        }
        if(*ptrNewName == '(')
        {
            *ptrNewName = '\\';
            ptrNewName++;
            *ptrNewName = '(';
            n++;
        }
        if(*ptrNewName == ')')
        {
            *ptrNewName = '\\';
            ptrNewName++;
            *ptrNewName = ')';
            n++;
        }
        if(*ptrNewName == '+')
        {
            *ptrNewName = '\\';
            ptrNewName++;
            *ptrNewName = '+';
            n++;
        }
        ptrNewName++;
        ptrOldName++;
    }
    strncpy(name, newName, n);
}

int readFileList(char *basePath, char *dstPath)
{
    DIR *dir;
    struct dirent *ptr;
    char base[1000];
    char tmp[1000];
    char ucFile[1000];
    char cmd[1000];
    static unsigned int number = 0;

    //打開目錄
    if ((dir=opendir(basePath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);    
    }

    //讀取目錄下的文件
    while ((ptr=readdir(dir)) != NULL)
    {
        if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0)    ///current dir OR parrent dir
            continue;
        if(strcmp(ptr->d_name, "main.c") == 0 || strcmp(ptr->d_name, "a.out") == 0 || strcmp(ptr->d_name, "dstPath") == 0)
            continue;
        else if(ptr->d_type == 8)    ///file
        {
            memset(ucFile,'\0',sizeof(ucFile));
            memset(tmp,'\0',sizeof(tmp));
            strcpy(ucFile,basePath);
            strcat(ucFile,"/");
            strcat(ucFile,ptr->d_name);
            strcpy(tmp,dstPath);
            strcat(tmp,ptr->d_name);
            if( (access( tmp, 0 )) != -1 )
            {
                memset(tmp,'\0',sizeof(tmp));
                sprintf(tmp, "%s%d_rename_%s", dstPath, number++,ptr->d_name);
                if( (access( tmp, 0 )) != -1 )
                {
                    exit(1);
                }
            }

            //system 修正文件名
            geditFileName(ucFile,900);
            geditFileName(tmp,900);
            sprintf(cmd, "cp -i %s %s", ucFile, tmp);
            printf("cmd:%s\n", cmd);
            system(cmd);
        }
        else if(ptr->d_type == 10)    ///link file
        {
            memset(ucFile,'\0',sizeof(ucFile));
            memset(tmp,'\0',sizeof(tmp));
            strcpy(ucFile,basePath);
            strcat(ucFile,"/");
            strcat(ucFile,ptr->d_name);
            strcpy(tmp,dstPath);
            strcat(tmp,ptr->d_name);
            if( (access( tmp, 0 )) != -1 )
            {
                memset(tmp,'\0',sizeof(tmp));
                sprintf(tmp, "%s%d_rename_%s", dstPath, number++,ptr->d_name);
                if( (access( tmp, 0 )) != -1 )
                {
                    exit(1);
                }
            }

            //system 修正文件名
            geditFileName(ucFile,900);
            geditFileName(tmp,900);
            sprintf(cmd, "mv -i %s %s", ucFile, tmp);
            printf("cmd:%s\n", cmd);
            system(cmd);
        }
        else if(ptr->d_type == 4)    ///dir
        {
            //遞歸進入子目錄執行拷貝
            memset(base,'\0',sizeof(base));
            strcpy(base,basePath);
            strcat(base,"/");
            strcat(base,ptr->d_name);
            readFileList(base, dstPath);
        }
    }
    closedir(dir);
    return 1;
}

//拷貝當前目錄下所有的子文件到一個目錄中
int main(int argc, char *argv[])
{
    char basePath[1000];
    char dstPath[1000];

    ///get the current absoulte path
    memset(basePath,'\0',sizeof(basePath));
    getcwd(basePath, 900);
    //printf("the current dir is : %s\n",basePath);

    //set the dstPath
    system("mkdir dstPath -p");
    memset(dstPath,'\0',sizeof(dstPath));
    strcpy(dstPath,basePath);
    strcat(dstPath,"/dstPath/");
    //printf("the dst dir is : %s\n",dstPath);

    ///get the file list
    readFileList(basePath, dstPath);
    return 0;
}
發佈了50 篇原創文章 · 獲贊 58 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章