C++ Builder处理文件名大写、小写、编号的问题。

    技术2022-05-11  58

    //---------------------------------------------------------------------------#define NO_WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 #include <shlobj.h> #include <vcl.h> #pragma hdrstop

    #include "Unit1.h"#include "idglobal.hpp"//---------------------------------------------------------------------------#pragma package(smart_init)#pragma link "CGAUGES"#pragma resource "*.dfm"TForm1 *Form1;TStringList *pList = new TStringList();//---------------------------------------------------------------------------__fastcall TForm1::TForm1(TComponent* Owner)        : TForm(Owner){}//---------------------------------------------------------------------------

    void __fastcall TForm1::Button1Click(TObject *Sender){  String this_file_name;  String original_path; //记录所选文件的原始路径名  pList->Clear();  Memo1->Clear();  Memo2->Clear();  CGauge1->Progress=0;  OpenDialog1->Options.Clear();  OpenDialog1->Options << ofAllowMultiSelect << ofFileMustExist;  OpenDialog1->FilterIndex = 2; // start the dialog showing all files  Memo1->Clear();  if (OpenDialog1->Execute())  {    for (int I = 0; I < OpenDialog1->Files->Count; I ++)    {      this_file_name=OpenDialog1->Files->Strings[I];      pList->Add(this_file_name);

           }  }

      //下面对读入的文件名称进行处理  //首选判断用户是否选择了文件  if (pList->Count==0) goto out;  //如果用户没有选择文件,则跳出下步处理程序  CGauge1->MaxValue=pList->Count;  BitBtn1->Enabled=true;  BitBtn2->Enabled=true;

      original_path=ExtractFilePath(pList->Strings[0]);  Edit1->Text=original_path;  pList->Sorted = true;

      for(int I=0; I<pList->Count; I++)  {    Memo1->Lines->Add(pList->Strings[I]);    }  Memo1->Lines->Add("---------------------------------------------------------------------

    ------------------------------");  Memo1->Lines->Add(IntToStr(pList->Count)+"个文件成功导入!") ;

      out:}//---------------------------------------------------------------------------void __fastcall TForm1::Button2Click(TObject *Sender){  Memo1->Clear();  Memo2->Clear();  Memo1->Lines->Add("选择待处理的文件...");  Memo2->Lines->Add("处理后结果...");  Edit1->Text="默认为原目录...";  CGauge1->Progress=0;  BitBtn1->Enabled=false;  BitBtn2->Enabled=false;  Label1->Caption="0";  Label3->Caption="0";}//---------------------------------------------------------------------------void __fastcall TForm1::BitBtn2Click(TObject *Sender){  //首先读取pList中的文件列表  String this_file;  String this_ex;  String original_path=ExtractFilePath(pList->Strings[0]);  String output_path=String(Edit1->Text);  CGauge1->Progress=0;  Label3->Caption=pList->Count; //一共选中了多少个文件

      if(RadioButton1->Checked==true) //如果把文件名改为小写  {   for(int I=0; I<pList->Count; I++)    {     this_file=output_path+LowerCase(ExtractFileName(pList->Strings[I]));     //判断是否是原目录,如果输出目录为原目录,则执行改名操作     if(strcmp(original_path.c_str(),output_path.c_str())==0)       RenameFile(pList->Strings[I],this_file);     else     CopyFileTo(pList->Strings[I],this_file);

         CGauge1->Progress=I+1;     Label1->Caption=I+1;     Memo2->Lines->Add(this_file);    } //end for (int I=0; I<pList->Count; I++)    Memo2->Lines->Add("-------------------------------------------------------------------

    --------------------------------");    Memo2->Lines->Add(IntToStr(CGauge1->Progress)+"个文件名转换成功! ");  } //end  if(RadioButton1->Checked==true)

       if(RadioButton2->Checked==true) //如果把文件名改为大写  {   for(int I=0; I<pList->Count; I++)  {     this_file=output_path+UpperCase(ExtractFileName(pList->Strings[I]));     //判断是否是原目录,如果输出目录为原目录,则执行改名操作     if(strcmp(original_path.c_str(),output_path.c_str())==0)       RenameFile(pList->Strings[I],this_file);     else     CopyFileTo(pList->Strings[I],this_file);

         CopyFileTo(pList->Strings[I],this_file);     CGauge1->Progress=I+1;     Label1->Caption=I+1;     Memo2->Lines->Add(this_file);    }     Memo2->Lines->Add("-----------------------------");     Memo2->Lines->Add(IntToStr(CGauge1->Progress)+"个文件名转换成功! ");  }

     if(RadioButton3->Checked==true)   //如果处理为虚拟专家软件需要的格式 {   int jpg_cnt=0;   for(int I=0; I<pList->Count; I++)  {      this_ex=LowerCase(ExtractFileExt(pList->Strings[I]));      if(this_ex.Pos("jp")>0) //如果扩展名中含有jp,即以.jpg,.jpeg,.jpe为扩展名      {        this_file=output_path+IntToStr(jpg_cnt+1)+".jpg";        CopyFileTo(pList->Strings[I],this_file);        CGauge1->Progress=jpg_cnt+1;        Label1->Caption=jpg_cnt+1;        Memo2->Lines->Add(this_file);        jpg_cnt++;       } //end  if(this_ex.Pos("jp")>0)     } //end for(int I=0; I<pList->Count; I++)    Memo2->Lines->Add("-----------------------------");    Memo2->Lines->Add(IntToStr(CGauge1->Progress)+"个文件名转换成功! "+IntToStr(pList-

    >Count-jpg_cnt)+"个非JPEG文件未转换." );    jpg_cnt=0;  }  //end if(RadioButton3->Checked==true)

      if(RadioButton4->Checked==true)   //如果处理为虚拟专家软件需要的格式 {  for(int I=0; I<pList->Count; I++)  {        this_ex=LowerCase(ExtractFileExt(pList->Strings[I]));        this_file=output_path+IntToStr(I+1)+this_ex;        CopyFileTo(pList->Strings[I],this_file);        CGauge1->Progress=I+1;        Label1->Caption=I+1;        Memo2->Lines->Add(this_file);    } //end for(int I=0; I<pList->Count; I++)    Memo2->Lines->Add("-----------------------------");    Memo2->Lines->Add(IntToStr(CGauge1->Progress)+"个文件名转换成功! ");   }  //end if(RadioButton4->Checked==true)

      BitBtn1->Enabled=false;  BitBtn2->Enabled=false;  }//---------------------------------------------------------------------------void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action){   delete pList;        }//---------------------------------------------------------------------------bool __fastcall TForm1::MyBrowserDir(HANDLE hWin, LPSTR lpCaption, LPSTR lpDir, LPSTR

    lpDispName){    BROWSEINFO bi;    LPITEMIDLIST pidl;     LPMALLOC pShellMalloc;     bool bRet = false;     // SHBrowseForFolder returns a PIDL. The memory for the PIDL is     // allocated by the shell. Eventually, we will need to free this     // memory, so we need to get a pointer to the shell malloc COM     // object that will free the PIDL later on.     if(SHGetMalloc(&pShellMalloc) == NO_ERROR)     {      // if we were able to get the shell malloc object,      // then proceed by initializing the BROWSEINFO stuct      memset(&bi, 0x00, sizeof(bi));      bi.hwndOwner = hWin; // Owner window      bi.pidlRoot = 0; // root folder      bi.pszDisplayName = lpDispName; // return display name      bi.lpszTitle = lpCaption; // label caption      bi.ulFlags = BIF_RETURNONLYFSDIRS; // config flags      bi.lpfn = 0; // callback function      // execute the browsing dialog      pidl = SHBrowseForFolder(&bi);      // pidl will be null if they cancel the browse dialog.      // pidl will be not null when they select a folder      if(pidl)      {          // try to convert the pidl to a display string          // return is true if success          if(SHGetPathFromIDList(pidl, lpDir))          bRet = true;          // use the shell malloc com object to free the pidl.          // then call Relasee to signal that we don't need          // the shell malloc object anymore          pShellMalloc->Free(pidl);      }      pShellMalloc->Release();     }     return bRet; }//------------------------------------------------------------------------------

    void __fastcall TForm1::BitBtn1Click(TObject *Sender){  char lpDir[MAX_PATH];     char lpDispName[MAX_PATH];     bool bRet = MyBrowserDir(Handle, "请选择一个文件夹:", lpDir, lpDispName);     if(bRet)      //ShowMessage(String("选择的文件夹是: ") + String(lpDir) + String("/r/n")+ String("显

    示名称是: ") + String(lpDispName));     Edit1->Text=String(lpDir)+"//";    else     ShowMessage("没有选择文件夹!");         }//---------------------------------------------------------------------------

    void __fastcall TForm1::Button3Click(TObject *Sender){  ShowMessage("/t    聊城大学教育传播技术学院/n/t        http://ect.lcu.edu.cn/n /n本程序专

    为三维全景制作-环视专家软件制作/n /n环视专家图片要求:/n    1.图片必须是JPEG格式,并以小写

    jpg做为扩展名/n    2.图片必须按1.jpg,2.jpg,3.jpg...格式命名");}//------------------------------------------------------------ 


    最新回复(0)