读取文件夹下的文件,并且修改文件名

来源:互联网 发布:mac隐藏文件夹打不开 编辑:程序博客网 时间:2024/06/06 03:45

最近用5sing在手机上听歌,听歌过程中,歌曲并不会下下来,而是作为缓存存在手机的某个文件夹下,因为听了很多结果缓存很大,有几百兆,大概有100首缓存音乐吧,后来我发现了整个文件夹,发现文件都是以*.mp3.kgtmp为结尾的文件,每个都有几兆那么大。但是我将后缀的.kgtmp去掉就变成了mp3文件,可以用酷我打开直接播放了,和正常的音乐文件一样,只是有些不完整。我改了几首就觉得一直改太累了,很烦。而我又不想删,所以想来可以用程序来做这件事,就写了这个程序。

#include <iostream>#include <io.h>  //整个头文件可以帮忙读取文件夹,_find开头的函数或类型名#include <string>#include <stdio.h>  //这个头文件dos下的rename命令,必须使用rename命令,因为读出的文件名为const类型,不能被修改using namespace std;int main(){    _finddata_t file;    long lf;    if((lf=_findfirst("E:\\dirtest\\cache\\*.kgtmp",&file))==-1)  //这个是特定的要读取的文件夹,就是我存放缓存的文件夹拷到电脑里的        cout<<"not found"<<endl;    else{        cout<<"file name list:"<<endl;        while(_findnext(lf,&file)==0)  //这个函数可以帮忙一直读完所有文件        {            cout<<file.name<<endl;  //输出文件名            string s="E:\\dirtest\\cache\\";   //这个是想要写进的文件夹            string oldname=s;string newname=s;  //旧文件名和新文件名包括路径s            oldname+=file.name;            newname=oldname;            newname.erase(newname.length()-6,newname.length()-1);  //把.kgtmp给去掉            cout<<oldname<<endl;            cout<<newname<<endl;            char *old;            char *news;            old=const_cast<char*>(oldname.c_str());  //const转为非const            news=const_cast<char*>(newname.c_str());            rename(old,news);  旧文件名用新文件名替换掉就可以了        }    }    _findclose(lf);    return 0;}



程序运行一次,一下就改了90多首歌,还是蛮有成就的,哈哈!

特别要注意,文件夹路径名和文件名其实是一起的,只要输出old和news就可以发现。

最后附张改好的图。


0 0
原创粉丝点击