Board index » cppbuilder » Renaming multiple files

Renaming multiple files


2007-10-22 10:38:44 AM
cppbuilder72
And I mean multiple, I have like hundreds that I have to rename to insert
into
a DB. This is my dilemma at the moment.
I have filenames in the following format:
20040821.txt (first line: Saturday, August 21, 2004 )
20050414.txt (first line: Thursday April 14, 2006)
20050415.txt (first line: Friday April 15, 2006)
In this format of the file, in the first line of the file the date is
already there.
But I need to rename the filenames in the following manner.
21 - Saturday.txt
14 - Thursday.txt
15 - Friday.txt
My other trillion filenames are in the format of this one.
Thursday December 01, 2005.txt
Thursday December 08, 2005.txt
Thursday December 22, 2005.txt
But I still need to rename them to the same format as above.
01 - Thursday.txt
08 - Thursday.txt
22 - Thursday.txt
etc. etc..
Does anyone have like a quick one liner of code that will do this? I am
really sucky
at parsing stuff.
Thanks for any help.
CJ
 
 

Re:Renaming multiple files

Hi CJ
In article <471c0d2b$ XXXX@XXXXX.COM >
XXXX@XXXXX.COM
CJ says:
Quote

Does anyone have like a quick one liner of code that will do this?
One liner ? No!
But the code below should do it:
Kind regards
Asger
//this function converts without opening the file, but
//if You cant trust the original filenames You can always
//open the file read the first line and use the other
//function
bool __fastcall ConvertNumberName(String &ResName, char*
SourceName)
{
FILETIME fTime = {0};
SYSTEMTIME sTime = {0};
char *Sn = SourceName;
char Tmp, *End = SourceName + 4;
int i, L = strlen(Sn);
for(i = 0; i < L && Sn[i] != '.'; ++i)
{
if(Sn[i] < '0' || Sn[i]>'9')return false;
}
if(Sn[i] != '.' || i != 8)return false;
Tmp = *End; *End = '\0';
sTime.wYear = atoi(Sn);
*End = Tmp;
Sn = End; End += 2;
Tmp = *End; *End = '\0';
sTime.wMonth = atoi(Sn);
*End = Tmp;
Sn = End; End += 2;
Tmp = *End; *End = '\0';
sTime.wDay = atoi(Sn);
*End = Tmp;
SystemTimeToFileTime(&sTime, &fTime);
FileTimeToSystemTime(&fTime, &sTime);
if(sTime.wDay < 10)
ResName = String("0") + String(sTime.wDay) + " - ";
else ResName = String(sTime.wDay) + " - ";
switch(sTime.wDayOfWeek)
{
case 0: ResName += "Sunday"; break;
case 1: ResName += "Monday"; break;
case 2: ResName += "Tuesday"; break;
case 3: ResName += "Wensday"; break;
case 4: ResName += "Thuesday";break;
case 5: ResName += "Frieday"; break;
case 6: ResName += "Saturday";break;
}
ResName += ".txt";
return true;
}
//------------------------------------------------------
bool __fastcall ConvertDateName(String &ResName, char*
SourceName)
{
char Tmp, *End, *Start = SourceName;
String DayName, DayNumber;
End = Start;
while((unsigned)(*End)>31 && *End != ' ' && *End != ',')
++End;
if(*End != '\0')
{
Tmp = *End; *End = '\0';
DayName = Start;
*End = Tmp;
}else return false;
while((unsigned)(*End)>31 && (*End < '0' || *End>'9'))
++End;
if(*End>= '0' && *End <= '9')
{
Start = End;
while(*End>= '0' && *End <= '9')++End;
if(End - Start>2)return false;
Tmp = *End; *End = '\0';
DayNumber = Start;
*End = Tmp;
}else return false;
ResName = DayNumber + " - " + DayName + ".txt";
return true;
}
//-----------------------------------------------------------
//The function below is not tested since i dont have a
//bunch of file with that kind of names.
//Remember to make a backup before testing.
void __fastcall RenameFiles(String SourcePath, String
DestPath)
{
String SearchPath;
if(SourcePath[SourcePath.Length()] != '\\')
SourcePath += "\\";
SearchPath = SourcePath + "*.txt";
if(DestPath[DestPath.Length()] != '\\')
DestPath += "\\";
HANDLE hFile;
WIN32_FIND_DATA Wfd;
hFile = FindFirstFile(SearchPath.c_str(), &Wfd);
if(hFile == INVALID_HANDLE_VALUE)return;
do{
bool Res;
String Buf;
char *cFile = Wfd.cFileName;
if(*cFile < '0' || *cFile>'9')
Res = ConvertNumberName(Buf, cFile);
else Res = ConvertDateName(Buf, cFile);
if(!Res)continue;
String NewFileName = DestPath + Buf;
if (FileExists(NewFileName))
{
//Give Your self a message
continue;
}
if( ! RenameFile(SourcePath + String(cFile),
NewFileName) )
{
//Give Your self a message
}
}while(FindNextFile(hFile, &Wfd));
FindClose(hFile);
}
//-----------------------------------------------------------
 

Re:Renaming multiple files

Hi Asger,
Thanks for this code. I had no idea that it would be so many lines of
code.
Thanks again
CJ
 

{smallsort}

Re:Renaming multiple files

CJ < XXXX@XXXXX.COM >wrote:
Quote
Thanks for this code. I had no idea that it would be so many lines of
code.
Of course! C++ is not designed for file renaming and has to do a bit of
work to manipulate strings. If you have access to a Unix machine, it
could be done in a (long, complicated) one liner using head, awk and
friends, but based on the newsgroup, I'd say this was the kind of
solution you were after.
Just one caveat - since you are throwing away the year and month
information in your file naming scheme, there is a high occurance
of naming clashes. You'll probably need some method to handle this.
--
*--------------------------------------------------------*
| ^Nothing is foolproof to a sufficiently talented fool^ |
| Heath Raftery, HRSoftWorks _\|/_ |
*______________________________________m_('.')_m_________*