C语言遍历文件夹并批量更改文件名
本文最后更新于863 天前,其中的信息可能已经过时,如有错误请发送邮件到1739584917@qq.com
#include <iostream>
#include <cstring>
#include <io.h>
#include<stdio.h> 
#include<string>
using namespace std;

void listFiles(const char* dir);
int main()
{
	char dir[200] = "D:\\data";//文件夹的目录
	listFiles(dir);
	return 0;
}
void listFiles(const char* dir)
{
	char dirNew[200];
	strcpy(dirNew, dir);
	strcat(dirNew, "\\*.*");    // 在目录后面加上"\\*.*"进行第一次搜索
	intptr_t handle;
	_finddata_t findData;
	handle = _findfirst(dirNew, &findData);
	if (handle == -1)        // 检查是否成功
		return;
	do
	{
		if (findData.attrib & _A_SUBDIR)//如果是子目录(文件夹)
		{
			if (strcmp(findData.name, ".") == 0 || strcmp(findData.name, "..") == 0)//"."(当前目录),".."(上一层目录)
				continue;
			//如果要遍历包含子目录在内下的所有的文件,则要继续遍历子目录:
			//strcpy(dirNew, dir);
			//strcat(dirNew, "\\");// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
			//strcat(dirNew, findData.name);// 拼接:当前文件夹目录\文件名
			//listFiles(dirNew);
		}
		else
		{
			char oldname[100], newname[100];
			strcpy(oldname, dir);
			strcat(oldname, "\\");
			strcpy(newname, oldname);
			strcat(oldname, findData.name);
			strcat(newname, "newname");//newname改为文件的新名字
			if (rename(oldname, newname) == 0)
				cout << "修改成功\n";
			else
				perror("rename");
		}
	} while (_findnext(handle, &findData) == 0);
	_findclose(handle);    // 关闭搜索句柄
}

findfirst 和 _findnext 查找文件原理

一、这两个函数均在io.h里面。

二、首先了解一下文件结构体:
struct _finddata_t {
unsigned attrib;
time_t time_create;
time_t time_access;
time_t time_write;
_fsize_t size;
char name[260];
};

attrib,就是所查找文件的属性包括:

_A_NORMAL 0x00 // Normal file - No read/write restrictions(普通文件,没有只读/写权限)
_A_RDONLY 0x01 // Read only file(只读文件)
_A_HIDDEN 0x02 // Hidden file(隐藏文件)
_A_SYSTEM 0x04 // System file(系统文件)
_A_SUBDIR 0x10 // Subdirectory(子目录)
_A_ARCH   0x20 // Archive file(存档文件)

time_create、time_access和time_write分别是创建文件的时间、最后一次访问文件的时间和文件最后被修改的时间。

size:文件大小

name:文件名

_findfirst函数

_findfirst函数:long _findfirst(const char *, struct _finddata_t *);

第一个参数为文件名(带文件地址),可以用”.“来查找所有文件,也可以用”*.cpp”来查找.cpp文件。第二个参数是_finddata_t结构体指针。若查找成功,返回文件句柄,若失败,返回-1。

_findnext函数

_findnext函数:int _findnext(handle, struct _finddata_t *);

第一个参数为文件句柄,第二个参数同样为_finddata_t结构体指针。若查找成功,返回0,失败返回-1。

_findclose()函数

_findclose()函数:int _findclose(handle);

只有一个参数,文件句柄。若关闭成功返回0,失败返回-1。

rename()函数

rename()函数,功能是给一个文件重命名

rename() 函数的声明:

int rename(const char *old_filename, const char *new_filename)

old_filename // 这是 C 字符串,包含了要被重命名/移动的文件名称。
new_filename // 这是 C 字符串,包含了文件的新名称。

如果要修改的文件不在项目的目录下,则

old_filename 与 new_filename 要带上完整的路径信息,如 C:\data\test.txt

rename("C:\\data\\test.txt","C:\\test1.txt")

将C:\data\下的test.txt移动到C:\下,并且将文件名改为test1.txt

如果觉得本文对您有所帮助,可以支持下博主,一分也是缘
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