?
Solved

Renaming all files in a directory

Posted on 2002-04-29
2
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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 …
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Suggested Courses

719 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