大项目时时常需要抽出属于自己编写的那部分代码,从SVN下载后,往往需要一个个的进入文件夹下拿取相应的文件。这样很浪费时间,虽然使用bat编写确实更快,但是我觉得使用C语言可能在文件过多时会快一点,也是为了 练习练习。如果那里存在问题,或是不足,欢迎指出。代码如下:
#include<stdio.h>
#include<io.h>
#include<string.h>
#include<direct.h>
#include<errno.h>#include<stdio.h> #include<io.h> #include<string.h> #include<direct.h> #include<errno.h> /* 1.文件批处理的快速操作, 2.当前电脑中不存在的功能 3.实用性较高 4.速度较快 5.需求功能 5.1 大文件夹下搜索指定文件名文件 复制到指定对应文件路径下 部分文件指定复制 是否覆盖大 5.2 文件夹下搜索指定文件名文件 复制到某个文件夹下 模糊抽出功能 */ /* 字符串匹配算法 */ int Inquire_filaname(char *src, char *dest) { int i = 0, j = 0; int srclen = 0, destlen = 0; srclen = strlen(src); destlen = strlen(dest); if (srclen < destlen) { return -1; } while (i < srclen && j < destlen) { if (src[i] == dest[j]) { i++; j++; } else { i = i - j + 1; j = 0; } } if (j >= destlen) { return i - j; } else { return -1; } } /* 将Inpath路径的文件,复制到Outpath的路径中 */ int WriteFile(char *Inpath, char *Outpath) { FILE *Infile=NULL ; FILE *Outfile =NULL; char str[1024] ; if (Inpath != NULL && Outpath != NULL) { Infile = fopen(Inpath, "rb"); Outfile = fopen(Outpath, "wb"); if (Infile != NULL) { if (Outfile != NULL) { //printf("%c", getc(Infile)); while (fread(str, sizeof(str),1 , Infile) != NULL) { fwrite(str, sizeof(str),1, Outfile); } } fclose(Outfile); fclose(Infile); Infile = NULL; Outfile = NULL; return 2; } } return 0; } /* 指定搜索目录 指定目的目录 指定搜索文件名 指定搜索类型 */ int File_out(char *SrcPath, char *filename,char *DestPath, char *type) { int a = atoi(type); if (SrcPath != NULL && DestPath != NULL) // 如果指定大 文件目录存在 { _mkdir(DestPath); // type ==1 5.1 type == 2 5.2 FileSearch(SrcPath, filename, DestPath, a); } else { printf("请输入输入路径或输入路径"); return 0; } } int FileSearch(char *dir, char *filename, char *copypath, int type)//递归遍历当前目录下的所有文件 { char dirNew[200]; int Isnull = 0; //文件夹中是否有符合的文件,0为没有,1为有 char path_A[200]; //保存文件路径 char path_B[200]; //保存文件夹路径 strcpy(dirNew, dir); strcat(dirNew, "\\*.*"); // 修改此处改变搜索条件 struct _finddata_t filefind; int done = 0, i, handle; if ((handle = _findfirst(dirNew, &filefind)) != -1) { while (!(done = _findnext(handle, &filefind))) { strcpy(path_A, dir); strcpy(path_B, copypath); if (strcmp(filefind.name, "..") == 0) continue; printf("当前工作目录 is \"%s\"\n", dir); printf("当前目的目录 is \"%s\"\n", copypath); if ((_A_SUBDIR == filefind.attrib)) // 是目录 { //printf("当前工作目录 is \"%s\"\n", dirNew); //截取最后路径/ if (type == 1) { strcat(path_B, "\\"); strcat(path_B, filefind.name); _mkdir(path_B); } strcat(path_A, "\\"); strcat(path_A, filefind.name); if (1 == FileSearch(path_A, filename, path_B, type))// 递归遍历子目录 { memset(path_A, '\0', sizeof(path_A)); memset(path_B, '\0', sizeof(path_B)); strcpy(path_A, dir); strcpy(path_B, copypath); } } else { if (-1 != Inquire_filaname(filefind.name, filename)) { //printf("[File]:\t%s\n", filefind.name); //如果是5.1,则在之前创建的文件夹下,复制文件 if (type == 1) { strcat(path_B, "\\"); strcat(path_B, filefind.name); //copypath = path_B; //printf("[copypath]:\t%s\n", copypath); } else { //如果是5.2,则直接在总路径下复制路径文件, strcat(path_B, "\\"); strcat(path_B, filefind.name); //copypath = path_B; } strcat(path_A, "\\"); strcat(path_A, filefind.name); //(copypath, filefind.name); printf("[path_A]:\t%s\n", path_A); printf("[path_B]:\t%s\n", path_B); if (WriteFile(path_A, path_B) != 0) { Isnull = 1; } } } } _findclose(handle); //如果出现空文件夹或不是2类型,则删除空文件夹 if (Isnull == 0 && 1 == type) { _rmdir(copypath); } return 1; } return 0; } int main(int argc, char **argv) { File_out(dirname, dirname1, filename, argv[4]); return 0; }