Advertisement
KeplerBR

Renamer (v. 1.0)

Jul 8th, 2014
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.01 KB | None | 0 0
  1. /*
  2.  * File:   main.cpp
  3.  * Author: KeplerBR
  4.  * Version: 1.0
  5.  *
  6.  * Created on 8 de Julho de 2014, 23:10
  7.  *
  8.  * Tested only on Linux.
  9.  * To use, open via terminal. Will rename all files in the directory to lowcase.
  10.  * For example, a file named "KeplerBR" will be renamed to "keplerbr"
  11.  */
  12.  
  13. #include <stdio.h>
  14. #include <cstdlib>
  15. #include <dirent.h>
  16. #include <algorithm>
  17. #include <string>
  18. #include <unistd.h>
  19. #define GetCurrentDir getcwd
  20.  
  21. using namespace std;
  22.  
  23. int main(int argc, char** argv) {
  24.     // Get current path
  25.     char currentPath[FILENAME_MAX];
  26.  
  27.     if (!GetCurrentDir(currentPath, sizeof(currentPath))) {
  28.         fprintf(stderr, "Could not recognize the current directory\n");
  29.         return 1;
  30.     }
  31.  
  32.     currentPath[sizeof (currentPath) - 1] = '\0';
  33.     printf("The current working directory is %s\n", currentPath);
  34.  
  35.     // List files
  36.     DIR *dir;
  37.     struct dirent *ent;
  38.     if ((dir = opendir(currentPath)) != NULL) {
  39.         int countError = 0;
  40.        
  41.         while ((ent = readdir(dir)) != NULL) {
  42.             // Rename
  43.             string currentName = ent->d_name;
  44.             if (currentName == "." || currentName == "..") {
  45.                 continue;
  46.             }
  47.            
  48.             string newName;
  49.             newName.resize(currentName.size());
  50.             transform(currentName.begin(), currentName.end(), newName.begin(), ::tolower);
  51.            
  52.             bool resultRename;
  53.             resultRename = rename(currentName.data(), newName.data());
  54.             if (resultRename == 0) {
  55.                 printf("%s -> %s\n", currentName.data(), newName.data());
  56.             } else {
  57.                 fprintf(stderr, "Could not rename %s to %s\n", currentName.data(), newName.data());
  58.                 countError++;
  59.             }
  60.         }
  61.        
  62.         if (countError > 0) {
  63.             printf("Failed to rename %i file(s)\n", countError);
  64.         }
  65.     } else {
  66.         fprintf(stderr, "Could not open directory\n");
  67.         return 1;
  68.     }
  69.    
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement