【发布时间】:2022-01-22 14:04:54
【问题描述】:
我有以下代码:
/*
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 会按字母顺序给我entrys,但它没有。我在 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,
【问题讨论】:
-
您可以将所有条目放在一个容器中,对其进行排序,然后根据索引重命名。
-
@RetiredNinja 做到了,但我仍然需要帮助,因为我不能简单地使用 i.compare(j) 和排序。至少我不这么认为,因为我处理的字符串不是严格意义上的字母,但数字和特殊字符也会很好。
-
您的
alphabetSort函数不是必需的。尽管您的版本复制字符串并以迂回的方式进行比较,但它默认执行std::sort(fileStrings.begin(), fileStrings.end());所做的事情。如果您需要为strings 添加自己的排序,bool alphabetSort(const string& i, const string& j) { return i < j; }将是一个更简单的解决方案。 -
谢谢 Ted,没想到会这么简单。它现在可以工作了,显然我的 alphabetSort 函数是错误的。
-
@TedLyngmo 显然是错误的,但方式不同。一个名为 frame11.png 的文件会出现在 frame2.png 之前
标签: c++ sorting filesystems