How do I add an integer to a string for file renaming purposes? 
Author Message
 How do I add an integer to a string for file renaming purposes?

Probably a dumb question but I want to add a sequential number to a filename
so I can rename all files in a dir with a file prefix.  Can anyone help me
please?


Fri, 16 Sep 2005 05:29:47 GMT  
 How do I add an integer to a string for file renaming purposes?

Quote:
> Probably a dumb question but I want to add a sequential number to a
filename
> so I can rename all files in a dir with a file prefix.  Can anyone help me
> please?

CString::Format()


Fri, 16 Sep 2005 06:46:43 GMT  
 How do I add an integer to a string for file renaming purposes?
Either

UINT num = 5;
CString fn = _T("");
fn.format(_T("s%d.ext"),num);

or

UINT num = 5;
char buf[50];
sprintf(_T("s%d.exe"),num);

whichever you prefer.
See the documentaion for sprintf, and format to see what the %d means and
how to adjust it for adding bits like leading zeros

hth
Rob



Sat, 17 Sep 2005 03:48:07 GMT  
 How do I add an integer to a string for file renaming purposes?
But this has to be done internally.  I want the number added to the
filename(the string)  so I can rename all files when I click a button.

Quote:


> > Probably a dumb question but I want to add a sequential number to a
> filename
> > so I can rename all files in a dir with a file prefix.  Can anyone help
me
> > please?

> CString::Format()



Fri, 16 Sep 2005 22:48:03 GMT  
 How do I add an integer to a string for file renaming purposes?
I know this is probably real stupid but here's what I'm trying to do.

I want to fill a listbox with the files of a specific folder.  Rename all
files with a user-defined file prefix by clicking a rename button.  And I
want to add 001, 002, 003.... until the last file is renamed.  I've got
everything written and working but I don't know how to add the sequential
numbers to the files when renaming.  I believe format is used for output to
a control but I'm not outputting to a control, I'm renaming directly on the
disk.



Quote:
> Either

> UINT num = 5;
> CString fn = _T("");
> fn.format(_T("s%d.ext"),num);

> or

> UINT num = 5;
> char buf[50];
> sprintf(_T("s%d.exe"),num);

> whichever you prefer.
> See the documentaion for sprintf, and format to see what the %d means and
> how to adjust it for adding bits like leading zeros

> hth
> Rob



Fri, 16 Sep 2005 23:50:14 GMT  
 How do I add an integer to a string for file renaming purposes?

Quote:


> > Either

> > UINT num = 5;
> > CString fn = _T("");
> > fn.format(_T("s%d.ext"),num);

> > or

> > UINT num = 5;
> > char buf[50];
> > sprintf(_T("s%d.exe"),num);

> > whichever you prefer.
> > See the documentaion for sprintf, and format to see what the %d means
and
> > how to adjust it for adding bits like leading zeros

> > hth
> > Rob



Quote:
> numbers to the files when renaming.  I believe format is used for output
to
> a control but I'm not outputting to a control, I'm renaming directly on
the
> disk.

CString::Format() changes only the content of a CString. It seems what you
need is just to print an integer out to a memory block and then concatenate
it to another string. So either CString::Format() or sprintf() will do the
job.

regards
Lei



Sat, 17 Sep 2005 01:12:34 GMT  
 How do I add an integer to a string for file renaming purposes?
Thanks for you time.  I'll give it a shot.

Quote:


in

> > > Either

> > > UINT num = 5;
> > > CString fn = _T("");
> > > fn.format(_T("s%d.ext"),num);

> > > or

> > > UINT num = 5;
> > > char buf[50];
> > > sprintf(_T("s%d.exe"),num);

> > > whichever you prefer.
> > > See the documentaion for sprintf, and format to see what the %d means
> and
> > > how to adjust it for adding bits like leading zeros

> > > hth
> > > Rob


> > numbers to the files when renaming.  I believe format is used for output
> to
> > a control but I'm not outputting to a control, I'm renaming directly on
> the
> > disk.

> CString::Format() changes only the content of a CString. It seems what you
> need is just to print an integer out to a memory block and then
concatenate
> it to another string. So either CString::Format() or sprintf() will do the
> job.

> regards
> Lei



Sat, 17 Sep 2005 01:32:11 GMT  
 How do I add an integer to a string for file renaming purposes?
