void CChangeExtNameDlg::OnButton1() 
{
// TODO: Add your control notification handler code here
    CFileDialog fileDialog(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT,NULL,this);   OPENFILENAME m_ofn;
  //char buffer[1000];
  //m_ofn.lpstrFile=buffer;
  //m_ofn.nMaxFile=1000;
  if(fileDialog.DoModal()==IDOK)
  {
POSITION pos=fileDialog.GetStartPosition();
while(pos)
{
m_strPathName=fileDialog.GetNextPathName(pos); CFile file(m_strPathName,CFile::modeReadWrite);
m_strTitle=file.GetFileTitle();
m_strName=file.GetFileName();

CString strPath=m_strPathName;
        int i=strPath.ReverseFind('\\');
CString strPath=strPath.Left(i);//得到文件路径(不包括文件名) CString strName=m_strName;
CString strTitle=strName.Left(3);//得到文件名

CString m_strExt=strName.Right(3);//的到文件扩展名
//CString strOldName=strPathName;
//CString strNewName=path+"\\"+ext+".pdg"; CString strOldName=m_strName;//原文件名
CString strNewName=ext+".pdg";//新文件名
file.Rename(strOldName,strNewName);//有异常发生(在存取一未命名文件时发生共享违例)

//::MoveFileEx (strOldName,strNewName,MOVEFILE_REPLACE_EXISTING);//没有异常,但没有效果

}
}
}如果我用CFileDialog得到几个变量的话,函数没有错误,但只能一次改一个,现在我想一次选中文件夹中的文件一起改名.高手帮忙看看那里错拉.先谢谢了!我想将100.001改成001.pdg

解决方案 »

  1.   

    // file.Rename(strOldName,strNewName);//有异常发生(在存取一未命名文件时发生共享违例)file.Close();
    CFile::Rename(strOldName,strNewName);
      

  2.   

    我关闭了file,还是不行,当执行到
    CFile::Rename(strOldName,strNewName);//有异常发生(对一未命名的文件存取被拒绝)要是用
    ::MoveFileEx(strOldName,strNewName,MOVEFILE_REPLACE_EXISTING);
    可以执行,但是在一次选择多个文件时,好像最多只能选20个文件,不知道那里还有错误,再请帮忙看一下。
      

  3.   

    CFile::Rename 
    static void PASCAL Rename( LPCTSTR lpszOldName, LPCTSTR lpszNewName );
    throw( CFileException );ParameterslpszOldNameThe old path.lpszNewNameThe new path.ResThis static function renames the specified file. Directories cannot be renamed. This is equivalent to the REN command.Example//example for CFile::Rename
    extern char* pOldName;
    extern char* pNewName;
    TRY
    {
        CFile::Rename( pOldName, pNewName );
    }
    CATCH( CFileException, e )
    {
        #ifdef _DEBUG
            afxDump << "File " << pOldName << " not found, cause = "
                << e->m_cause << "\n";
        #endif
    }
    END_CATCHCFile Overview |  Class Members |  Hierarchy Chart
    --------------------------------------------------------------------------------
    Send feedback to MSDN.Look here for MSDN Online resources.
      

  4.   

    CString strOldName="c:\\sa.exe";
    CString strNewName="c:\\sa2.exe";
    CFile::Rename(strOldName,strNewName);是上面这样调用的下面这句根本不要
    CFile file(m_strPathName,CFile::modeReadWrite);
      

  5.   

    MSDN上说得很清楚Rename是一个静态函数
      

  6.   

    snsins(不再信仰,不再坚持) 这个我是知道的,对静态函数也可以用类对象调用吧。MSDN上的我也看过,但在此处就是不行,不知为何。另外,如果我不用CFile file(m_strPathName,CFile::modeReadWrite);
    我怎么能获得文件的路径和文件名呢?我在多选的情况下,CFileDialog类的函数得不到的,不知我说的是否正确,请指教。
      

  7.   

    void CChangeExtNameDlg::OnButton1() 
    {
    // TODO: Add your control notification handler code here
        CFileDialog fileDialog(TRUE,NULL,NULL,OFN_ALLOWMULTISELECT,NULL,this); OPENFILENAME m_ofn;
    //char buffer[10000];
    //m_ofn.lpstrFile=buffer;
    //m_ofn.nMaxFile=10000;
    if(fileDialog.DoModal()==IDOK)
    {
    POSITION pos=fileDialog.GetStartPosition();
    while(pos)
    {
    m_strPathName=fileDialog.GetNextPathName(pos);
    CFile file(m_strPathName,CFile::modeReadWrite);
    m_strTitle=file.GetFileTitle();
    m_strName=file.GetFileName();
    file.Close(); CString strPath=m_strPathName;
        int i=strPath.ReverseFind('\\');
    strPath=strPath.Left(i);//得到文件路径(不包括文件名) CString strName=m_strName;
    CString strTitle=strName.Left(3);//得到文件名
    m_strExt=strName.Right(3);//的到文件扩展名 //CString strOldName=m_strPathName;
    //CString strNewName=strPath+"\\"+m_strExt+".pdg"; CString strOldName=m_strName;//原文件名
    CString strNewName=m_strExt+".pdg";//新文件名
    CFile::Rename(strOldName,strNewName);//有异常发生

    //::MoveFileEx(strOldName,strNewName,MOVEFILE_REPLACE_EXISTING);//没有异常 }
    }
    }将上面的函数加到基于对话框的程序中,添加已m_开头的成员数据,帮忙调试一下。谢了!
    我关闭了file,还是不行,当执行到
    CFile::Rename(strOldName,strNewName);//有异常发生(对一未命名的文件存取被拒绝)要是用
    ::MoveFileEx(strOldName,strNewName,MOVEFILE_REPLACE_EXISTING);
    可以执行,但是在一次选择多个文件时,好像最多只能选20个文件,不知道那里还有错误,再请帮忙看一
      

  8.   

    CFileDialog不是有个GetNextPathName的方法吗?
      

  9.   

    看看这里
    可能对你有帮助
    这里推荐在WIN32下用MoveFileExhttp://www.sources.ru/cpp/mfc/t8263.htm
      

  10.   

    snsins(不再信仰,不再坚持) :if(fileDialog.DoModal()==IDOK)
    {
    POSITION pos=fileDialog.GetStartPosition();
    while(pos)
    {
    m_strPathName=fileDialog.GetNextPathName(pos);

    CString strPath=m_strPathName;
    int nLength=strPath.GetLength();
    int i=strPath.ReverseFind('\\');
    m_strName=strPath.Right(nLength-i-1);
    int j=strPath.ReverseFind('.');
    m_strExt=strPath.Right(nLength-j-1);
    CString strOldName=m_strName;
    CString strNewName=m_strExt+".pdg";
    ::MoveFileEx(strOldName,strNewName,MOVEFILE_REPLACE_EXISTING);//??在一次选择多个文件时,最多只能选20个文件,不知道那里还有错误,再请帮忙看一下.
    }
    }
      

  11.   

    感谢snsins(不再信仰,不再坚持) 多次关注,我会开贴加分的。谢谢!