Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MFC Project from Existing Code in One Click

0.00/5 (No votes)
7 Aug 2016 1  
The alternative way of the MFC Project from Existing Code creation

Image 1

Introduction

In the MFC Visual C++ project editor, the command File->New Project->From Existing Code... presents. If you've never used this command, just try. And you should suffer from a lot of stages with the conditions implementation required.

Background

Nevertheless, the possibility of the creation of the copy of the existing project with another name proved useful. It is possible to develop the new features and options on the base of the already developed ones.

And I've developed the simple code provided the creation of the copy of the existing project with another name in one click. The difference from the standard File->New Project->From Existing Code... command is that you may change the properties of the new project as required after its creation.

Using the Code

The basis of my project ProjRename has been initialized with the standard MFC procedure for dialog based application. The procedure is as simple as follows:

  • To select the path containing the project to rename
  • To specify the name of the project (or part of the name) to be changed
  • To specify the new name of the project (or part of the name) to replace the former string
  • And press OK button

The recursive procedure of the project renaming:

void CProjRenameDlg::RenameProject(CString tPath, CString oldName, CString newName)
{  //Recursive procedure to replace the oldName with the newName in the tPath directory 
	PushMyDir(); //remember current directory
_tchdir(tPath);
struct _tfinddata_t fInfo;

//Start the process of the handling all the directories and files in the tPath directory:
intptr_t handle = _tfindfirst(_T("*.*"), &fInfo );
if (handle == -1)
    return;
if(fInfo.name[0] != '.')
if( fInfo.attrib == _A_SUBDIR)
{
	_tgetcwd(str, MAX_PATH);
	CString dString = (CString)str + _T("\\") + (CString)fInfo.name;
	//if directory found inside the current strPath to call this procedure 
    //with the directory found:
	RenameProject(dString, oldName, newName);
	_tchdir(tPath);
}
else
	RenameFileMid(CString(fInfo.name), oldName, newName);

//Continue the process of the handling all the directories and files in the tPath directory:
while(!_tfindnext(handle, &fInfo))
if(fInfo.name[0] != '.')
if( fInfo.attrib == _A_SUBDIR)
{
_tgetcwd(str, MAX_PATH);
CString dString = (CString)str + _T("\\") + (CString)fInfo.name;
//if directory found inside the current strPath 
//to call this procedure with the directory found:
RenameProject(dString, oldName, newName);
_tchdir(tPath);
}
else //if fInfo is the File then replace the oldName 
     //with the newName in the File and in it's name if fit:
	RenameFileMid(fInfo.name, oldName, newName); 
_findclose(handle);

PopMyDir();//restore original directory

if (!m_bFileName) //if Replace in Files' Names CheckBox Control 
				  //not selected do not change Files' and Paths' names  
    return;

_tchdir(tPath);
CString fName = tPath;
int nn = tPath.ReverseFind('\\');
if (nn >= 0)
  fName = tPath.Mid(nn + 1);
//To found if the name of the current path is affected with the replacement;
CString sName = ReplaceToChange(fName, oldName, newName, m_bIgnoreCase, m_bWholeWord);
if (sName != fName)
{
	PushMyDir();
	_tchdir(_T("..\\"));
	_trename(fName, sName);//Rename current directory name with the new one;
	PopMyDir();

}//if (sName != fName)
}

Application Demo Controls

Dialog box menu and some special controls arranged in order to demonstrate the Project name rename:

Image 2

  • Menu File->Open - Selecting the path containing the project to rename
  • Menu File->Any Recent File - Selecting the path former has been changed with this program
  • String to Replace Control - The name of the project (or part of the name) to be changed
  • To Replace with the String Control - The new name of the project (or part of the name) to replace the former string
  • Replace in Files'Names Control - If selected, the former string to be replaced with the new one
  • Case sensitive Control - If selected, the former string to be replaced with the new one just if upper and lower case in the affected string are the same as the template specified
  • Whole Word only Control - If selected, the former string to be replaced with the new one just if the whole word in the affected string are the same as the template specified
  • Extensions Box Control - To show all the extensions of the files subject to replace the former string with the new one inside these folders
  • Ok Button Control - To execute the project rename in the path selected

Points of Interest

The technology provided above seemed as a little bit of a barbarian way of handling projects. Anyway, it is working and I believe some developers should be interested in it.

The project has been developed for MSVS-2015 pro. Nevertheless, it is valid for MSVS-2010 as well.

And this project has been renamed by itself several times during the development of this paper.

Acknowledgements

Recent file handling in the project has been borrowed from the fine CodeProject article, Adding a Recent File List to an MFC dialog based application by PPresedo.

History

  • 7th August, 2016: Initial version

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here