Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: main.cpp
- * Author: KeplerBR
- * Version: 1.0
- *
- * Created on 8 de Julho de 2014, 23:10
- *
- * Tested only on Linux.
- * To use, open via terminal. Will rename all files in the directory to lowcase.
- * For example, a file named "KeplerBR" will be renamed to "keplerbr"
- */
- #include <stdio.h>
- #include <cstdlib>
- #include <dirent.h>
- #include <algorithm>
- #include <string>
- #include <unistd.h>
- #define GetCurrentDir getcwd
- using namespace std;
- int main(int argc, char** argv) {
- // Get current path
- char currentPath[FILENAME_MAX];
- if (!GetCurrentDir(currentPath, sizeof(currentPath))) {
- fprintf(stderr, "Could not recognize the current directory\n");
- return 1;
- }
- currentPath[sizeof (currentPath) - 1] = '\0';
- printf("The current working directory is %s\n", currentPath);
- // List files
- DIR *dir;
- struct dirent *ent;
- if ((dir = opendir(currentPath)) != NULL) {
- int countError = 0;
- while ((ent = readdir(dir)) != NULL) {
- // Rename
- string currentName = ent->d_name;
- if (currentName == "." || currentName == "..") {
- continue;
- }
- string newName;
- newName.resize(currentName.size());
- transform(currentName.begin(), currentName.end(), newName.begin(), ::tolower);
- bool resultRename;
- resultRename = rename(currentName.data(), newName.data());
- if (resultRename == 0) {
- printf("%s -> %s\n", currentName.data(), newName.data());
- } else {
- fprintf(stderr, "Could not rename %s to %s\n", currentName.data(), newName.data());
- countError++;
- }
- }
- if (countError > 0) {
- printf("Failed to rename %i file(s)\n", countError);
- }
- } else {
- fprintf(stderr, "Could not open directory\n");
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement