栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

批量修改文件名

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

批量修改文件名

对一批HCT文件文件名进行批量修改 要求是将文件名修改为3DB0001、3DB0002、。。。。3DB0100

使用的是_finddata_t函数


#include "afx.h"
#include "stdafx.h"
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;

int main()
{
    vectorfiles;
    long handle;
    struct _finddata_t FileInfo;
    string path("C:\Users\m1575\Desktop\Bag\3DB0015\Data\");  //文件所在路径
    string file_path;   //用户手动输入路径
    cout << "请输入正确的文件路径:"<< endl;
    cin>> file_path;
    if (path > file_path)
    {
        printf("输入的文件路径太小了n");
    }
    else if(path < file_path)
    {
        printf("输入的文件路径太大了n");
    }
    else
    { 
        printf("输入的文件路径正确n");
    }                                             //判断用户输入的路径和给定的路径是否一致

    string fullpath = path+"*.HCT";    // 加上后缀名 HCT
    handle = _findfirst(fullpath.c_str(), &FileInfo);
    if (handle==-1)
    {
        cout << "没有找到HCT文件" << endl;
        printf("%sn", FileInfo.name);
        return 0;
    }   //找第一个HCT文件
    cout << handle << endl;   //handle值 ,这一行可有可无

    int index = 1;
    int width = 3;
    char t_str1[10];
    _itoa_s(index, t_str1,10);   //将整数转化为string
    string str(t_str1);
    string strnew(width-str.length(),'0');
    str = strnew + str;  //str的值为“001”
    string s("3DB0015_0");
    string oldname = file_path + FileInfo.name;
    string newname = file_path + s+str+".HCT";   

//这一部分是为了让文件名从0001、0002递增到0100
    rename(oldname.c_str(), newname.c_str());   //调用重命名函数rename
    while (_findnext(handle, &FileInfo) == 0)
    {
        index++;
        _itoa_s(index, t_str1,10);
        string str(t_str1);
        string strnew(width - str.length(), '0');
        str = strnew + str;
        string oldname = file_path + FileInfo.name;
        string newname = file_path + s + str + ".HCT";
        rename(oldname.c_str(), newname.c_str());
        if (rename(oldname.c_str(), newname.c_str()) < 0)
        {
            printf("重命名失败n");
        }
        else
        {
            printf("重命名成功n");
        }
    }_findclose(handle);
    system("pause");
    return 0;
}
 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/690250.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号