Solved

Renaming all files in a directory

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
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.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

696 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