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;
}

猜你喜欢

转载自blog.csdn.net/qq_39660930/article/details/78110223