This works for me:

CString CMyFileOp::CreateNumberedFileName(const CString &strBase, int iNum, int iNumLen)
{
        if (iNum<0)
                return strBase;

        CString strDrive,strPath,strName,strExt;
        _splitpath(strBase,strDrive.GetBuffer(_MAX_DRIVE),strPath.GetBuffer(_MAX_DIR),
                strName.GetBuffer(_MAX_FNAME),strExt.GetBuffer(_MAX_EXT));

        CString strRet;
        strRet.Format("%s%s%s%0*d%s",strDrive,strPath,strName,iNumLen,iNum,strExt);
        return strRet;

Quote:
}

By given the input:
strBase=c:\somedir\somefile.ext
iNum=2
iNumLen=3

It produces the output string;
c:\somedir\somefile002.ext

It could easily be changed to put the number in front of the file instead.

This only makes a string, it doesn't rename any files. To rename all files in a dir you can enumerate all files in the
dir (use recursion if you also want to rename files in subdirs) with the CFileFind class.

To get a CStringList with all files in a dir (including subdirs if you want) I use this:

BOOL CMyFileOp::GetAllFiles(CStringList &strListAllFiles, CString strPath, BOOL bRecurse,BOOL bIncludeDirs,CString
strMatch)
{
        strPath.TrimRight("\\");
        strPath+="\\";

        CStringList strListDirs;
        CFileFind cFF;

        BOOL bFoundFiles=FALSE;
        BOOL bFoundDirs=FALSE;

        BOOL bWorking=cFF.FindFile(strPath+strMatch);
        while(bWorking){
                bWorking=cFF.FindNextFile();
                if(cFF.IsDots())
                        continue;
                if (cFF.IsDirectory()){
                        strListDirs.AddTail(cFF.GetFilePath()+'\\');
                        bFoundDirs=TRUE;
                } else {
                        strListAllFiles.AddTail(cFF.GetFilePath());
                        bFoundFiles=TRUE;
                }
        }
        cFF.Close();

        if (bFoundDirs && (bRecurse || bIncludeDirs)){
                POSITION pos=strListDirs.GetHeadPosition();
                CString strDir;
                while(pos!=NULL){
                        strDir=strListDirs.GetNext(pos);
                        if (bRecurse){
                                if (GetAllFiles(strListAllFiles,strDir,TRUE,bIncludeDirs))
                                        bFoundFiles=TRUE;
                        }
                        if (bIncludeDirs){
                                strDir.TrimRight("\\");
                                strListAllFiles.AddTail(strDir);
                                bFoundFiles=TRUE;
                        }
                }
        }
        return bFoundFiles;

Quote:
}

Set bRecurse to TRUE if you want to recurse any subdirs.
Set bIncludeDirs if sub dir names should be included in the list.

Use SHFileOperation to do the actual renaming for you. I use a wrapper class called CShellFileOp by Michael Dunn, which
can be found on www.codeproject.com

First use GetAllFiles to build a CStringList with filenames, then make a new list with CreateNumberedFileName and finaly
pass all files with AddDestFile and AddSourceFile to CShellFileOp and let it do the renaming (or copying, or deleting)
for you.

Both my functions are static member functions of a utility class for files and directories. They can be global (c-type)
as well.

Ruben

Quote:

>Probably a dumb question but I want to add a sequential number to a filename
>so I can rename all files in a dir with a file prefix.  Can anyone help me
>please?



Sun, 18 Sep 2005 07:30:16 GMT  
 How do I add an integer to a string for file renaming purposes?
Thanks a lot for you time!!!  I got it to work with this simplw solution:

{
  int iCount;
  int iLastCount;
  int iPrefixNum = 001;

  // Get the number of files in the listbox
  iLastCount = m_FileList.GetCount();
  m_Progress.SetPos(0);
  // Create an array to go through all files in the folder
  for (iCount = iLastCount; iCount > 0; iCount--)
  {
   // Get the current file name
   m_FileList.GetText((iCount - 1), m_CurrentFileName);

   // Format prefix number as text
   CString text;
   text.Format("% .3d", iPrefixNum);

   // Rename the files
   rename(m_CurrentFileName, (m_strPrefix + text + "." + extension));
   iPrefixNum = iPrefixNum + 1;
   m_Progress.SetRange( static_cast<short>(0),
static_cast<short>(iLastCount) );
   m_Progress.SetStep(1);
   m_Progress.StepIt();
  }
 }


