C++下 文件夹内文件名提取、修改以及批量生成文件名

来源:互联网 发布:毒舌电影 知乎 编辑:程序博客网 时间:2024/04/29 18:24

因为做一个项目,涉及到这一块,然后在网上搜到额相关文章,为方便以后使用,所以修改后放到博客里做保存,首先是文件夹内文件的提取。

#undef UNICODE // 如果你不知道什么意思,请不要修改#define MAX_RESULT 1000#include <stdio.h>#include <stdlib.h>#include <Windows.h>char** EnumFiles(const char *directory, int *count){WIN32_FIND_DATA FindFileData;HANDLE hFind;char result[MAX_RESULT][MAX_PATH];char **returnresult;char pattern[MAX_PATH];int i = 0, j;// 开始查找strcpy(pattern, directory);strcat(pattern, "\\*.*");hFind = FindFirstFile(pattern, &FindFileData);if (hFind == INVALID_HANDLE_VALUE) {*count = 0;return NULL;} else {do{strcpy(result[i++], FindFileData.cFileName);}while (FindNextFile(hFind, &FindFileData) != 0);}// 查找结束FindClose(hFind);// 复制到结果中returnresult = (char **)calloc(i, sizeof(char *));for (j = 0; j < i; j++){returnresult[j] = (char *)calloc(MAX_PATH, sizeof(char));strcpy(returnresult[j], result[j]);}*count = i;return returnresult;}void main(){int i, count;char ** result;char directory[MAX_PATH];printf("请输入要查询的文件夹:");scanf("%s", directory);result = EnumFiles(directory, &count);for (i = 0; i < count; i++)printf("%s\n", result[i]);}

下面是文件夹内文件名的修改,我做的处理是对文件夹内图片名称的修改。

#include <io.h>  #include <AtlBase.h> /*** find the postfix of the input mod * @param mod* @return postfix*/char *find_postfix(char *mod){    char *postfix = (char *)malloc(6*sizeof(char));    int len = strlen(mod);    int i,j,step;    for(j=4,i = len-1; mod[i]!='.'; i--,j--){        postfix[j] = mod[i];    }    step = 4-j;    for(i=0;i<6;i++,step--){        if(step<=0)            postfix[i]='\0';        else            postfix[i]=postfix[i+j+1];    }    return postfix;} int main(int argc,char* argv[]){    char* file_mod;    char* postfix;    int done,count=649;    _finddata64i32_t ff;// file handler    intptr_t hFile;    if(argc>1){        file_mod = argv[1];    }    postfix = find_postfix(file_mod);    hFile = _findfirst(file_mod,&ff);    if(hFile==-1){        printf("No file find in this postfix:%s\n",postfix);        return 0;    }         do{        count++;        char new_name[36]={0};        char new_num[5];        // renamed all file to this mod, "1.postfix" "2.postfix" ... "n.postfix"        //strcat(new_name,itoa(count,new_num,10));sprintf(new_name,"frame_%04d",count);printf("%s\n",new_name);        strcat(new_name,".");        strcat(new_name,postfix);        printf("%s\t%s\n",ff.name,new_name);        // rename old name to new name        rename(ff.name,new_name);    }while(!_findnext(hFile,&ff));// find next file match file_mod    // close search handle    _findclose(hFile);    return 0;}

对第二个程序,作一些说明:首先是调试运行生成.exe文件(在debug目录下)然后将该.exe放到与图片相同的文件夹内,打开cmd窗口,切换到该文件夹的目录下,输入(比如该.exe的名字是Eigen_learning.exe):Eigen_learning *.jpg。



接下来的的是在某一个文件夹中批量生成文件名,该程序可以递归地生成文件目录


#include <stdio.h>#include <stdlib.h>#include <string.h>#include<direct.h>int mkdir_r(const char *path) {        if (path == NULL) {                return -1;        }        char *temp = strdup(path);        char *pos = temp;        /* 去掉开头的 './' 或 '/' */        if (strncmp(temp, "/", 1) == 0) {                pos += 1;        } else if (strncmp(temp, "./", 2) == 0) {                pos += 2;        }        /* 循环创建目录 */        for ( ; *pos != '\0'; ++ pos) {                if (*pos == '/') {                        *pos = '\0';                        mkdir(temp);                        printf("for %s\n", temp);                        *pos = '/';                }        }        /* 如果最后一级目录不是以'/'结尾,        遇到'\0'就中止循环,        不会创建最后一级目录 */        if (*(pos - 1) != '/') {                printf("if %s\n", temp);                mkdir(temp);        }        free(temp);        return 0;}int main() {char name[20]="train1_";char str[10];char temp1[30];char prefix[20]="./PassEveryImg/";char temp2[30];for(int i=1;i<=47;i++){strcpy(temp1,name);strcpy(temp2,prefix);sprintf(str,"%02d",i);strcat(temp1,str);strcat(temp2,temp1);    mkdir_r(temp2);}        return 0;}


0 0
原创粉丝点击