C语言 自动修改文件名小程序

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>

int protectstr(const char *name,char *newname,int len)
{
    int errmsg = 0;
    /*先找到数字5或者6,其次判断相邻的3个字符是否都是数字,取出数字*/
    int i = 0;
    /*strchr()*/
    char * temp = (char *)name;
    /*集数*/
    char temp2[10] = { 0 };
    /*文件格式*/
    char temp3[10] = { 0 };
    while (1)
    {
        temp = strchr(temp, '6');
        //if (temp==NULL)
        //    temp = strchr(temp, '6');
        if (temp == NULL)
            return -1;
        if (*(temp + 1) >= '0'&&*(temp + 1)<='9'&&*(temp + 2)>='0'&&*(temp + 2) <= '9')
        {
            //表明连续3个字符都是数字
            strncpy(temp2, temp, 3);
            temp = strchr(temp, '.');
            strcpy(temp3, temp);
            break;
        }
        temp += 1;
    }
    sprintf(newname, "F:/仓库1/火影忍者/火影忍者%s%s", temp2, temp3);
    return errmsg;
}

void main()
{
  //读取目录下文件的方法
struct _finddata_t files; int File_Handle; File_Handle = _findfirst("F:/仓库1/火影忍者/*", &files); if (File_Handle == -1) { printf("error\n"); return ; } int i = 0; char oldname[1024] = { 0 }; char newname[1024] = { 0 }; do { memset(oldname, 0, sizeof(oldname)); memset(newname, 0, sizeof(newname)); //printf("%s \n", files.name); sprintf(oldname, "F:/仓库1/火影忍者/%s", files.name); if (protectstr(files.name, newname, strlen(files.name)) == 0) rename(oldname, newname); i++; } while (0 == _findnext(File_Handle, &files)); _findclose(File_Handle); //char newname[1024] = { 0 }; //protectstr(names, newname, strlen(names)); //printf("%s\n", newname); system("pause"); }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C语言中,可以使用系统调用函数来批量修改文件名。首先需要打开目标文件夹,并使用相关函数列出目标文件夹中的所有文件名,然后循环遍历每个文件名,对每个文件名进行修改操作。 在打开目标文件夹时,可以使用`opendir`和`readdir`函数来获取文件夹中的文件名列表。然后可以使用`chdir`函数来进入文件夹内部,对每个文件名使用`rename`函数来进行文件名修改操作。需要注意的是,修改文件名时需要填写原文件名和新文件名。 具体的C语言代码可能如下所示: ```c #include <stdio.h> #include <stdlib.h> #include <dirent.h> #include <string.h> int main() { DIR *dir; struct dirent *ent; if ((dir = opendir("path_to_folder")) != NULL) { // 打开目标文件夹 while ((ent = readdir(dir)) != NULL) { // 遍历文件夹中的文件名 if (ent->d_type == DT_REG) { // 判断是否为普通文件 char old_name[100]; char new_name[100]; sprintf(old_name, "path_to_folder/%s", ent->d_name); // 原文件名路径 sprintf(new_name, "path_to_folder/new_%s", ent->d_name); // 新文件名路径 rename(old_name, new_name); // 修改文件名 } } closedir(dir); // 关闭目标文件夹 } else { perror("Unable to open directory"); return 1; } return 0; } ``` 需要注意的是,以上代码只是一个示例,实际使用时需要替换`path_to_folder`为目标文件夹的实际路径,并根据具体需求进行代码调整。另外,修改文件名时需要考虑文件名的合法性和重复性,以免造成意外错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值