class Tool
{
public :
//srcfileFolder下格式名为srcFormat的全部改为后缀formatName 输入范例"C:\\Users\\Smart\\Desktop\\d","*.jpg","JPG"
void reName(const char * srcFileFolder,const char * srcFormat,const char * formatName);
///srcfileFolder下格式名为srcFormat的文件路径全部保存到nameList.txt中
void getNameList(const char * srcFileFolder,const char * srcFormat,const char * nameList);
//如果srcFileFolder目录下文件名存在空格,则去掉。
void delSpaceKey(const char * srcFileFolder );
//得到srcFileFolder下每个txt文本的第二行并保存到新目录下
void getTxtSecondLine(const char * srcFileFolder,const char * dstFileFolder);
};
#include <afx.h>
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void Tool::reName(const char * srcFileFolder,const char * srcFormat,const char * formatName)
{
CFileFind ffindsrc;
CString sfindwhat;
sfindwhat.Format(_T("%s\\%s"),srcFileFolder,srcFormat);
const char * oldname = 0;
bool start = ffindsrc.FindFile(sfindwhat); //寻找此类文件
while (start)
{
start = ffindsrc.FindNextFile();
if (ffindsrc.IsDirectory() || ffindsrc.IsDots())//找到的是仍目录或 .
continue;
//oldname = ffi
CString cts = ffindsrc.GetFilePath();
oldname = cts.GetBuffer();//CSTRing to char 若oldname = (LPSTR)(LPCTSTR) ffindsrc.GetFilePath();oldname将是原因是返回后对象析构了,所以指针的指向 被回收了,当然也可以先用CTS过渡一下再强制转换)
int len = strlen(oldname);
int len1 = strlen(formatName);
int lastdots = 1;
while(oldname[len-lastdots] != '.')//找出拓展名所占字符
lastdots++;
char newname[64];
int i,j;
for( i = 0,j = 0; i < len-lastdots+len1+1 && i < 64;i++)
{
if(i < len-lastdots+1)
newname[i] = oldname[i];
else
newname[i] =formatName[j++];
}
newname[i] = '\0';
rename(oldname,newname);//重命名
}
}
void Tool::getNameList( const char * srcFileFolder,const char * srcFormat,const char * nameList)
{
CFileFind cfile;
CString cstring;
cstring.Format(_T("%s\\%s"),srcFileFolder,srcFormat);
ofstream f(nameList);//打开输出流
bool start = cfile.FindFile(cstring);
while(start)
{
start = cfile.FindNextFile();
if(cfile.IsDirectory() || cfile.IsDots())
continue;
f<<cfile.GetFilePath()<<endl;//含路径文件名 GetFileName不含路径 GetFileTitle不含后缀
}
f.close();
}
void Tool:: delSpaceKey(const char * srcFileFolder )
{
CFileFind cfilefind;
CString cstring;
CString cs1;
cstring.Format(_T("%s\\*.*"),srcFileFolder); ///这里一定要是这样的格式才表示找所有文件
bool start = cfilefind.FindFile(cstring);
while(start)
{
start = cfilefind.FindNextFileA();
if(cfilefind.IsDirectory() || cfilefind.IsDots())
continue;
cs1 = cfilefind.GetFilePath();
const char * pch = cs1.GetBuffer();//CSTring to char*
char newname[64];
int fast,slow,len;
len = strlen(pch);
fast = slow = 0;
while(fast < len)
{
if (' '!= pch[fast]) //delete spacekey
newname[slow++] = pch[fast++];
else
fast++;
}
newname[slow] = '\0';
rename(pch,newname);//重命名*/
}
}
void Tool::getTxtSecondLine(const char * srcFileFolder,const char * dstFileFolder)
{
CFileFind cfilefind;
CString cstring,cs;
cstring.Format(_T("%s\\*.txt"),srcFileFolder);
bool start = cfilefind.FindFile(cstring);
while(start)
{
start = cfilefind.FindNextFile();
if( cfilefind.IsDirectory() || cfilefind.IsDots())
continue;
cs = cfilefind.GetFilePath();
const char * pchsrc;
char pchdst[64];
pchsrc = cs.GetBuffer();
int fast,slow,len;
len = strlen(pchsrc);
int len1 = strlen(dstFileFolder);
fast = slow = 0;
while(slow<len1)
{
pchdst[slow] = dstFileFolder[slow];
slow++;
}
pchdst[slow++] = '\\';
pchdst[slow++] = 'N';
fast = len - 1;
while(pchsrc[fast--] != '\\');
fast += 2;
while(fast<len)
pchdst[slow++] = pchsrc[fast++];
pchdst[slow] = '\0';
string s;
ifstream rf(pchsrc);
ofstream wf(pchdst);
if(!rf || !wf)
return;
getline(rf,s);
getline(rf,s);
wf<<s<<endl;
rf.clear();
rf.close();
wf.clear();
wf.clear();
}
}