我正在尝试重命名文件夹中的文件,这些文件夹具有相同的名称但扩展名不同。我到处寻找解决方案。到目前为止,我已经编写了以下代码,但它没有重命名文件..任何想法将不胜感激??
void someFunction(){
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
_splitpath(m_DirTree.GetCurrentDir(), drive, dir, fname, ext);
CString fpath(_T(m_DirTree.GetCurrentDir()));
CString filenameWithoutExtension = fpath.Left(fpath.ReverseFind(_T('.')));
CString fileContainer = fpath.Left(fpath.ReverseFind(_T('\\'))); // eg. L:\PowerStar 5 Demo II\Programs\Demo\Programs
fpath = fileContainer + _T("\\"); // eg. L:\PowerStar 5 Demo II\Programs\Demo\Programs\
CString fileN = fpath + fname + _T(".***"); // eg. L:\PowerStar 5 Demo II\Programs\Demo\Programs\TestX.***
WIN32_FIND_DATA data;
HANDLE h = FindFirstFile(fpath, &data);
if( h!=INVALID_HANDLE_VALUE )
{
do
{
CString hpath = (fpath + myPRGName + _T(".***")); // eg. L:\PowerStar 5 Demo II\Programs\Demo\Programs\TestY.***
rename(fileN, hpath);
} while(FindNextFile(h,&data));
}
else {
cout << "Error: No such folder." << endl;
}
FindClose(h);
}
}
}
答案 0 :(得分:0)
以下代码可能适合您。
DirectoryInfo d = new DirectoryInfo(@"C:\DirectoryToAccess");
FileInfo[] infos = d.GetFiles();
foreach(FileInfo f in infos)
{
File.Move(f.FullName, f.FullName.ToString().Replace("abc","xyz"));
}
&#13;