Solved

Renaming all files in a directory

Posted on 2002-04-29
2
196 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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Whole sheet autoscrub still needed 19 53
ASCII Non-Printable characters/codes and their HTML equivalents 6 143
sumDigits challenge 9 172
dog bark java program 15 120
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.

763 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