Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Renaming all files in a directory

Posted on 2002-04-29
2
Medium Priority
?
201 Views
Last Modified: 2013-11-20
Given a directory from the user, I wish to open each file in this directory and append a user-defined string to the filename.

I know how to open the file and rename it, but I do not know how to go through and automatically rename all files in the directory using MFC. How do I do this?

Thanks,
-Dan
0
Comment
Question by:SuperMario
2 Comments
 
LVL 2

Accepted Solution

by:
Hemant_Modak earned 400 total points
ID: 6979686
Hi Dan,
You can accomplish this by using following 2 functions
void CFilelistDlg::GetAllFolders(CString strFilepath)
{
if(strFilepath.Right(2)!= "\\\\")
   strFilepath = strFilepath + "\\*.*";
WIN32_FIND_DATA fd;
BOOL ret = 1;
HANDLE han = 0L;
han = FindFirstFile(strFilepath,&fd);
if( han != INVALID_HANDLE_VALUE)
{          
   while(ret)
   {
     if(han != INVALID_HANDLE_VALUE)
     {
     if((fd.cFileName != CString(".")) && (fd.cFileName != CString("..")))
     {
     if(fd.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
     {
     strFilepath = strFilepath.Left(strFilepath.ReverseFind('\\'));
     strFilepath = strFilepath + "\\" + fd.cFileName;
     m_strFolderList.Add(strFilepath);
     }
     }
    }
    ret = FindNextFile(han,&fd);
   }
}
if(han)
     FindClose(han);
     
}

void CFilelistDlg::GetFiles(CString strFilepath)
{
CString strTempPath;
char strSize[MAX_PATH];
DWORD sz;
strTempPath = strFilepath;
if(strFilepath.Right(2)!= "\\\\")
     strFilepath = strFilepath + "\\*.*";
WIN32_FIND_DATA fd;
BOOL ret = 1;
HANDLE han = 0L;
han = FindFirstFile(strFilepath,&fd);
if( han != INVALID_HANDLE_VALUE)
{
while(ret)
{
   if(han != INVALID_HANDLE_VALUE)
   {
     if((fd.cFileName != CString(".")) && (fd.cFileName != CString("..")))
     {
       if(fd.dwFileAttributes != FILE_ATTRIBUTE_DIRECTORY)
       {
              //Rename the files.
       }
     }
     ret = FindNextFile(han,&fd);
}
}
if(han)
FindClose(han);
}


Hope this helps.

Hemant
0
 
LVL 3

Author Comment

by:SuperMario
ID: 6981363
Wow!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question