// VTB File Utility // // Douglas Thrift // // VTBFileUtil.cpp #ifdef _DEBUG #pragma warning(disable:4786) #endif // _DEBUG #include #include #include #include #include #include #include #include using namespace std; const unsigned LENGTH = 1024; const char* CFG = "VTBFileUtil.cfg"; string filename(string& scan); void rename(string& scan, string& good); void move(string& good, string& directory); void size(string& scan); void quit(int code); int main(int argc, char* argv[]) { string directory; if (argc > 1) { ifstream fin(CFG); getline(fin, directory); fin.close(); } else { cout << "VTB File Utility\n" << "Usage: VTBFileUtil [pdfscan ...]\n"; return 0; } if (directory == "") { do { cout << "No directory specified.\nDirectory: "; char* cstr = new char[LENGTH]; cin.getline(cstr, LENGTH); directory = cstr; delete [] cstr; } while (directory == ""); ofstream fout(CFG); fout << directory << "\n"; fout.close(); } for (unsigned index = 1; index < argc; index++) { if (strpbrk(argv[index], "*?")) continue; string extension; for (int number = 4; number > 0; number--) { extension += tolower(argv[index][strlen(argv[index]) - number]); } if (extension != ".pdf") { cout << argv[index] << " is not a PDF.\n"; continue; } ifstream tester(argv[index]); if (!tester.is_open()) { cout << argv[index] << " does not exist.\n"; continue; } tester.close(); string scan = argv[index]; string command = "start " + scan; system(command.c_str()); string good; bool correct = false; do { good = filename(scan); cout << "Correct (y/n): "; string answer; char* cstr = new char[LENGTH]; cin.getline(cstr, LENGTH); answer = cstr; delete [] cstr; for (int index = 0; index < answer.length(); index++) { answer[index] = tolower(answer[index]); } if (answer == "yes" || answer == "y") { correct = true; } else { cout << "Continue (y/n): "; cstr = new char[LENGTH]; cin.getline(cstr, LENGTH); answer = cstr; delete [] cstr; if (answer != "yes" && answer != "y") quit(0); } } while (!correct); rename(scan, good); move(good, directory); tester.open(directory.c_str()); if (tester.is_open()) { tester.close(); rename(directory, good); command = "mkdir " + directory; system(command.c_str()); move(good, directory); } size(directory + "\\" + good); } return 0; } string filename(string& scan) { string name, file; unsigned number; cout << "Scan PDF: " << scan << "\n" << "Client Name: "; char* cstr = new char[LENGTH]; cin.getline(cstr, LENGTH); name = cstr; delete [] cstr; cout << "Client Number: "; cstr = new char[LENGTH]; cin.getline(cstr, LENGTH); number = strtoul(cstr, 0, 0); delete [] cstr; cstr = new char[LENGTH]; sprintf(cstr, "%u_", number); file += cstr; delete [] cstr; for (unsigned index = 0; index < name.length(); index++) { if (name[index] == ',' || name[index] == '.') { } else if (isspace(name[index])) { file += '_'; } else { file += tolower(name[index]); } } file += ".pdf"; cout << "Filename: " << file << "\n"; return file; } void rename(string& scan, string& good) { string command = "rename \"" + scan + "\" \"" + good + "\""; while (system(command.c_str())) system("pause"); } void move(string& good, string& directory) { string command = "move \"" + good + "\" \"" + directory + "\""; while (system(command.c_str())) system("pause"); } void size(string& scan) { struct stat status; if (!stat(scan.c_str(), &status)) { char* cbytes = new char[LENGTH]; sprintf(cbytes, "%u", status.st_size); string bytes = cbytes; delete [] cbytes; for (int index = bytes.length(), number = 0; index > 0; index--, number++) { if ((number % 3 == 0) && ((index - 3) > 0)) { bytes.insert(index - 3, 1, ','); } } char* megabytes = new char[LENGTH]; sprintf(megabytes, "%.2f", (double(status.st_size) / double(1024) / double(1024))); cout << "Filesize: " << bytes << " bytes (" << megabytes << " MB)\n"; delete [] megabytes; } else { cerr << "VTBFileUtil: stat(scan.c_str(), &status) failed in void size(" << "string& scan).\n"; exit(1); } } void quit(int code) { exit(code); }