Re:Renaming lots of files
Try something line this /totally untested/:
(I'll add the code to the attachments group also)
--- CPP ---
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include <Filectrl.hpp>
#include "Unit1.h"
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
Edit1->Text = ExtractFilePath( ParamStr(0) );
}
// Select root folder
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString Dir = Edit1->Text;
if ( !SelectDirectory( Dir, TSelectDirOpts() << sdAllowCreate <<
sdPerformCreate << sdPrompt, 0 ) )
return;
Edit1->Text = IncludeTrailingBackslash( Dir );
}
// Go
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Button1->Enabled = false;
try
{
ListView1->Items->Clear();
AnsiString Dir = Edit1->Text;
if ( ! DirectoryExists( Edit1->Text ) )
{
ShowMessage( "Folder does not exist!" );
return;
}
Pass = 0;
FindFilesInDirectory ( Dir );
Pass = 1;
FindFilesInDirectory ( Dir );
}
__finally
{
Button1->Enabled = true;
}
}
// Recursion
void TForm1::FindFilesInDirectory (const AnsiString &Dir )
{
if ( Pass == 0 )
FindExtension ( Dir, "*.txt" );
if ( Pass == 1 )
FindExtension ( Dir, "*.~txt" );
WIN32_FIND_DATA filedata;
HANDLE filehandle;
String dir = Dir + "*.*" ;
filehandle = FindFirstFile ( ( Dir + "*.*" ).c_str (), &filedata );
if ( filehandle != INVALID_HANDLE_VALUE )
{
do
{
if ( ( filedata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) != 0
&& String (filedata.cFileName) != "."
&& String (filedata.cFileName) != ".." )
{
FindFilesInDirectory ( Dir + filedata.cFileName + "\\" );
}
Application->ProcessMessages () ;
} while ( FindNextFile ( filehandle, &filedata ) ) ;
FindClose ( filehandle ) ;
}
Application->ProcessMessages() ;
}
void TForm1::FindExtension ( const AnsiString &Path, const AnsiString &Ext )
{
WIN32_FIND_DATA filedata;
HANDLE filehandle ;
filehandle = FindFirstFile ( ( Path + Ext ).c_str(), &filedata ) ;
if ( filehandle != INVALID_HANDLE_VALUE )
{
int Succ;
AnsiString Result = "";
do
{
TListItem *li = ListView1->Items->Add();
AnsiString FullFileName = Path + filedata.cFileName;
li->Caption = ExtractFileName( FullFileName );
if ( filedata.nFileSizeHigh == 0 )
{
li->SubItems->Add ( AnsiString ( (int) ( ( filedata.nFileSizeLow
+ 1023 ) / 1024 ) ) + " K" );
}
else
{
double filesize = ( MAXDWORD * filedata.nFileSizeHigh ) +
filedata.nFileSizeLow;
li->SubItems->Add ( String ( filesize / 1024.0 ) + " K" );
}
switch ( Pass )
{
case 0: li->SubItems->Add ( "Changing+Renaming: *.txt" );
break;
case 1: li->SubItems->Add ( "Renaming: *.~txt ->*.txt" );
break;
default: li->SubItems->Add ( "WTF!" );
break;
}
Succ = GoParseFileName( FullFileName );
Result = "N/A";
switch ( Succ )
{
/*
case 0: Result = "Successful!";
break;
case 1: Result = "Could not rename to a new file name";
break;
case 2: Result = "Could not rename new file name to *.~txt";
break;
case 3: Result = "Could not rename new file name to *.txt";
break;
*/
}
li->SubItems->Add ( Result ) ;
Application->ProcessMessages () ;
} while ( FindNextFile ( filehandle, &filedata ) ) ;
FindClose (filehandle) ;
}
}
int TForm1::GoParseFileName( AnsiString FN )
{
if ( Pass == 0 )
{
AnsiString NewFileName = GiveMeNewFileName( FN );
if ( ! RenameFile( FN, NewFileName ) )
return ( 1 );
if ( ! RenameFile( NewFileName, ChangeFileExt( NewFileName,
".~txt" ) ) )
return ( 2 );
return ( 0 );
}
if ( Pass == 1 )
{
if ( ! RenameFile( FN, ChangeFileExt( FN, ".txt" ) ) )
return ( 2 );
return ( 0 );
}
}
AnsiString TForm1::GiveMeNewFileName( const AnsiString &AFileName )
{
// Expected:
// Monday January 01, 1955.txt
// Wednesday March 27, 1910.txt
AnsiString FN = ExtractFileName( AFileName );
AnsiString FP = ExtractFilePath( AFileName );
AnsiString Year = FN.SubString( FN.Pos( "." )-4, 4 );
AnsiString Day = FN.SubString( FN.Pos( "," )-2, 2 );
// English is not my native language... Please check if I typed correctly
AnsiString Months[12] = { "January", "February", "March", "April", "May",
"June", "July", "August", "September", "October", "November", "December" };
AnsiString Month;
for ( int i=0; i<12; i++ )
{
if ( FN.Pos( Months[i] )>0 )
{
Month = AnsiString( i+1 );
break;
}
}
if ( Month.Length() == 1 )
Month = "0" + Month;
return ( FP + Year + Month + Day + ".txt" );
}
--- H ---
//---------------------------------------------------------------------------
#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ComCtrls.hpp>
#include <Grids.hpp>
#include <ExtCtrls.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TButton *Button1;
TButton *Button2;
TEdit *Edit1;
TPanel *TitlePanel;
TLabel *IzaberiteLabel;
TListView *ListView1;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // Repository headers declarations
int Pass;
void FindExtension (const AnsiString&, const AnsiString&);
void FindFilesInDirectory (const AnsiString& );
int GoParseFileName( AnsiString );
AnsiString GiveMeNewFileName( const AnsiString& );
public: // Repository headers declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
--
Best Regards,
Vladimir Stefanovic