How To Make C++ App Rename Files
Hi all. I have thousands of files that need to be renamed. What I have is a folder full of files (named sequentially) and then a .txt file with the desired names of each file (also in sequential order). What I need is a program that opens the .txt file, reads a line and then goes to the folder and renames the file, traverses the .txt file by one line, reads said line and then goes to folder and renames the next file.. so on and so forth. I've taken c++ in college several years ago but haven't coded since then. I'm trying to access each file using directory functions (which I've never had experience with). Is this the correct approach? So far, this is what I have come up with but I am clueless on how to get any further. Thanks for any and all help!
#include <iostream>
#include <fstream>
#include <string>
#include <dirent.h>
using namespace std;
int main()
{
DIR * dir = opendir("J:\Folder_Name\")
ifstream inFile ("desiredName.txt");
string line,
oldFile,
newFile;
inFile.open();
if (!dir)
cout << "Error opening directory." << endl;
else
{
struct dirent *entry;
while (entry = readdir(dir))
{
oldFile = entry->d_name;
if ( !inFile.is_open() )
cout << "Failed to open input file." << endl;
else
{
getline(inFile, line);
cout << line << endl;
newFile = line;
}
if (newFile.size() == 0;
cout << "File read error." << endl;
//here is where I get stuck
how am I supposed to go about renaming the current "entry" and then get both the directory and the inFile to traverse to the next line??
It sounds like something better done in a script. But to be fair, I'm really not clear on what you need to do as you've described your process rather than your requirement.
You have a Windows file spec but use the POSIX directory API. What platform are you actually using? What does the .txt file look like?
I'm not sure I follow exactly: Your input file has the desired names, but how do you match those names up with what is in the file system? Are you saying that you want to name the "current entry" from readdir() to the current line from the text file?
If so, you are going to need to consider what to do if your lists are not the same length.
It will be more helpful if you use the code tags (under Format) around your code.
I'm not sure what you mean by saying it'd be better done in script. I can't exactly post the .txt file because they are tax documents. However, the text file is just a long list of the desired file names. Example:
IRS_E_23449_yadayada.xls
IRS_E_23450_yadayada.xls
IRS_E_23451_yadayada.xls
and the actual files I need renamed look exactly the same without the "_yadayada.xls" (Example: IRS_E_23449.xls). While all the files are stored in a folder on my local drive, all of the names I need them renamed to are stored in a .txt file. So what I need is a program that can pull the desired file names and replace the current file names.
Fortunately, all the desired names and all the files were pulled from the same database at the same time. So there is no need to compare the current file names to the desired file names because they all fall in the same sequential order. Nor is there a need to worry about the length of the lists, just when one or the other ends the program should stop
I'm not sure you answered my question about the "current entry" (because I didn't phrase it well), but I get the part where your file has the desired file names.
For example, do you need to find the a file named IRS_E23449.xls and rename it IRS_E_23449-yadayyads.xls? And so on and so forth?
You can build a script file with your program.
Open an output stream (CmdFile)
While reading your input file
Copy the "new" file name to a variable called NewFileName.
Write to the NewFileName to a variable but without the "yadayadayada.xls" -- call it existingName
Write a command to the output stream:
CmdFile << "rename existingName NewFileName" << endl;
When you are finished reading the input file, just execute the script.
kooth, thank you for your responses. I understand what it is you are saying. So once I have the new "CmdFile" with several lines such as:
rename IRS_E_23449 IRS_E_23449_yadayada.xls
rename IRS_E_23450 IRS_E_23450_yadayada.xls
rename IRS_E_23451 IRS_E_23451_yadayada.xls
.......
How then do I "execute the script." You designate it as a Cmd file. My guess is you do this somehow through command prmpt. I've never actually used command prmpt other than to decipher some internet settings. Thanks again!
You're welcome! If you named your script with the extension of ".bat", such as "RenameABunch.bat" just type in the name: RenameABunch.bat.
As kbw said, you never really did answer his question about platform, and I concur with kbw: It seems that you are using Windows.
Good luck!
kooth and kbw, I wish I knew you two personally so I could take you both out for drinks. What was originally going to take me a little over 140 hours (that is 3.5 weeks of working non stop on this), I was able to do in 30 minutes (downloading files and running the script). Thank you both VERY much :)
I'm sure I speak for kbw on this - Thanks for the kind words! Happy you saved some time!
If you're ever in Tampa, FL, I'll take you up on that drink offer!
Topic archived. No new replies allowed.
How To Make C++ App Rename Files
Source: http://www.cplusplus.com/forum/general/70729/
Posted by: wrighthathery.blogspot.com
0 Response to "How To Make C++ App Rename Files"
Post a Comment