[C++] 批量删除文件名多余的一个相同字符串
2014-01-27 15:46
633 查看
使用C++方法:
// ++++++++
#include <string>
#include <io.h>
#include <iostream>
using namespace std;
bool change_name(const char* char_path,
const char* char_del)
{
string path="";
string path_main="";
string find_str="";
path.append(char_path);
find_str.append(char_del);
cout<<"Now,you find file path is :"<<path.c_str()<<endl;
path_main.append(path.c_str());
path.append("/*.*");
string path_old="";
string path_new="";
string file_name="";
string find_result="";
string temp_name="";
string file_exe="";
string file_pre="";
_finddata_t file;
bool bFind=false;
bool bFile=false;
long lf;
//输入文件夹路径
if((lf = _findfirst(path.c_str(), &file))==-1)
cout<<"Not Found!"<<endl;
else
{
//输出文件名
cout<<"file name list:"<<endl;
while(_findnext( lf, &file)==0)
{
bFile =false;
bFind =false;
path_old="";
path_new="";
file_name="";
find_result="";
temp_name="";
file_pre="";
file_exe="";
file_name.append(file.name);
path_new.append(path_main.c_str());
path_new.append("\\");
if(file_name.find("..")!= string::npos)
{
continue;
}
//cout<<file.name<<endl;
path_old.append(path_main.c_str());
path_old.append("\\");
path_old.append(file.name);
//cout<<path_old.c_str()<<endl;
int iPos_dot=0;
iPos_dot =file_name.find_last_of(".");
if(iPos_dot<=0)
{
bFile =false;
}
else
{
bFile = true;
}
if(false == bFile)
{
continue;
}
file_exe.append(file_name.substr(iPos_dot,file_name.length()));
temp_name.append(file_name.substr(0,iPos_dot));
int ilen = file_name.length();
int ilen_find = temp_name.length() - find_str.length();
if(temp_name.find(find_str) != string::npos)
{
bFind=true;
}
if(ilen_find>0 && (true==bFind))
{
find_result.append(temp_name.substr(ilen_find,find_str.length()));
file_pre.append(temp_name.substr(0,ilen_find));
if(find_result.find(find_str.c_str())!= string::npos)
{
//cout<<"\r\n find! \r\n"<<endl;
path_new.append(file_pre.c_str());
path_new.append(file_exe.c_str());
cout<<"Old file"<<path_old.c_str()<<endl;
cout<<"New file"<<path_new.c_str()<<endl;
//check path_new if exist?
int access_rs=0;
access_rs = access(path_new.c_str(), 0) ;
if(access_rs == 0)
//if ((fopen(path_new.c_str(),"r"))!=0)
{
cout<<"Files has existed!"<<path_new.c_str()<<endl;
}
else
{
rename(path_old.c_str(),path_new.c_str());
}
}
}
else
{
cout<<"Not find any file!"<<endl;
}
}
}
_findclose(lf);
//cin.get();
return true;
}
void OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString sz_Path=_T("");
CString sz_Del=_T("");
GetDlgItemText(IDC_EDIT1,sz_Path);
GetDlgItemText(IDC_EDIT2,sz_Del);
string str_path="";
str_path.append(sz_Path);
cout<<str_path<<endl;
string str_del="";
str_del.append(sz_Del);
change_name(str_path.c_str(),str_del.c_str());
MessageBox("Successfully!",_T("title"),MB_OK);
}
//--------------
在VS2010上面测试没问题
// ++++++++
#include <string>
#include <io.h>
#include <iostream>
using namespace std;
bool change_name(const char* char_path,
const char* char_del)
{
string path="";
string path_main="";
string find_str="";
path.append(char_path);
find_str.append(char_del);
cout<<"Now,you find file path is :"<<path.c_str()<<endl;
path_main.append(path.c_str());
path.append("/*.*");
string path_old="";
string path_new="";
string file_name="";
string find_result="";
string temp_name="";
string file_exe="";
string file_pre="";
_finddata_t file;
bool bFind=false;
bool bFile=false;
long lf;
//输入文件夹路径
if((lf = _findfirst(path.c_str(), &file))==-1)
cout<<"Not Found!"<<endl;
else
{
//输出文件名
cout<<"file name list:"<<endl;
while(_findnext( lf, &file)==0)
{
bFile =false;
bFind =false;
path_old="";
path_new="";
file_name="";
find_result="";
temp_name="";
file_pre="";
file_exe="";
file_name.append(file.name);
path_new.append(path_main.c_str());
path_new.append("\\");
if(file_name.find("..")!= string::npos)
{
continue;
}
//cout<<file.name<<endl;
path_old.append(path_main.c_str());
path_old.append("\\");
path_old.append(file.name);
//cout<<path_old.c_str()<<endl;
int iPos_dot=0;
iPos_dot =file_name.find_last_of(".");
if(iPos_dot<=0)
{
bFile =false;
}
else
{
bFile = true;
}
if(false == bFile)
{
continue;
}
file_exe.append(file_name.substr(iPos_dot,file_name.length()));
temp_name.append(file_name.substr(0,iPos_dot));
int ilen = file_name.length();
int ilen_find = temp_name.length() - find_str.length();
if(temp_name.find(find_str) != string::npos)
{
bFind=true;
}
if(ilen_find>0 && (true==bFind))
{
find_result.append(temp_name.substr(ilen_find,find_str.length()));
file_pre.append(temp_name.substr(0,ilen_find));
if(find_result.find(find_str.c_str())!= string::npos)
{
//cout<<"\r\n find! \r\n"<<endl;
path_new.append(file_pre.c_str());
path_new.append(file_exe.c_str());
cout<<"Old file"<<path_old.c_str()<<endl;
cout<<"New file"<<path_new.c_str()<<endl;
//check path_new if exist?
int access_rs=0;
access_rs = access(path_new.c_str(), 0) ;
if(access_rs == 0)
//if ((fopen(path_new.c_str(),"r"))!=0)
{
cout<<"Files has existed!"<<path_new.c_str()<<endl;
}
else
{
rename(path_old.c_str(),path_new.c_str());
}
}
}
else
{
cout<<"Not find any file!"<<endl;
}
}
}
_findclose(lf);
//cin.get();
return true;
}
void OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
CString sz_Path=_T("");
CString sz_Del=_T("");
GetDlgItemText(IDC_EDIT1,sz_Path);
GetDlgItemText(IDC_EDIT2,sz_Del);
string str_path="";
str_path.append(sz_Path);
cout<<str_path<<endl;
string str_del="";
str_del.append(sz_Del);
change_name(str_path.c_str(),str_del.c_str());
MessageBox("Successfully!",_T("title"),MB_OK);
}
//--------------
在VS2010上面测试没问题
相关文章推荐
- [删除字符串中多余的空格]给定字符串,删除开始和结尾处的空格,并将中间的多个连续的空格合并成一个
- 急救啊,c++的一个字符串中删除指定的字符串,
- C++和C代码:删除字符串中所有前导空格和所有后导空格,单词之间最多只留一个空格
- 删除字符串中多余的空格(只保留一个空格)
- 定义一个字符串类DelSameStr,从左到右对字符串中每个字符删除其后所有相同的字符,只留下第一次出现的那一个
- C++中删除float转字符串后尾部多余的0
- 删除一个字符串中相同字符
- 用java写关于删除一个字符串的字符以及删除一个字符串与另一个字符串中相同字符的问题
- 定义一个字符串类DelSameStr,从左到右对字符串中每个字符删除其后所有相同的字符,只留下第一次出现的那一个。例如,若字符串为”cocoon”,删除重复出现的字符后,其结果是字符串”con”
- C++_USACO_求一个字符串的从开头字母开始,直到发现某一不相同字母时,这个子串的长度
- word中批量删除多余空行用C++实现
- 已知线性表中元素以值递增有序排列,并以单链表作为存储结构,设计一个算法,删除表中值相同的多余元素
- C++_USACO_求一个字符串的连续相同字符的最大长度
- 删除字符串里多余的空格,只保留一个空格
- C++ 将一个文件里面的相同的一个字符串替换成另一个字符串
- 删除一个字符串中相同的字符
- 文本中多余字符串的批量删除和替换
- 删除字符串中多余的空格]给定字符串,删除开始和结尾处的空格,并将中间的多个连续的空格合并成一个
- 将一字符串从小到大输出并删除多余相同的字符例如aaaabbbcc11122,输出12abc
- C++中不同指针指向同一个字符串,然而地址相同