114.遍历文件夹并批量修改文件名

  • 遍历文件需要使用的头文件
    #define _CRT_SECURE_NO_WARNINGS
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <io.h>
  • 遍历
    1 //文件结构体用于遍历文件
    2     struct _finddata_t files;
    3     //打开文件句柄判断是否打开成功
    4     int File_Handle;
    5     //用于遍历的文件夹
    6     File_Handle = _findfirst("F:\1001\20150330C语言\video\*", &files);
  • 1 //遍历当前文件夹
    2     do
    3     {
    4         .....
    5     } while (0 == _findnext(File_Handle, &files));
  • 修改文件名
    1 rename(path,new_name);

完整代码:

 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 #include <string.h>
 5 #include <io.h>
 6 
 7 void main()
 8 {
 9     //文件结构体用于遍历文件
10     struct _finddata_t files;
11     //打开文件句柄判断是否打开成功
12     int File_Handle;
13     //用于遍历的文件夹
14     File_Handle = _findfirst("F:\1001\20150330C语言\video\*", &files);
15     if (-1 == File_Handle)
16     {
17         printf("遍历文件出错
");
18         return;
19     }
20     //要修改文件的名字的路径
21     char path[100] = { 0 };
22     //修改后的名字
23     char new_name[100] = { 0 };
24     //遍历当前文件夹
25     do
26     {
27         //格式化处理
28         sprintf(path, "F:\1001\20150330C语言\video\%s", files.name);
29         //在文件名前加信息
30         sprintf(new_name, "F:\1001\20150330C语言\video\%s%s","第三天", files.name);
31         //显示到屏幕
32         //printf("%s 
", new_name);
33         //重命名
34         rename(path,new_name);
35     } while (0 == _findnext(File_Handle, &files));
36     //关闭文件句柄
37     _findclose(File_Handle);
38     system("pause");
39 }
原文地址:https://www.cnblogs.com/xiaochi/p/8525017.html