一款批量修改文件后缀扩展名的工具

前段时间因需要修改大量的文件扩展名,因而就出现了一个想法,用程序自己帮我们修改,所以就出现了本文,本工具部分代码摘操自网络,经过我的修改,实现了批量修改文件扩展名的功能。

本工具支持使用控制台传参使用,第0个参数是程序名,这个相信大家都知道,第1个参数是要修改文件的目录,第2个参数为要修改的文件扩展名(格式如:.txt)

下面上代码:

#include <iostream>
#include <string>
#include <io.h>
#include <stdlib.h>
#include <vector>
using namespace std;
 
void getFiles(string path, vector<string>& files)
{
        //文件句柄  
        long   hFile = 0;
        //文件信息,声明一个存储文件信息的结构体  
        struct _finddata_t fileinfo;
        string p;//字符串,存放路径
        if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)//若查找成功,则进入
        {
                do
                {
                        //如果是目录,迭代之(即文件夹内还有文件夹)  
                        if ((fileinfo.attrib &  _A_SUBDIR))
                        {
                                //文件名不等于"."&&文件名不等于".."
                                //.表示当前目录
                                //..表示当前目录的父目录
                                //判断时,两者都要忽略,不然就无限递归跳不出去了!
                                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                                        getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
                        }
                        //如果不是,加入列表  
                        else
                        {
                                files.push_back(p.assign(path).append("\\").append(fileinfo.name));
                        }
                } while (_findnext(hFile, &fileinfo) == 0);
                //_findclose函数结束查找
                _findclose(hFile);
        }
}
 
void string_replace(std::string &strBig, const std::string &strsrc, const std::string &strdst)
{
        std::string::size_type pos = 0;
        std::string::size_type srclen = strsrc.size();
        std::string::size_type dstlen = strdst.size();
 
        while ((pos = strBig.find(strsrc, pos)) != std::string::npos)
        {
                strBig.replace(pos, srclen, strdst);
                pos += dstlen;
        }
}
 
std::string GetPathOrURLShortName(std::string strFullName)
{
        if (strFullName.empty())
        {
                return "";
        }
 
        string_replace(strFullName, "/", "\\");
 
        std::string::size_type iPos = strFullName.find_last_of('\\') + 1;
 
        return strFullName.substr(iPos, strFullName.length() - iPos);
}
 
//获取文件路径
std::string GetFilePath(std::string strFullName)
{
        if (strFullName.empty())
        {
                return "";
        }
 
        string_replace(strFullName, "/", "\\");
 
        std::string::size_type iPos = strFullName.find_last_of('\\') + 1;
 
        return strFullName.substr(0, iPos);
}
 
void ModifyFileName(string modifyName, vector<string> fileNameList)
{
        //遍历文件名向量,并进行修改
        string strAdd = modifyName;   //在原文件名的基础上要增加的部分
        for (vector<string>::iterator iter = fileNameList.begin(); iter != fileNameList.end(); ++iter)
        {
                string oldName = *iter;
                //str1为原文件名要保留的部分
                string str1 = GetPathOrURLShortName(*iter);//(*iter).substr(0, 5);
                string strNewName = str1.substr(0, str1.rfind("."));
                string strFilePath = GetFilePath(*iter);
                string newName = strFilePath + strNewName + strAdd;
                cout << "oldName:" << oldName << endl;
                cout << "newName:" << newName << endl;
 
                //cout << "oldName size = " << oldName.size() << endl;
                //cout << "newName size = " << newName.size() << endl;
 
                char *oldNamePointer, *newNamePointer;
                oldNamePointer = (char *)malloc((oldName.size() + 1) * sizeof(char));
                newNamePointer = (char *)malloc((newName.size() + 1) * sizeof(char));
                strcpy(oldNamePointer, oldName.c_str());
                strcpy(newNamePointer, newName.c_str());
                cout << oldNamePointer << endl;
                cout << newNamePointer << endl;
 
                rename(oldNamePointer, newNamePointer);
 
                free(oldNamePointer);
                free(newNamePointer);
        }
}
 
int main(int argc, char* argv[])
{
        vector<string> files;
        //wcout.imbue(locale("chs"));
        //要修改的目录名
        string strDirPath = "";
        if (argc <= 1)
        {
                while (strDirPath.length() <= 0)
                {
                        if (strDirPath.length() <= 0)
                        {
                                cout << "请输入文件目录(可以是相对路径):";
                                cin >> strDirPath;
                        }
                }
        }
        else
        {
                strDirPath = argv[1];
        }
        cout << "要修改目录为:" << strDirPath.c_str() << endl;
 
        //要修改的扩展名
        string strName = "";
        if (argc <= 2)
        {
                while (strName.length() <= 0)
                {
                        if (strName.length() <= 0)
                        {
                                cout << "请输入要修改的文件后缀名,如(" << ".txt" << "):";
                                cin >> strName;
                        }
                }
        }
        else
        {
                strName = argv[2];
        }
         
        cout << "要修改的扩展名为:" << strName.c_str() << endl;
 
        //获取该路径下的所有文件  
        getFiles(strDirPath.c_str(), files);
        int size = files.size();
        if (size <= 0)
        {
                cout << "没有发现文件!" << endl;
                system("pause");
                return 0;
        }
        for (int i = 0; i < size; i++)
        {
                cout << files.c_str() << endl;
        }
        //修改文件名
        ModifyFileName(strName, files);
        system("pause");
        return 0;
}

最近搞了一个论坛,主要是分享一些程序员的技术、源码、学习资源之类的,如果大家有兴趣,欢迎大家来捧场。

网址:51开源时代

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值