⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 communit.pas

📁 delphi写的对学习delphi初级入门的数据编程很有用。是用用来查找硬盘mp3文件
💻 PAS
字号:
unit CommUnit;

interface

uses windows,forms,SysUtils,Classes,MP3info,U_Dm,ExtCtrls,Controls,StrUtils,Dialogs;
type
//MP3Info = Record
 MP3Protery = Record
  ID: integer;
  Name: string;
  DiskName: string;
  OtherInfo: string;
  Autor: string;
  Path: string;
  Size: Integer;
  IndexN: Integer;
  IndexC: string;
  FileExt:string;
 end;
//function GetAllDirMP3(aMP3Protery: MP3Protery):boolean;
function GetFileSize(sFName:string):Longint;
function bdSearchFile(mainpath: string): Boolean;
function IsValidDir(SearchRec: TSearchRec): Boolean;
procedure ShowProgressDlg(Msg: string=''; H: integer=50; W: integer=240);
procedure ChgProgressMsg(Msg: string);
procedure HideProgressDlg;
procedure CloseProgressDlg;
var
 ProgressDlg: TForm;
 MP: TPanel;
 file_count: integer;
implementation
procedure ShowProgressDlg(Msg: string=''; H: integer=50; W: integer=240);
begin
  if Assigned(ProgressDlg) then exit;
  Application.MainForm.Enabled:=False;
  ProgressDlg:=TForm.Create(nil);
  MP:=TPanel.Create(nil);

  with ProgressDlg do
  begin
    FormStyle:=fsStayOnTop;
    BorderStyle:=bsNone;
    Position:=poDesktopCenter;
    //Color:=clGreen;
    Font.Name:='宋体';
    Font.Size:=9;
    //Font.Color:=clYellow;
    Width:=W;
    Height:=H;
    Cursor:=crHourglass;
  end;
  with MP do
  begin
    Parent:=ProgressDlg;
    BevelInner:=bvRaised;
    Color:=ProgressDlg.Color;
    Align:=alClient;
    if Msg='' then Caption:='正在处理,请稍后...' else Caption:=Msg;
  end;
  ProgressDlg.Show; 
  Application.ProcessMessages;
end;
procedure ChgProgressMsg(Msg: string);
begin
  MP.Caption:=Msg;
  Application.ProcessMessages;
end;

procedure HideProgressDlg;
begin
  ProgressDlg.Hide;
end;

procedure CloseProgressDlg;
begin
  try
    if MP=nil then exit;
    freeandnil(MP);
    freeandnil(ProgressDlg)
  finally
    Application.MainForm.Enabled:=True;
    screen.Cursor:=crdefault;
  end;
end;

function GetFileSize(sFName:string):Longint;
var
 SearchRec: TSearchRec;
begin
try
 try
 if FindFirst(sFName,faAnyFile,SearchRec) = 0 then
 Result:=SearchRec.Size
 else
 Result:=-1;
 except
 end;
finally
 FindClose(SearchRec);
end;
end;

function IsValidDir(SearchRec: TSearchRec): Boolean;
begin
  if (SearchRec.Attr = 16) and (SearchRec.Name <> '.') and
     (SearchRec.Name <> '..') then
     Result := True
  else
    Result := False;
end;

function bdSearchFile(mainpath: string): Boolean;
var
  i:integer;
  Found:Boolean;
  subdir1:TStrings;
  searchRec:TsearchRec;
  aMP3Protery: MP3Protery;
  aMP3Info: TMP3Info;
begin
  found:=false;
  aMP3Info := TMP3Info.Create(nil);
  subdir1:=TStringList.Create;//字符串列表必须动态生成
  ShowProgressDlg('处理文件开始');
