Solved

Renaming all files in a directory

Posted on 2002-04-29
2
192 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 100 total points
Comment Utility
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
Comment Utility
Wow!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
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 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.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now