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)
{
PushMyDir();
_tchdir(tPath);
struct _tfinddata_t fInfo;
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;
RenameProject(dString, oldName, newName);
_tchdir(tPath);
}
else
RenameFileMid(CString(fInfo.name), oldName, newName);
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;
RenameProject(dString, oldName, newName);
_tchdir(tPath);
}
else
RenameFileMid(fInfo.name, oldName, newName);
_findclose(handle);
PopMyDir();
if (!m_bFileName)
return;
_tchdir(tPath);
CString fName = tPath;
int nn = tPath.ReverseFind('\\');
if (nn >= 0)
fName = tPath.Mid(nn + 1);
CString sName = ReplaceToChange(fName, oldName, newName, m_bIgnoreCase, m_bWholeWord);
if (sName != fName)
{
PushMyDir();
_tchdir(_T("..\\"));
_trename(fName, sName);
PopMyDir();
}
}
Application Demo Controls
Dialog box menu and some special controls arranged in order to demonstrate the Project name rename:
- 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