c ++文件系统,按字母顺序获取目录中的文件,重命名输入
c++ filesystem, get files in directory alphabetically, rename inputs
我有以下代码:
/*
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
}
我有以下代码:
/*
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
}