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 "); 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 ", 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 ", newname); system("pause"); }
原文地址:https://www.cnblogs.com/zhanggaofeng/p/5991383.html