c++文件系统,按字母顺序获取目录中的文件,重命名输入



我有以下代码:

/*
This function conditions folders into a standard format with the following specifications
The first file in the folder to render has an index starting at 0
with the name of outPutName[Index].fileExtension  dataType is preserved 
*/
void vCanvas::conditionFolder(vizModule target){
vector<string> fileNames;
int index = 0;
string outputName = "output";
string oPath = target.path;
string temp;
cout << "vCanvas::conditionFolder is processing" << oPath << endl;
//https://en.cppreference.com/w/cpp/filesystem/rename
for (const auto & entry : fs::directory_iterator(oPath)){
if (entry.is_regular_file()){
temp = entry.path().stem().string();
cout << oPath+temp << " modified to " << oPath+(outputName+to_string(index)+"."+target.dataType) << endl;
fs::rename(oPath+temp, oPath+(outputName+to_string(index)+"."+target.dataType));
index++;
}
}
}

我希望fs::directoryiterator能按字母顺序给我entry,但事实并非如此。我在0001.png之前得到了0007.png,这是不可行的。有没有什么简单的解决方案可以让我按字母顺序获取文件夹中的所有文件,并重命名它们?

上面代码的第二次迭代,使用std::sort

//see https://www.cplusplus.com/reference/algorithm/sort/
bool alphabetSort(string i, string j){
int val = i.compare(j);
if (val == -1) {return true;}
else if (val == 1) {return false;}
else {return false;}
}
/*
This function conditions folders into a standard format with the following specifications
The first file in the folder to render has an index starting at 0
name of outPutName.  dataType is preserved
*/
void vCanvas::conditionFolder(vizModule target){
string outputName = "output";
string oPath = target.path;
string temp;
vector<string> fileStrings;
cout << "vCanvas::conditionFolder is processing" << oPath << endl;
//https://en.cppreference.com/w/cpp/filesystem/rename
for (const auto & entry : fs::directory_iterator(oPath)){
if (entry.is_regular_file()){
temp = entry.path().stem().string();
fileStrings.push_back(temp);
}
}
for (int i = 0; i < fileStrings.size(); i++){
cout << fileStrings[i] << ",";
}
sort(fileStrings.begin(), fileStrings.end(),alphabetSort);
cout << "Directory sorted" << endl;
for (int i = 0; i < fileStrings.size(); i++){
cout << fileStrings[i] << ",";
}
cout << endl;
for (int k = 0; k < fileStrings.size(); k++){
temp = fileStrings[k];
cout << oPath+temp << " modified to " << oPath+(outputName+to_string(k)+"."+target.dataType) << endl;
fs::rename(oPath+temp, oPath+(outputName+to_string(k)+"."+target.dataType));
}
}

这就产生了

vCanvas::conditionFolder is processing./unit_tests_folder/testpng1/
0007,0014,0018,0008,0012,0002,0005,0010,0016,0003,0001,0006,0015,0019,0017,0004,0011,0013,0009,0020,
0007,0009,0008,0002,0005,0004,0003,0001,0006,0014,0013,0011,0017,0019,0015,0016,0010,0012,0018,0020,

我编写并运行了这个函数的单元测试,它实现了我想要的功能,具体来说,就是首先按字母顺序排序,但如果程序比较2位数字,则采用较小大小的字符串。这适用于我的目的,即简单地处理一致但任意命名的.png系列,并将它们转换为标准化格式。

bool alphaNumerSort(string i, string j){
string shorterStr;
(i.size() <= j.size()) ? shorterStr = i : shorterStr = j;
for (int k = 0; k < shorterStr.size(); k++)
{
if (i[k] != j[k] && isdigit(i[k]) && isdigit(j[k]) ) {
if (i.size() == j.size()) {
return i[k] < j[k];
}
return (i.size() < j.size());
}
if (i[k] != j[k]) {
return i[k] < j[k];
}
}
return false;
//returns true if i is alphabetically before j
}

最新更新