若若's profile芳草天涯PhotosBlogListsMore Tools Help

Blog


    October 11

    C++批量修改文件名

    从驴子上拖cd,最恼火就是乱七八糟的文件名和专门存放文件名的文本文件闹分裂。
    索性写个程序,从txt读取文件名列表,然后修改文件夹下所有文件的文件名。
    小程序仅限mp3文件。
     
     1 #include "stdafx.h"
     2 #include <afx.h>
     3 #include <iostream>
     4 #include <fstream> 
     5 #include <string>
     6 using namespace std;
     7 
     8 int RenameFromTXT(string InputFolderPath, string InputTXTName)
     9 {    
    10     int result=-1;
    11     CString cstr, CNewName;
    12     string str, OldName, NewName;
    13     CFileFind finder;   
    14     CString FolderPath = InputFolderPath.c_str();
    15     bool ifFind = finder.FindFile(FolderPath + "*.mp3"); 
    16 
    17     if(ifFind == false){
    18         cout<<"Folder not found!"<<endl;
    19         return -1;
    20     }
    21 
    22     ifstream inputNewName(InputTXTName.c_str());
    23     if(!inputNewName){
    24         cout<<"TXT-file not found!"<<endl;
    25         return -1;
    26     }
    27 
    28     while(ifFind)   
    29     {   
    30         ifFind = finder.FindNextFile();  
    31         cstr = finder.GetFilePath();
    32         USES_CONVERSION;
    33         OldName = W2A(cstr);
    34         cout<<"old path: "<<OldName.c_str()<<endl;
    35         getline(inputNewName, str);
    36         cout<<"new name: "<<str.c_str()<<endl;
    37         cstr = FolderPath + str.c_str();       
    38         CNewName = cstr + ".mp3";
    39         NewName = W2A(CNewName);
    40         cout<<"new path: "<<NewName<<endl;
    41         result = rename(OldName.c_str(), NewName.c_str());
    42         if( result == 0)
    43             cout<<"File successfully renamed!"<<endl;
    44         else{
    45             cout<<"Error renaming file: Permission denied"<<endl;
    46             cout<<"The new filename may contain \ / : ? \" < > |"<<endl;
    47             return -1;
    48         }
    49         cout<<endl;
    50         result=-1;
    51     }
    52     finder.Close();
    53     inputNewName.close();
    54     return 0;
    55 }
    56 
    57 int main(int argc, char **argv)
    58 {    
    59     string input1 = "D:\\Musik\\V.A.-.[Eurovision.Song.Contest.Moscow.2009.(2CD).(2009)].专辑.(MP3)\\";
    60     string input2 = "D:\\Musik\\NewNameList.txt";
    61     RenameFromTXT(input1, input2);
    62 
    63     system("pause");
    64     return 0;
    65 }

    main中调用时,只要输入待改文件名的文件夹路径,和存放新名字的文本文件路径和名字就行。 

    其中NewNameList.txt内容格式如下:

    101. Susanna Georgi — Le Teva Decisio (Get A Life) [Andorra]
    102. Kejsi Tola — Carry Me In Your Dreams [Albania]
    103. Inga & Anush — Jan Jan [Armenia]
    104. AySel & Arash — Always [Azerbaijan]
    105. Regina — Bistra Voda [Bosnia & Herzagovina]
    106. Copycat — Copycat [Belgium]
    107. Krasimir Avramov — Illusion [Bulgaria]
    108. Petr Elfimov — Eyes That Never Lie [Belarus]
    109. Lovebugs — Highest Heights, The [Switzerland]
    110. Christina Metaxa — Firefly [Cyprus]
    111. Gipsy.cz — Aven Romale [Czech Republic]
    112. Alex Swings Oscar Sings! — Miss Kiss Kiss Bang [Germany]
    113. Brinck — Believe Again [Denmark]
    114. Urban Symphony — Randajad [Estonia]
    115. Soraya — La Noche Es Para Mi (The Night Is For Me) [Spain]
    116. Waldo's People — Lose Control [Finland]
    117. Patricia Kaas — Et S'il Falliat Le Faire [France]
    118. Jade Ewen — It's My Time [United Kingdom]
    119. Sakis Rouvas — This Is Our Night [Greece]
    120. Igor Cukrov feat. Andrea — Lijepa Tena (Beautiful Tena) [Croatia]
    121. Zoli Adok — Dance With Me [Hungary]
    201. Sinead Mulvey & Black Daisy — Et Cetera [Ireland]
    202. Noa & Mira Awad — There Must Be Another Way [Israel]
    203. Yohanna — Is It True [Iceland]
    204. Sasha Son — Love [Lithuania]
    205. Intars Busulis — Probka [Latvia]
    206. Nelly Ciobanu — Hora Din Moldova (Dancing Moldova) [Moldova]
    207. Andrea Demirovic — Just Get Out Of My Life[Montenegro]
    208. Next Time — Neshto Shto Ke Ostane [FYR Macedonia]
    209. Chiara — What If We [Malta]
    210. Toppers — Shine [Netherlandsnew wave Eurovision 2009 mix]
    211. Alexander Rybak — Fairytale [Norway]
    212. Lidia Kopania — I Don't Wanna Leave [Poland]
    213. Flor-De-Lis — Todas As Ruas Do Amor [Portugal]
    214. Elena — Balkan Girls, The [Romania]
    215. Marko Kon & Milaan — Cipela [Serbia]
    216. Anastasia Prikhodko — Mamo [Russia]
    217. Malena Ernman — La Voix [Sweden]
    218. Nela Pociskova & Kamil Mikulcik — Let' Tmou [Slovakia]
    219. Quartissimo — Love Symphony [Slovenia]
    220. Hadise — Dum Tek Tek [Turkey]
    221. Svetlana Loboda — Be My Valentine! (Anti-crisis Girl) [Ukraine]

     

    一秒钟全改了,很爽Smiley mit geöffnetem Mund

    Comments (5)

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Qiuwrote:
    嘿嘿,我都只留音频文件。
    Oct. 12
    若若 杜wrote:
    只要顺序一致就ok
    Oct. 12
    Micky - -wrote:
    你怎么对号入座啊 会不会把歌改成其他的歌名??
    Oct. 12
    若若 杜wrote:
    汗,菜鸟乱写点东西吓唬人罢了。。。
    不是分裂,是我不喜欢有些文件的命名方式。
    Oct. 11
    un Swrote:
    真专业。。。用电驴会啥分裂啊??没碰到过啊
    Oct. 11

    Trackbacks

    The trackback URL for this entry is:
    http://kassandrarr.spaces.live.com/blog/cns!77C720CEE14EB4C5!2685.trak
    Weblogs that reference this entry
    • None