我用SHFileOperation给指定的文件改名,程序如下:SHFILEOPSTRUCT shFileOpStruct;
shFileOpStruct.pFrom=strFrom;
shFileOpStruct.pTo=strTo; //
shFileOpStruct.fFlags =FOF_RENAMEONCOLLISION; //
shFileOpStruct.wFunc=FO_RENAME;int nReturn=::SHFileOperation(&shFileOpStruct);注:strFrom/strTo是CString类,
strFrom是要改的文件的全路径,strTo是被改后的全路径名.如:strFrom="c:\\a\\a.txt",strTo="c:\\a\\b.txt")
但执行结果返回nRetrun=31,
请高手指导一下.

解决方案 »

  1.   

    to:nbgyf(梅花峰) 
    CFile::Rename()
    能实现文件夹改名吗?
      

  2.   

    rename("d:\\Folder1","d:\\Folder2");
    rename("d:\\File1.txt","d:\\File2.doc");
      

  3.   

    pFrom
    Address of a buffer to specify one or more source file names. These names must be fully qualified paths. Standard Microsoft® MS-DOS® wild cards, such as "*", are permitted in the file-name position. Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. (注意)Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of pFrom. 
      

  4.   

    1、没有设定shFileOpStruct.hwnd (可以为0)
    2、shFileOpStruct.pFrom指向的字符串必须以2个0字符结尾,而不是普通
    的以1个0字符结尾的字符串。为什么了?这是因为,shFileOpStruct.pFrom
    可以包含多个文件名,每个文件名之间用1个0字符分隔,所以结束时需要2个
    0字符。所以你不能直接使用CString类,而需要定义一块内存,确保字符串
    以2个0字符结尾。
      

  5.   

    rename(OldName,NewName);
    经过测试对文件和文件夹都可以。
      

  6.   

    如果不是批量重命名的,用MoveFile就可以了。