//找出所有下级子目录。
  try
  try

   if mainpath[Length(mainpath)] <> '\' then
       mainpath := mainpath + '\';
    if (FindFirst(mainpath+'*.*', faDirectory, SearchRec)=0) then
    begin
      if IsValidDir(SearchRec) then
        subdir1.Add(SearchRec.Name);
      while (FindNext(SearchRec) = 0) do
      begin    //查找当前目录。
        application.ProcessMessages;
        if IsValidDir(SearchRec) then
          subdir1.Add(SearchRec.Name)
        else
        begin
         //ShowProgressDlg('处理文件开始');
         if (not AnsiContainsText(SearchRec.Name,'.mp3')) and (not AnsiContainsText(SearchRec.Name,'.wav'))
         and (not AnsiContainsText(SearchRec.Name,'.wmv')) and (not AnsiContainsText(SearchRec.Name,'.mid'))
         and (not AnsiContainsText(SearchRec.Name,'.RMI')) and (not AnsiContainsText(SearchRec.Name,'.cda')) then
         continue;
         if (SearchRec.Name<>'..') and (AnsiContainsText(SearchRec.Name,'.mp3')) then
            begin
              //if SearchRec.Name='周笔畅-笔记.mp3' then
              //begin
              //showMessage('ddddd');
              //end;
              try
              aMP3Protery.ID := 0;
              aMP3Protery.Name :='';
              aMP3Protery.FileExt := '';
              aMP3Protery.Autor := '';
              aMP3Protery.DiskName := '';
              aMP3Protery.OtherInfo := '';
              aMP3Protery.IndexN := 0;
              aMP3Protery.IndexC := '';
              aMP3Protery.Path := '';
              aMP3Protery.Size := 0;
              //赋值
              aMP3Info.Filename := mainpath+SearchRec.Name;
              aMP3Protery.Name := Copy(SearchRec.Name,1,Length(SearchRec.Name)-4);
              aMP3Protery.FileExt := Copy(SearchRec.Name,Length(SearchRec.Name)-2,3);
              aMP3Protery.Autor := aMP3Info.Artist;
              aMP3Protery.DiskName := aMP3Info.Album;
              aMP3Protery.OtherInfo := aMP3Info.Comment;
              aMP3Protery.IndexN := 0;
              aMP3Protery.IndexC := '';
              aMP3Protery.Path := mainpath+SearchRec.Name;
              aMP3Protery.Size := (SearchRec.Size div 1024) div 1024;
              ChgProgressMsg('正处理文件'+SearchRec.Name);//
              //ShowProgressDlg('正处理文件'+SearchRec.Name);
              if DM.AddFileInfoToDataBase(aMP3Protery.Name,
              aMP3Protery.Autor,aMP3Protery.DiskName,aMP3Protery.OtherInfo,
              aMP3Protery.Path,aMP3Protery.IndexN,aMP3Protery.IndexC,aMP3Protery.Size,aMP3Protery.FileExt) then
              begin
              //inc(file_count);
              //CloseProgressDlg;
              end;
              except
              CloseProgressDlg;
              end;
            end
         else
            begin
              try
              aMP3Protery.ID := 0;
              aMP3Protery.Name :='';
              aMP3Protery.FileExt := '';
              aMP3Protery.Autor := '';
              aMP3Protery.DiskName := '';
              aMP3Protery.OtherInfo := '';
              aMP3Protery.IndexN := 0;
              aMP3Protery.IndexC := '';
              aMP3Protery.Path := '';
              aMP3Protery.Size := 0;
              //赋值
              //aMP3Info.Filename := mainpath+SearchRec.Name;
              aMP3Protery.Name := Copy(SearchRec.Name,1,Length(SearchRec.Name)-4);
              aMP3Protery.FileExt := Copy(SearchRec.Name,Length(SearchRec.Name)-2,3);
              aMP3Protery.Autor := '';
              aMP3Protery.DiskName := '';
              aMP3Protery.OtherInfo := '';
              aMP3Protery.IndexN := 0;
              aMP3Protery.IndexC := '';
              aMP3Protery.Path := mainpath+SearchRec.Name;
              aMP3Protery.Size := (SearchRec.Size div 1024) div 1024;
              ChgProgressMsg('正处理文件'+SearchRec.Name);
              //ShowProgressDlg('正处理文件'+SearchRec.Name);
              if DM.AddFileInfoToDataBase(aMP3Protery.Name,
              aMP3Protery.Autor,aMP3Protery.DiskName,aMP3Protery.OtherInfo,
              aMP3Protery.Path,aMP3Protery.IndexN,aMP3Protery.IndexC,aMP3Protery.Size,aMP3Protery.FileExt) then
              begin
              //inc(file_count);
              CloseProgressDlg;
              end;
              except
              CloseProgressDlg;
              end;
            end;
        end;
      end;
    FindClose(SearchRec);
    found:=true;
//这是递归部分,查找各子目录。
    for i:=0 to subdir1.Count-1 do
      found:=bdSearchfile(mainpath+subdir1.Strings[i]+'\')or found;
    subdir1.Free;
    //CloseProgressDlg;
  end;
  result:=found;
  except
  CloseProgressDlg;
  end;
  finally
  CloseProgressDlg;
  end;
end;
end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -