-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageRename.cpp
More file actions
58 lines (49 loc) · 1.73 KB
/
ImageRename.cpp
File metadata and controls
58 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include "ImageRename.h"
#include <opencv2/opencv.hpp>
#include <iostream>
#include <cstdio>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include <algorithm>
#include <filesystem>
using namespace std;
using namespace cv;
vector<string> oldNames;
vector<string> newNames;
void ImageRename::loadCSV(const string& csvFilePath, string directory) {
ifstream csvFile(csvFilePath);
if (!csvFile.is_open()) {
cerr << "Could not open the CSV file: " << csvFilePath << endl;
return;
}
string line;
// Read each line from the CSV file
while (getline(csvFile, line)) {
stringstream ss(line);
string oldName, newName;
// Get the old and new names from the CSV
if (getline(ss, oldName, ',') && getline(ss, newName, ',')) {
// Trim whitespace
oldName.erase(remove_if(oldName.begin(), oldName.end(), ::isspace), oldName.end());
newName.erase(remove_if(newName.begin(), newName.end(), ::isspace), newName.end());
// Store names in the respective vectors
oldNames.push_back(oldName);
newNames.push_back(newName);
}
}
csvFile.close();
}
////////////////////////////////////////////////////////////////////////////////
void ImageRename::RenameFileName(string directory, string extension)
{
// Rename
for (size_t i = 0; i < oldNames.size(); i++) {
// Construct full file paths
string oldFilePath = directory + oldNames[i] + extension;
string newFilePath = directory + "Final\\" + newNames[i] + extension;
// Attempt to rename the file
rename(oldFilePath.c_str(), newFilePath.c_str());
}
}