c/c++ 按照自定义脚本递归处理所有文件

今天突然说项目要申请软助, 然后要把准备几千行代码出来. 想着一行一行拷贝很麻烦, 不过专门写一个把文件合并的程序又很鸡肋, 所以就写了一个用来递归处理文件的程序.

recur

效果如上图: 参数1 是递归遍历的目录, 参数2 是执行的命令(%s 每一个单独的文件名), 参数3 是过滤的文件类型
然后整理几千行代码就可以简单的用:

recur.exe E:workspace "type %s" "c,cpp" > code.txt

如果还有其他特别的需求要处理多个文件, 就可以专心写处理一个文件的脚本或者程序然后调用. (程序 > code.txt 就是把程序的输出导入到 code.txt 文件中)

recur.exe E:workspace "你的程序.exe %s" "类型1,类型2"

来一次处理多个.

实现代码

#include <stdio.h>
#include <windows.h>

void help();

int checkSuffix(char *name, char *suffix)
{
    char *p = suffix;
    char *ext = strrchr(name, '.');
    int len, end;

    while (*p)
    {
        end = *(p + 1) == '\0';
        if (*p == ',' || end)
        {
            len = p - suffix;

            if (end)
                len++;

            if (strncmp(ext + 1, suffix, len) == 0)
                return 1;

            suffix = p + 1;
        }
        p++;
    }

    return 0;
}

int isDirectory(WIN32_FIND_DATA file)
{
    return file.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
}

char *getFilepath(char *dest, char *dir, char *name)
{
    strcpy(dest, dir);
    strcat(dest, "\");
    strcat(dest, name);
    return dest;
}

void recur(char *src, char *command, char *suffix)
{
    char *name;
    char path[MAX_PATH];
    char dir[MAX_PATH];
    char buf[MAX_PATH];
    WIN32_FIND_DATA file;
    HANDLE hDir;

    getFilepath(dir, src, "*.*");
    hDir = FindFirstFile(dir, &file);

    if (hDir == INVALID_HANDLE_VALUE)
        return;

    while (FindNextFile(hDir, &file))
    {
        name = file.cFileName;
        if (isDirectory(file))
        {
            if (name[0] != '.')
            {
                getFilepath(path, src, name);
                recur(path, command, suffix);
            }
        }
        else if (checkSuffix(name, suffix))
        {
            getFilepath(path, src, name);
            wsprintf(buf, command, path);
            system(buf);
        }
    }
}

int main(int argc, char *argv[])
{
    if (argc != 4)
    {
        help();
        return 0;
    }

    recur(argv[1], argv[2], argv[3]);

    printf("nRecur overn");
    return 0;
}

void help()
{
    printf("Usage:  recur <dir> <cmd> <suffix>n" 
           "        recur E:\test "echo %%s" c,cpp n"
           "   dir  Directorys that you deal with.n" 
           "        It can be like E:\test E:\test2 n" 
           "   cmd  command you want run for example:n" 
           "        1) type %%s n"
           "        2) echo %%s n"
           "        3) your.bat %%s n"
           " suffix filter suffix, for example:n"
           "        c,cpp n"
           );
}
Advertisements

发表评论

Fill in your details below or click an icon to log in:

WordPress.com 徽标

You are commenting using your WordPress.com account. Log Out /  更改 )

Google+ photo

You are commenting using your Google+ account. Log Out /  更改 )

Twitter picture

You are commenting using your Twitter account. Log Out /  更改 )

Facebook photo

You are commenting using your Facebook account. Log Out /  更改 )

Connecting to %s