cocos2d顺序文件名自动格式化

内容纲要

将img_3.png这种文件格式化为img_003.png 或者img_0003.png的c++文件 使用的时候把c++放在与图片同目录下编译运行 方便进行动画编辑 相关参数见示例

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include   <io.h>
#include   <direct.h>
#include   <iomanip>
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(hFile);
    }
}
string getnumName(char keychar,int size, string name)
{
    int pos = name.find(keychar);
    if (pos != string::npos)
    {
        string str = name.substr(pos + 1, name.rfind('.') - pos - 1);
        if (str.length() < size)
        {
            int addnum = size - str.length();
            for (int i = 0; i < addnum; i++)
            {
                str = '0' + str;
            }
        }
        return name.substr(0, pos + 1) + str + name.substr(name.rfind('.'));
    }
    return name;
}
int main()
{
    vector<string>   files;
    getFiles( ".", files );
    for   (int   j = 0;   j < files.size();   ++j)
    {
        rename(files[j].c_str(), getnumName('_',3, files[j]).c_str());
    }
    cout<<"格式化名称完成"<<endl;
    return 0;
}

还有mac/linux版 要先给路径

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <string>
#include <dirent.h>
#include   <iomanip>
using namespace std ;

void createDocList(string real_dir,std::vector<std::string> &doc_list)
{
    int return_code;
    DIR *dir;
    struct dirent entry;
    struct dirent *res;
    if ((dir = opendir(real_dir.c_str())) != NULL)  //打开目录
    {
        for (return_code = readdir_r(dir, &entry, &res); res != NULL && return_code == 0; return_code = readdir_r(dir, &entry, &res))
        {
            if (entry.d_type != DT_DIR)  //存放到列表中
            {
                doc_list.push_back(string(entry.d_name));
            }
        }
        closedir(dir);//关闭目录
    }
}


string getnumName(char keychar, int size, string name)
{
    int pos = name.find(keychar);
    if (pos != string::npos)
    {
        string str = name.substr(pos + 1, name.rfind('.') - pos - 1);
        if (str.length() < size)
        {
            int addnum = size - str.length();
            for (int i = 0; i < addnum; i++)
            {
                str = '0' + str;
            }
        }
        return name.substr(0, pos + 1) + str + name.substr(name.rfind('.'));
    }
    return name;
}
int main()
{

    vector<string>   files;
    string dir="/Users/sky/Downloads/Graphic/";//后面记得加上/要不会崩
    createDocList(dir,files);
    for   (int   j = 0;   j < files.size();   ++j)
    {
        // cout<<files[j]<<endl;
        // cout<<dir+getnumName('_', 3, files[j])<<endl;
        rename((dir+files[j]).c_str(), (dir+getnumName('_', 3, files[j])).c_str());
    }
    cout << "格式化名称完成" << endl;
    return 0;
}

发表回复