如上图所示选择了文件类型后,点击“选择路径”,选择了一个文件夹,然后在列表控件中显示搜到的文件名,想把这些文件名后面添加一个密级,像0: 不改名;1:“公开”等,举例:上下拉电阻的作用.pdf变成上下拉电阻的作用(公开).pdf,让操作者在选择了Combox 下拉列表控件的某一项之后,点击“重命名”按钮后,实现如上的操作。这里还有一个要求就是搜出来文件后,把所有的文件的文件名都修改喽,之后根据需要多选某些特定的文件名,实现对这些文件的文件名的标密,请问实现这些功能应该怎么编程?

解决方案 »

  1.   

    CFile::Rename()
      

  2.   

    MoveFile API
      

  3.   

    为什么不直接生成一个bat批处理文件然后执行它呢?
      

  4.   

    没接触过bat批处理文件,请详细说明一下,怎么使用,我是初学。
      

  5.   

    没接触过bat批处理文件,请详细说明一下,怎么使用,我是初学。
    百度搜“bat批处理改名”
      

  6.   

    这样的问题根本不用编程
    dir 列出文件名
    复制到excel里
    用excel的文本函数
    前面加上ren XXX  新文件名,拖拉
    在贴到.bat文件里面
      

  7.   

    不是有个 vector<FileInfo>fileList;
      

  8.   

    void find (LPCTSTR lpPath,std::vector<FileInfo>&FileList)
     {
    //std::vector<FileInfo>fileList;
        std::string szFind(lpPath);
    WIN32_FIND_DATA FindFileData;
    szFind+=_T("\\*.*");
    HANDLE hFind=::FindFirstFile(szFind.c_str(),&FindFileData);  //这里用到了szFind
    if(INVALID_HANDLE_VALUE==hFind) return;
    do//while(1)       
    {
    if(FindFileData.dwFileAttributes&FILE_ATTRIBUTE_DIRECTORY)   //判断
    {//dir 目录在列表中可以双击打开,不要递归!
    if(FindFileData.cFileName[0]!='.') 
    {
    std::string szPath(lpPath);
    szPath+=_T("\\");
    szPath+=FindFileData.cFileName;
    TRACE1("%s\n",szPath.c_str());
    find(szPath.c_str(),FileList);   //自注:这里小写的fileList不行。这一句是递归。
    }
    }
    else 
            {  std::string strFileName=FindFileData.cFileName;//文件名
     suffixStr=strFileName.substr(strFileName.find_last_of('.')+1);   //这里的后缀就是所有文件类型的后缀,下面的文件是所有类型的文件的显示。
     if(std::regex_match(suffixStr,suffixStr1))
      { FILETIME ft=FindFileData.ftCreationTime;   //取得创建时间
    SYSTEMTIME st={0};
    TCHAR buf[64]={0};
    FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft);    //转换成本地时间
    FileTimeToSystemTime(&ft,&st);     //在这句之前加上上一句,就不用再小时那里+8了。
    sprintf_s(buf,_T("%4d年-%02d月-%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
    std::string strFilePath=lpPath;
    strFilePath+=_T("\\");
    strFilePath+=FindFileData.cFileName;   //文件全路径名
    FileInfo fi;    //自注:结构变量不能代入
    fi.szFileName=strFileName;
    fi.szFilePath=strFilePath;
    fi.szCreateTime=buf;
    TRACE1("%s\n",strFileName.c_str());    //自注:这句是后加上的
    FileList.push_back(fi);  //自注:push_back(fi)是什么意思?
           }
     else if(suffixStr==suffixStr2)
     {
     FILETIME ft=FindFileData.ftCreationTime;   //取得创建时间
    SYSTEMTIME st={0};
    TCHAR buf[64]={0};
    FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft);    //转换成本地时间
    FileTimeToSystemTime(&ft,&st);     //在这句之前加上上一句,就不用再小时那里+8了。
    sprintf_s(buf,_T("%4d年-%02d月-%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
    std::string strFilePath=lpPath;
    strFilePath+=_T("\\");
    strFilePath+=FindFileData.cFileName;   //文件全路径名
    FileInfo fi;    //自注:结构变量不能代入
    fi.szFileName=strFileName;
    fi.szFilePath=strFilePath;
    fi.szCreateTime=buf;
    TRACE1("%s\n",strFileName.c_str());    //自注:这句是后加上的
    FileList.push_back(fi);  //自注:push_back(fi)是什么意思?
     }
     else if(std::regex_match(suffixStr,suffixStr3))
     {
     FILETIME ft=FindFileData.ftCreationTime;   //取得创建时间
    SYSTEMTIME st={0};
    TCHAR buf[64]={0};
    FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft);    //转换成本地时间
    FileTimeToSystemTime(&ft,&st);     //在这句之前加上上一句,就不用再小时那里+8了。
    sprintf_s(buf,_T("%4d年-%02d月-%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
    std::string strFilePath=lpPath;
    strFilePath+=_T("\\");
    strFilePath+=FindFileData.cFileName;   //文件全路径名
    FileInfo fi;    //自注:结构变量不能代入
    fi.szFileName=strFileName;
    fi.szFilePath=strFilePath;
    fi.szCreateTime=buf;
    TRACE1("%s\n",strFileName.c_str());    //自注:这句是后加上的
    FileList.push_back(fi);  //自注:push_back(fi)是什么意思?
     }  
     else if(std::regex_match(suffixStr,suffixStr4))
     {
     FILETIME ft=FindFileData.ftCreationTime;   //取得创建时间
    SYSTEMTIME st={0};
    TCHAR buf[64]={0};
    FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft);    //转换成本地时间
    FileTimeToSystemTime(&ft,&st);     //在这句之前加上上一句,就不用再小时那里+8了。
    sprintf_s(buf,_T("%4d年-%02d月-%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
    std::string strFilePath=lpPath;
    strFilePath+=_T("\\");
    strFilePath+=FindFileData.cFileName;   //文件全路径名
    FileInfo fi;    //自注:结构变量不能代入
    fi.szFileName=strFileName;
    fi.szFilePath=strFilePath;
    fi.szCreateTime=buf;
    TRACE1("%s\n",strFileName.c_str());    //自注:这句是后加上的
    FileList.push_back(fi);  //自注:push_back(fi)是什么意思?
     }
     else if(suffixStr==suffixStr5)
     {
     FILETIME ft=FindFileData.ftCreationTime;   //取得创建时间
    SYSTEMTIME st={0};
    TCHAR buf[64]={0};
    FileTimeToLocalFileTime(&(FindFileData.ftCreationTime),&ft);    //转换成本地时间
    FileTimeToSystemTime(&ft,&st);     //在这句之前加上上一句,就不用再小时那里+8了。
    sprintf_s(buf,_T("%4d年-%02d月-%02d日%02d:%02d:%02d"),st.wYear,st.wMonth,st.wDay,st.wHour/*北京时间*/,st.wMinute,st.wSecond);
    std::string strFilePath=lpPath;
    strFilePath+=_T("\\");
    strFilePath+=FindFileData.cFileName;   //文件全路径名
    FileInfo fi;    //自注:结构变量不能代入
    fi.szFileName=strFileName;
    fi.szFilePath=strFilePath;
    fi.szCreateTime=buf;
    TRACE1("%s\n",strFileName.c_str());    //自注:这句是后加上的
    FileList.push_back(fi);  //自注:push_back(fi)是什么意思?
     }
         }
        }while(::FindNextFile(hFind,&FindFileData));
    // getchar();
     }    
     是对这段程序进行修改,就可实现我前面提到的功能了吗?还是又需要别的程序,请指教。
      

  9.   

     vector<FileInfo>fileList; 是个 全局 变量 ,用于 填充 list,你用 list 也 可以 取 原文件 名 (path+NAME)但 速度慢
    而读写 fileList 要 快得多 !
      

  10.   

      这个全局变量,如何填充list,用list怎么取原文件名(path+NAME),如何读写fileList?
      

  11.   

    从 m_list 取 int nSelectedItem=m_list.GetSelectionMark();//获取第几行
    CString name=m_list.GetItemText(nSelectedItem,1);
    CString ext=name.Right(name.GetLength()-name.ReverseFind('.'));
    CString path=m_list.GetItemText(nSelectedItem,3);
    CStringArray ExeName;
    char fullname[MAX_PATH];
    strcpy(fullname,(LPTSTR)(LPCTSTR)path);
    strcat(fullname,"\\");
    strcat(fullname,(LPTSTR)(LPCTSTR)name);
    afxDump << fullname << "\n";
    fileList see "FillList"
      

  12.   

    这段话什么意思?
    CString ext=name.Right(name.GetLength()-name.ReverseFind('.'));是得到扩展名吧?
    CStringArray  ExeName是干什么的?
      

  13.   

    要改名 先要得到 原文件名,通过上面代码 得到 一个 文件名 (选中项,全部是 i=0到 GetItemCount)
    然后 改名 。
    CString ext=name.Right(name.GetLength()-name.ReverseFind('.'));
    另有它用, 注意 “机密” 在 ext 前。
      

  14.   

    得到一个还是自己任选的一批文件名,GetItemCount在程序里没出现啊?
    CString ext=name.Right(name.GetLength()-name.ReverseFind('.'));干什么用?为什么不是“绝密”在ext前?
      

  15.   

    你不是 要 让 “绝密” 等 新名 在ext 前吗,所以 把 文件名 先 分成 左右 2部分 ,右边 ext 名 不变,左边 名 后 加 “x密”,
    再 合并成 新 文件名 。
      

  16.   

    下面 是 选择 m_list 中 文件(单选或者多选)后 ,点击 “rename“ 按钮// rename 
    void CShowdirDlg::OnButton4() 
    {
    // TODO: Add your control notification handler code here
    POSITION pos = m_list.GetFirstSelectedItemPosition();
    int nSelectedItem = -1;
    while (pos != NULL)
    {
    nSelectedItem = m_list.GetNextSelectedItem(pos);
    CString name  = m_list.GetItemText(nSelectedItem,1);
    CString path  = m_list.GetItemText(nSelectedItem,3);
    CString extame=name.Right(name.GetLength()-name.ReverseFind('.'));
    CString pureName=name.Left(name.ReverseFind('.'));
    CString newName=path;
    newName += "\\";
    newName += pureName;
    newName += "机密";// 增加的
    newName += extame;
    AfxMessageBox(newName);
    }
    }
      

  17.   

    dir 列出文件名后,复制到excel
    假设为A列,然后在B列内用公式B1=“ren ”& A1 & “  ” & A1 &  “(公开)”
    复制出来,粘贴到记事本中,存为bat文件
      

  18.   

    不用对Combox box这个控件编程吗,每个选项都可能需要添加对应的密级到文件名的后面啊。
      

  19.   

    "不用对Combox box这个控件编程吗"
    要 即 newName += "机密";// 增加的
    是 m_cmb 当前 选择项 (m_cmb。GetWindowText(),即 其 edit 的 内容)
      

  20.   

     newName += "机密";// 增加的     这句写在“重命名”按钮的程序里,不用写在Combo box对应的程序里吗?Combo box程序与重命名按钮程序逻辑是上是怎样的联系呢?m_cmb当前选择项是怎么回事,编辑框edit程序应该怎么写?请指教。
      

  21.   

    "m_cmb当前选择项是怎么回事"
    就是用户 选择 密级,你那个 cmb 不要 1.6 ,直接 就是 “机密“  要不然 必须 去除 ‘1“ ’
    用户 选择 密级 后 cmb 的 edit 显示 响应 密级 如 “机密“
    newName += "机密";
    变成 
    newName += m_cmb.GetWindowText() ;//
      

  22.   

    向导要 add 变量 mComboBox (即 IDC_CMB)控件“带着0~5的标号” 要么用 0到5,要么用 "x密“ , 你的 处理 一下 !
      

  23.   

    写在选项里的时候是两个都要,即0:    不更改;1:       公开;2:     内部;3:  ......等等,真正改名时只要改成XXX(内部).pdf; XXX(机密).docx这种格式的标密文件就行了,应该怎么改?
      

  24.   

    0:    不更改 
    0: 有什么用 ?
    不更改 又有 什么用 ?"1:       公开"
    分 左右 2部分 ,取 右部 即可, 分割 标记 是 tab (09)或者 空格 (0x20)
      

  25.   

    是啊,您说的对,这一项的确是没用啊。但分割标记是怎么回事tab(09)或空格(0x20)写在哪里,怎么写?
      

  26.   

    分割 标记 是 tab (09)在 cmb 的 list 中 不可见 使用 空格 (0x20)吧,即 “1:“  与   “公开“ 间 用 空格 分开 
    用 Find (“     ”)或 ReverseFind (' ') 
      

  27.   

    用 Find (“     ”)或 ReverseFind (' ') 是怎么个用法,具体写成代码是什么样子?
      

  28.   

    CString GetSecurityLevel()
    {
    CString cmbName="1:     公开";// 5 spaces !
    int at=cmbName.ReverseFind(' ');
    return cmbName.Right(cmbName.GetLength()-at-1);
    } 调用
    CString sl=GetSecirityLevel();
      

  29.   

    CString GetSecurityLevel()是得到密级对吧?int at=cmbName.ReverseFind(' ');是什么意思?接下来两句是什么意思?调用的sl又是什么意思?请指教。除了“公开”以外的其他密级怎么办?写在哪儿?
      

  30.   

    CString cmbName="1:     公开";// 5 spaces !
    其他 密级 就是 CString cmbName=m_cmb.GetWindowText() ;// "1:     公开";// 5 spaces !
    int at=cmbName.ReverseFind(' ');// c从 右到左 找到 第一个 空格 的 位置 at。CString  sl=GetSecirityLevel();
    newName += sl ;// "机密";
      

  31.   

    要想得到其他的密级只需要把程序由
    CString cmbName="1:     公开";// 5 spaces !
    改为CString cmbName=m_cmb.GetWindowText() ;即可,是吧?
      

  32.   


    // rename
    CString CShowdirDlg::GetSecurityLevel()
    {
    CString cmbName;
    m_Cmb1.GetWindowText(cmbName);//"1:     公开";
    int at=cmbName.ReverseFind(' ');
    return cmbName.Right(cmbName.GetLength()-at-1);

    //
    void CShowdirDlg::OnButton4() 
    {
    // TODO: Add your control notification handler code here
    CString sl=GetSecurityLevel();
    POSITION pos = m_list.GetFirstSelectedItemPosition();
    int nSelectedItem = -1;
    while (pos != NULL)
    {
    nSelectedItem = m_list.GetNextSelectedItem(pos);
    CString name  = m_list.GetItemText(nSelectedItem,1);
    CString path  = m_list.GetItemText(nSelectedItem,3);
    CString extname=name.Right(name.GetLength()-name.ReverseFind('.'));
    CString pureName=name.Left(name.ReverseFind('.'));
    CString newName=path;
    newName += "\\";
    newName += pureName;
    newName += sl;// "机密";// 增加的
    newName += extname;
    AfxMessageBox(path + "\\"+ name + " To " + newName);
    // CFile::Rename(path + "\\"+ name, newName );
    }
    }
      

  33.   

    // rename
    CString CShowdirDlg::GetSecurityLevel()
    {
        CString cmbName;
        m_Cmb1.GetWindowText(cmbName);//"1:     公开";
        int at=cmbName.ReverseFind(' ');
        return cmbName.Right(cmbName.GetLength()-at-1);

    //这个函数是不是就是应该写到我VC2010程序的void XXXDlg:: OnCbnselchangeCombo()处呢?
      

  34.   

    不必,这是取当前选择项,与 当前选择项改变 无关。
    因为 CString sl=GetSecurityLevel(); 是第一句 (不在while内),以后所有list的多选项 会用同一个 密级 改名
      

  35.   

    那我自动生成的  void XXXDlg:: OnCbnselchangeCombo()声明
    {
         //ToD:在此添加控件通知处理程序代码
    }
    这里针对我需要实现的功能,需要写些什么呢?
      

  36.   

    点Combox box 控件的下拉框选项,对应这里m_Cmb1.GetWindowText(cmbName);,m_Cmb1中的GetWindowText的内容为1:     公开;2:    内部;3:     秘密........。等
    还有请问:while以下的语句是什么意思,看不懂啊。
      

  37.   

     while (pos != NULL) 是 检查 m_list 有多少 选择 项, 取出 选择项的 id。
    m_list 属性应该 是 多选。
    用户 操作 如下 , 移动 鼠标 在 list 界面中 选择 任意多 项,(或 再改变 密级),点击 “Rename”, 那么 这批 文件就 同时 改名 到 一个 密级。
      

  38.   

    void CresecretDlg::ListShow(int pageNo)
    {      
        UINT nCount=fileList.size();   //得到控件的行数   总的文件个数。
        UINT i;
    //int pageNo=1;
        Start=pageSize*(pageNo-1);
    End=pageSize*pageNo;
    CString empty;   //难道是这里定义错了?
        CString name;
        CString time;
        CString fpath;
    mlist.LockWindowUpdate(); 
    mlist.DeleteAllItems();     //要想实现翻页效果这句必不可少。
    for(i=Start;i<End;i++)  //改成i=0;i<50也还是不行,这里不是逻辑出了问题,而是其它的问题。
     {  
    if(i>=nCount)break;
    empty.Format(_T("%d"),i+1);     
    int nRow=mlist.InsertItem(i,empty);    //这句话什么意思?
    afxDump<<nRow<<"\n";    //这句对翻页没有影响
    name.Format(_T("%s\n"),fileList[i].szFileName.c_str());
    mlist.SetItemText(nRow,1,name);      //插入列   自注:从这里开始程序就不对了,这句还是对的
    time.Format(_T("%s\n"),fileList[i].szCreateTime.c_str());
        mlist.SetItemText(nRow,2,time);
    fpath.Format(_T("%s\n"),fileList[i].szFilePath.c_str());   //不影响翻页
    mlist.SetItemText(nRow,3,fpath);
     }
        mlist.UnlockWindowUpdate();
    // getchar();
    }
            
    void CresecretDlg::OnCbnSelchangeCombo()
    {
    // TODO: 在此添加控件通知处理程序代码
    }CString CresecretDlg::GetSecurityLevel()
    {
        CString cmbName;
        mComeboBox.GetWindowText(cmbName);//"1:     公开";
        int at=cmbName.ReverseFind(' ');
        return cmbName.Right(cmbName.GetLength()-at-1);
    } void CresecretDlg::OnBnClickedButton4()
    {
    // TODO: 在此添加控件通知处理程序代码
    CString sl=GetSecurityLevel();
        POSITION pos = mlist.GetFirstSelectedItemPosition();
        int nSelectedItem = -1;
        while (pos!=NULL)
        {
            nSelectedItem = mlist.GetNextSelectedItem(pos);
            CString name  = mlist.GetItemText(nSelectedItem,1);
            CString path  = mlist.GetItemText(nSelectedItem,3);
            CString extname=name.Right(name.GetLength()-name.ReverseFind('.'));
            CString pureName=name.Left(name.ReverseFind('.'));
            CString newName=path;
            newName += "\\";
            newName += pureName;
            newName += sl;// "机密";// 增加的
            newName += extname;
            AfxMessageBox(path + "\\"+ name + " To " + newName);
    //        CFile::Rename(path + "\\"+ name, newName );
        }
    }
    这两段程序中都有CString变量,变量相同,有没有冲突啊?我这些程序这么写被证实有问题,因为没有在文件名后面添加(公开),(内部),(秘密)等密级,请问是怎么回事。
      

  39.   

    "这两段程序中都有CString变量,变量相同,有没有冲突啊"
    概念错误 !
    CString变量 ! CString 是 某变量 的 类型,不是 变量 , 如 int 是 整数 类型, 不是 整数 变量 !
    mComeboBox.GetWindowText(cmbName);
     检查  cmbName 是否 空 :     if(cmbName.IsEmpty()) return CString(错误);在 rename 前 要 选择 mComeboBox
      

  40.   

    对,您说的对,CString是变量类型,我就是想知道这两段程序中的同样变量类型的同名变量,用起来有没有冲突?rename前,我选择mComeboBox的下拉框的选项了,但是Rename后就是没显示XXX(公开).pdf,XXX(秘密).doc等类似的标密文件。检查  cmbName 是否 空 :     if(cmbName.IsEmpty()) return CString(错误);有什么用?