#include
#include
#include
using namespace std;
using namespace boost::filesystem;
void process_file()
{
}
class Path_Enumerator
{
protected:
path p;
recursive_directory_iterator iter;
virtual void operatefile() {};
public:
void enumerator()
{
try
{
if (exists(p)) // does p actually exist?
{
if (is_directory(p)) // is p a directory?
{
recursive_directory_iterator end_iter;
while(iter != end_iter)
{
if(is_regular_file(*iter))
operatefile();
iter++;
}
}
else
cout << p << " is not a directory!" << endl;
}
else
cout << p << " does not exist\n";
}
catch (const filesystem_error& ex)
{
cout << ex.what() << '\n';
}
}
};
class Enumerator_print : public Path_Enumerator {
void operatefile()
{
cout << iter->path().string() << endl;
}
public:
Enumerator_print(std::string pathname)
{
p = path(pathname);
iter = recursive_directory_iterator(p);
}
};
int main()
{
Enumerator_print ep("result");
ep.enumerator();
return 0;
}
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
typedef std::vector < std::string > stringVector;
void getFileList(const string& pathName, stringVector &recusiveFileVec)
{
boost::filesystem::recursive_directory_iterator rdi(pathName);
boost::filesystem::recursive_directory_iterator end_rdi;
recusiveFileVec.empty();
for (; rdi != end_rdi; rdi++) {
if (is_directory(*rdi)) {
std::cout << *rdi << "is pathName" << std::endl;
} else {
recusiveFileVec.push_back(rdi->path().string());
std::cout << *rdi << " is a file" << std::endl;
}
}
}
stringVector breakTokens(const string& raw_input)
{
boost::regex re(" [-] ");
boost::sregex_token_iterator i(raw_input.begin(), raw_input.end(), re, -1);
boost::sregex_token_iterator j;
stringVector lineVec;
while (i != j) {
lineVec.push_back(*i);
i++;
}
return lineVec;
}
void processFile( const string &filePathName, const char* directory)
{
string tempFileName = filePathName + string(".bak");
boost::regex pathRegex(directory);
std::string delRegex("");
std::string path = regex_replace(filePathName, pathRegex, delRegex);
stringVector filePath;
std::string delims = "/";
boost::split(filePath, path, boost::is_any_of(delims));
int size = filePath.size();
string category;
if (size > 2) {
category = string("__") + filePath[size - 3] + string("__") + filePath[size - 2];
} else if (size == 2) {
category = string("__") + filePath[size - 2];
} else {
category = "";
}
std::ifstream inputFile(filePathName.c_str());
std::ofstream outfile(tempFileName.c_str());
stringVector lineVec;
if (inputFile.is_open()) {
while (inputFile.good()) {
string line;
getline(inputFile, line);
string outString;
lineVec = breakTokens(line);
size = lineVec.size();
cout << "size = " << size << endl;
if (size == 1) {
outString = lineVec[0] + category;
cout << "outString = " << outString << endl;
outfile << outString << endl;
}
else if (size == 2) {
outString = lineVec[0] + string("::") + line + category;
cout << "outString = " << outString << endl;
outfile << outString << endl;
outString = lineVec[1] + string("::") + line + category;
cout << "outString = " << outString << endl;
outfile << outString << endl;
}
}
} else {
cout << "Unable to open file";
}
inputFile.close();
outfile.close();
// boost::filesystem::remove(filePath.c_str());
// boost::filesystem::rename(tempfile.c_str(), filePath.c_str());
}
int main(int argc, char *argv[])
{
if (argc < 2) {
cout << "Invalid arg --- You should input a Directory path as an argument.(ex: /home/hello" << endl;
return -1;
}
stringVector fileList;
getFileList(argv[1], fileList);
for (int i = 0; i < fileList.size(); i++) {
processFile(fileList[i], argv[1]);
cout<
}