Quote:
> This works for me:

> CString CMyFileOp::CreateNumberedFileName(const CString &strBase, int
iNum, int iNumLen)
> {
> if (iNum<0)
> return strBase;

> CString strDrive,strPath,strName,strExt;

_splitpath(strBase,strDrive.GetBuffer(_MAX_DRIVE),strPath.GetBuffer(_MAX_DIR
),
Quote:
> strName.GetBuffer(_MAX_FNAME),strExt.GetBuffer(_MAX_EXT));

> CString strRet;

strRet.Format("%s%s%s%0*d%s",strDrive,strPath,strName,iNumLen,iNum,strExt);
Quote:
> return strRet;
> }

> By given the input:
> strBase=c:\somedir\somefile.ext
> iNum=2
> iNumLen=3

> It produces the output string;
> c:\somedir\somefile002.ext

> It could easily be changed to put the number in front of the file instead.

> This only makes a string, it doesn't rename any files. To rename all files

in a dir you can enumerate all files in the
Quote:
> dir (use recursion if you also want to rename files in subdirs) with the
CFileFind class.

> To get a CStringList with all files in a dir (including subdirs if you
want) I use this:

> BOOL CMyFileOp::GetAllFiles(CStringList &strListAllFiles, CString strPath,

BOOL bRecurse,BOOL bIncludeDirs,CString
Quote:
> strMatch)
> {
> strPath.TrimRight("\\");
> strPath+="\\";

> CStringList strListDirs;
> CFileFind cFF;

> BOOL bFoundFiles=FALSE;
> BOOL bFoundDirs=FALSE;

> BOOL bWorking=cFF.FindFile(strPath+strMatch);
> while(bWorking){
> bWorking=cFF.FindNextFile();
> if(cFF.IsDots())
> continue;
> if (cFF.IsDirectory()){
> strListDirs.AddTail(cFF.GetFilePath()+'\\');
> bFoundDirs=TRUE;
> } else {
> strListAllFiles.AddTail(cFF.GetFilePath());
> bFoundFiles=TRUE;
> }
> }
> cFF.Close();

> if (bFoundDirs && (bRecurse || bIncludeDirs)){
> POSITION pos=strListDirs.GetHeadPosition();
> CString strDir;
> while(pos!=NULL){
> strDir=strListDirs.GetNext(pos);
> if (bRecurse){
> if (GetAllFiles(strListAllFiles,strDir,TRUE,bIncludeDirs))
> bFoundFiles=TRUE;
> }
> if (bIncludeDirs){
> strDir.TrimRight("\\");
> strListAllFiles.AddTail(strDir);
> bFoundFiles=TRUE;
> }
> }
> }
> return bFoundFiles;
> }

> Set bRecurse to TRUE if you want to recurse any subdirs.
> Set bIncludeDirs if sub dir names should be included in the list.

> Use SHFileOperation to do the actual renaming for you. I use a wrapper

class called CShellFileOp by Michael Dunn, which
Quote:
> can be found on www.codeproject.com

> First use GetAllFiles to build a CStringList with filenames, then make a

new list with CreateNumberedFileName and finaly
Quote:
> pass all files with AddDestFile and AddSourceFile to CShellFileOp and let

it do the renaming (or copying, or deleting)
Quote:
> for you.

> Both my functions are static member functions of a utility class for files

and directories. They can be global (c-type)

- Show quoted text -

Quote:
> as well.

> Ruben


> >Probably a dumb question but I want to add a sequential number to a
filename
> >so I can rename all files in a dir with a file prefix.  Can anyone help
me
> >please?



Sun, 18 Sep 2005 23:55:24 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. how to add integers to strings?

2. adding integer to string

3. adding integers to a string

4. Checking an integer in a string against the max integer limit in limits.h

5. What Is the Purpose of a Map File?

6. What is the purpose of DAO253x.tlb files?

7. general purpose file-compression lib/dll

8. how to add a string in the new line of the file

9. Need API for doing a Route add

10. how can I add submenu to IE popup menu like NetAnts have done

11. String operations that add string terminator

12. Compile errors after adding strings to string table

 

 
Powered by phpBB® Forum Software