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"); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM