VC++之文件高级操作之修改文件名

来源:互联网 发布:修复相片软件 编辑:程序博客网 时间:2024/05/29 02:22

VC++之文件高级操作之修改文件名

一、创建对话框应用程序

二、编辑对话框资源


三、添加函数


四、添加代码

void CCFileDlg::OnCancel() 
{
// TODO: Add extra cleanup here
CDialog::OnOK(); 
// CDialog::OnCancel();
}

void CCFileDlg::OnOK() 
{
// TODO: Add extra validation here
CString OldName,NewName,path;
CFileDialog file(TRUE,"文件","*.*",OFN_HIDEREADONLY,"FILE(*.*)|*.*||",NULL);
    if(file.DoModal()==IDOK)
{
   path=file.GetPathName();
   GetDlgItem(IDC_EDIT_OLD)->SetWindowText(path);
        GetDlgItem(IDC_EDIT_NEW)->GetWindowText(NewName);
   CString temp1,temp2;
   temp1=temp2=path;
   int position=temp1.Find("\\"); //最后一个“//”放置文件名
   while(position>0)
   {
    temp1=temp1.Right(temp1.GetLength()-1-position);
    position=temp1.Find("\\");
   }
      GetDlgItem(IDC_EDIT_NEW2)->SetWindowText(temp1);
   temp2=temp2.Left(temp2.GetLength()-temp1.GetLength());
   temp2+=NewName;   //修改文件名
   CFile::Rename(path,temp2); //重命名

// CDialog::OnOK();
}

五、编译

六、运行


七、函数说明

       1、CFile::Rename函数声明

       static void Rename(LPCTSTR lpszOldName,LPCTSTR lpszNewName)

       lpszOldName:原文件名。

       lpszNewName:文件的新名称。

       功能:重命名某一特定文件。

       2、CFile::GetLength函数声明

       int GetLength()

       功能:获取某字符串长度。

       3、CFile::Find函数声明

        int Find(LPCTSTR lpszSub)const

        lpszSub:指向某子字符串的指针。

        功能:于字符串内寻找匹配某子字符串。返回子字符串在字符串中的位置。

1 0