Skip to content

Instantly share code, notes, and snippets.

@sms420
Created May 28, 2009 05:39
Show Gist options
  • Save sms420/119120 to your computer and use it in GitHub Desktop.
Save sms420/119120 to your computer and use it in GitHub Desktop.
SMSRename.cpp
/* SMSRename.cpp
Save the executable from this source code
in a directory where you want to rename a
set of files.
Reads a file name from SMSoldnamesINPUT.txt and
then reads a file name from SMSnewnamesINPUT.txt
and then changes the name of the file in the
directory from the oldname -> newname.
Can spelunk through file directories fine.
Iterates line-by-line through each file.
Prints results to SMS_Log.txt.
********************************************/
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
int main () {
int result;
string temp_old;//temporary string to hold old file name
string temp_new;//temporary string to hold new file name
ifstream oldNames ("SMSoldnamesINPUT.txt");
ifstream newNames ("SMSnewnamesINPUT.txt");
ofstream SMS_Log;
SMS_Log.open("SMS_Log.txt");
if (oldNames.is_open() && newNames.is_open() )
{
while (! oldNames.eof() && ! newNames.eof() )
{
// read line from SMSoldnamesINPUT.txt & copy to temp string
getline (oldNames,temp_old);
// dynamically alocate enough memory for char array
char *a=new char[temp_old.size()+1];
// assign the value of zero to the array
a[temp_old.size()]=0;
// copy data from temp string into char array
memcpy(a,temp_old.c_str(),temp_old.size());
/* repeat above process with SMSnewnamesINPUT.txt */
getline (newNames,temp_new);
char *b=new char[temp_new.size()+1];
b[temp_new.size()]=0;
memcpy(b,temp_new.c_str(),temp_new.size());
result= rename( a , b ); //a = oldName, b = newName
if ( result == 0 )
{
SMS_Log << "oldname: ";
for (int i = 0; i < temp_old.size(); i++)
{
SMS_Log << a[i];
}
SMS_Log << '\t' << "newname: ";
for (int i = 0; i < temp_new.size(); i++)
{
SMS_Log << b[i];
}
SMS_Log << endl;
}
}
SMS_Log.close();
oldNames.close();
newNames.close();
}
else cout << "Unable to open file(s)";
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment