修改: http://blog.csdn.net/jiangredsheep/archive/2005/07/19/429302.aspx 的目录操作,改为文件名批量修改。
如果想保留原来的文件,可以采用copyFile函数,如下代码:
#include
<
iostream
>
#include
<
string
>
#include
<
string
.h
>
#include
<
string
>
#include
<
afx.h
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
using
namespace
std;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
char
*
GetAnsicString(
const
CString
&
s)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int nSize = (s.GetLength() + 1)*sizeof(TCHAR);
//char *pAnsicString = new char[nSize];
char * pAnsicString = (char *)malloc(nSize*sizeof(char));
ZeroMemory(pAnsicString, nSize);
wcstombs(pAnsicString, s, nSize);
return pAnsicString;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
void
Recurse(LPCTSTR pstr)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
CFileFind finder;
HANDLE hSearch;
DWORD dwAttrs;
TCHAR szDirPath[] = TEXT("C:/Documents and Settings/null/Desktop/fadg0/video/");
TCHAR szNewPath[MAX_PATH];
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
BOOL fFinished = FALSE;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// build a string with wildcards
CString strWildcard(pstr);
strWildcard += _T("/*.*");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// start working for files
BOOL bWorking = finder.FindFile(strWildcard);
CString src,dst,filetitle,filename;
int result;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
while (bWorking)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
bWorking = finder.FindNextFile();
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// skip . and .. files; otherwise, we'd
// recur infinitely!
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (finder.IsDots())
continue;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// if it's a directory, recursively search it
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
if (finder.IsDirectory())
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
CString str = finder.GetFilePath();
cout << (LPCTSTR) str << endl;
Recurse(str);
continue;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
src = finder.GetRoot();
filetitle = finder.GetFileTitle();
filename = finder.GetFileName();
if (filename != filetitle)
continue;
src = src + _T("/") + filetitle;
dst = src + _T(".jpg");
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
//采用重命名部分
char * asicsrc = GetAnsicString(src);
char * asicdst = GetAnsicString(dst);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
result = rename(asicsrc,asicdst);
if (result == 0 )
puts ( "File successfully renamed" );
else
perror( "Error renaming file" );
free(asicdst);
free(asicsrc);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// 采用拷贝部分
//if (CopyFile(src, dst, FALSE))
//{
// dwAttrs = GetFileAttributes(src);
// if (dwAttrs==INVALID_FILE_ATTRIBUTES)
// return;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
// if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
// {
// SetFileAttributes(dst,
// dwAttrs | FILE_ATTRIBUTE_READONLY);
// }
//}
//else
//{
// printf("Could not copy file. ");
// return;
//}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
}
finder.Close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int
nCmdShow)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
Recurse(_T("C:/Documents and Settings/null/Desktop/fadg0/video/"));
}
如果不需要保留,可以直接采用rename函数实现。