最近遇到一个以日期时间为文件名的重命名问题,本来用CFile写了一堆的代码,还不是很稳定,用CopyFile()很容易实现,MSDN 上解释是这样,不懂百度,
BOOL WINAPI CopyFile( __in LPCTSTR lpExistingFileName, //源文件的位置 __in LPCTSTR lpNewFileName, //目标文件的位置 __in BOOL bFailIfExists //true/false, if是true,如果文件存在,则调用函数失败,if是false,如果文件存在,则覆盖存在函数 );
解释到此,直接上函数,自己的经验:
SYSTEMTIME tm; GetLocalTime(&tm); //读取当前时间 CString strTime,strTime1,strPicName; strTime.Format( "%04d年 %d月 %02d日 %02d时 %02d分 %02d秒 %03d毫秒 ", tm.wYear,tm.wMonth,tm.wDay, tm.wHour,tm.wMinute,tm.wSecond, tm.wMilliseconds); strTime1.Format( "%04d年 %d月 %02d日", tm.wYear,tm.wMonth,tm.wDay); CString srcPath="rec.jpg"; //这张图片我放在工程,文件下,什么位置,自己修改 CString descPath= _T("D:\\")+strTime1+"\\"; // 目标位置,以日期为文件夹名称 MakeSureDirectoryPathExists(descPath); //查看文件夹是否存在,不存在就创建一个 CString Filepath=descPath+strTime+".jpg"; //我自己需要的文件名 ,看自己的需要,自己修改 CopyFile(srcPath, Filepath,true); //不解释了 return 0;
个人经验,仅供参考。