C++ Builder

La Isla Bonita



Rename File Extensions in a given Directory
Rename File Extensions in a given Directory


Description

This example illustrates how to rename file extensions for all files located in a given directory and its subdirectories.

The Rename_FileExtensions() function uses recursion to access all the subdirectories of the specified directory and to rename all the files in all the subdirectories of the specified directory.

Files

RenameFileExtensions.cpp

#include <vcl.h>
#pragma hdrstop
#include "RenameFileExtensions.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{
}
//---------------------------------------------------------------------------

String ExtractBaseName(String File)
{
    String Base;
    int i = 1;

    while (true)
    {
       if (i > File.Length())
       {
          break;
       }

       if (File[i] == '.' )
       {
          break;
       }
       else
       {
         Base += File[i];
       }
       
       i++;
    }

    return Base;
}

void Rename_FileExtensions(String Path, String Ext)
{
    if ( Path[Path.Length()] != '\\' )
         Path += String("\\");

    String mask = ( Path + "*.*" );
    WIN32_FIND_DATA fd = {0};
    HANDLE h = FindFirstFile( mask.c_str(), &fd );

    if( h != INVALID_HANDLE_VALUE )
    {
        while( FindNextFile( h, &fd ) )
        {
            String File = fd.cFileName;

            if( File == "." || File == ".." )
                continue;

            if( fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
            {
                //FILE ATTRIBUTE IS A DIRECTORY:
                // Recursive call
                Rename_FileExtensions(Path + File, Ext);
            }
            else
            {
                //FILE ATTRIBUTTE IS NOT A DIRECTORY:
                if ( !RenameFile(Path + File, Path + ExtractBaseName(File) + Ext) )
                     ShowMessage("RenameFile() Failed");
            }
        }

        FindClose( h );                              //Win 32 API
    }
    else
    {
        ShowMessage("Error: FindFirstFile() returned an Invalid Handle");
    }
}
//------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//Example of Usage
   
   Rename_FileExtensions("c:\\TestDir\\", ".123");
}
//---------------------------------------------------------------------------

RenameFileExtensions.h

#ifndef RenameFileExtensionsH
#define RenameFileExtensionsH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:	// IDE-managed Components
        TButton *Button1;
        void __fastcall Button1Click(TObject *Sender);
private:	// User declarations
public:		// User declarations
        __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif

Homepage

Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.