Renaming a large amount of files

Started by
9 comments, last by Drew_Benton 19 years, 1 month ago
I need to write a tool that takes a number of files with a specific extension (i.e. .zip) and rename them with another extension. I'm not looking for the solution, just ideas. Any help is appreciated.
Advertisement
How will you select the files to be renamed? If you just need to fix them in a specific directory then this should do the trick:
rename *.zip *.new_extension
You of course also use rename to change the extension of specific files:
rename specific_file.zip *.new_extension
Here's a useful script for recursively changing the extension of all files on a drive.
Otherwise you could always use ANSI C's rename function.
How do you want to get the input files? Recursively find them? Get the user to drag and drop files into a listbox? There's a rename() function (MSDN is useless for finding links for functions with names like that, so I can't clickify it) which takes two filenames, and renames the file for you which you could use.
Write a shell script?
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
yeah... I found the rename function. I plan for the input to be the path to the directory where the files reside. Assuming all of the files have the same extension, would it be possible to get the file names from the system? Like redirecting a dir command into a temp file and looping through the files based on that?

Any insight is appreciated.

EDIT: I'm not using *nix
Quote:Original post by KindFred
I plan for the input to be the path to the directory where the files reside. Assuming all of the files have the same extension, would it be possible to get the file names from the system? Like redirecting a dir command into a temp file and looping through the files based on that?
It a lot easier than that. You can use the following batch script:
rename %1\*.zip *.new
Just call it with the path and it will rename any .zip files to .new files.
If you want to do this programatically, you can use FindFirstFile():
#include <windows.h>#include <string>bool Rename(){HANDLE hFind;WIN32_FIND_DATA theFind;   // Start search //   hFind = FindFirstFile("C:/blah/*.zip",&theFind);   if(hFind == INVALID_HANDLE_VALUE)   {      // No .zip files found      return false;   }   // Process //   do {      // Get current and new filenames (and paths) //      std::string strFilePath("C:/blah/");      strFilePath += theFind.cFileName;      std::string strNewName(strFilePath);      int nPos = strNewName.rfind('.');      if(nPos != std::string::npos)      {         strNewName.erase(strNewName.begin()+nPos+1,strNewName.end());         strNewName += ".foo" // New extension      }      // Rename //      rename(strFilePath.c_str(),strNewName.c_str());   } while(FindNextFile(hFind,&theFind));   // Cleanup and return //   FindClose(hFind);   return true;}

Or something like that anyway...
Well, holy crap... don't I just feel like a fool.

I might still write it just to prove to myself that I can reinvent the wheel. :P

Thanks for your help and patience.
[edit]Darn you Evil Steve. You're evil. Wait... Oh yeah, you are evil. Doh! Meanwhile, I'm just slow.[/edit]
Assuming a more complicated renaming process (since simpler methods have already been described) (and assuming the Win32 platform), a C program would be rather easy to create, using functions such as FindFirstFile(), FindNextFile(), FindClose(), and MoveFile(). I don't know if you could use MoveFile() in the loop controlled by first three functions, but it wouldn't be hard to simply throw all the matching filenames into a std::vector<std::string>, and then loop through that afterwards, using the MoveFile() function to rename them.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
shell script.

This topic is closed to new replies.

Advertisement