Jump to content
  • Advertisement
Sign in to follow this  
KindFred

Renaming a large amount of files

This topic is 5020 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!