Hi All,
I wish to open a drive, read the
dir file names to a text file.
eg > Open Drv H, where exist a number of .mp3 files,
which I wish to capture the filenames only, to a text file eg dirnames.txt (not copy the contents, just the file names)

I have delphi 5/6
Any ideas of 'how to'?

Thank you for your time

Regards,
Gerhard

Recommended Answers

All 4 Replies

procedure Search_MP3_File(PathName: string; SL: TStringList);
var
  Found:         Integer;
  Attr:          Integer;
  I:             Integer;
  S:             string;
  T:             string;
  FName:         string;
  DirName:       string;
  SearchRec:     TSearchRec;
begin
  Attr := faDirectory;
  Found := FindFirst(PathName+'*.*', Attr, SearchRec);
  while (Found=0) do
  try
    if (((FileGetAttr(SearchRec.Name) and faArchive) > 0) 
    or ((FileGetAttr(SearchRec.Name) and faAnyFile) > 0)) then
    begin
      FName := PathName + SearchRec.Name; // SL.Strings[I];
      if FileExists(FName) then
      begin
        S := UpperCase(ExtractFileExt(FName));
        T := 'MP3'; // T := '.MP3';
        if (S=T) then
        begin
          // Congratulations, you fond a MP3 file
          SL.Add(FName);
        end;
      end;
    end;
    if ((SearchRec.Attr=2064) or (SearchRec.Attr=16)) 
    and (not (SearchRec.Name[1]='.')) then
    begin
      DirName := PathName + SearchRec.Name + '\';
      Search_MP3_File(DirName);
    end;
  finally
    Found := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
end;


procedure Main_Procedure;
var
  PCRootPathName:PChar;
  DriveType:     DWORD;
  S:             string;
  SL:            TStringList;
begin
  SL := TStringList.Create;
  try
      S := 'H:\';
      PCRootPathName := PChar(S, SL);
      DriveType := GetDriveType(PCRootPathName);
      if (DriveType=DRIVE_FIXED) then  // DRIVE_FIXED is HDD
        Search_MP3_File(S);
  finally
    SL.SaveToFile('List_of_MP3.txt');
    SL.Destroy;
  end;
end;

Hi,
Thanks for your quick reply. After evaluating, attempting
to compile in D6, I am unable to actually execute your code,
such for me to understand how you arrived at the solution.

I admit that my relative limited knowledge of the subject may
indeed be my problem, thus, is it possible to send me a more
complete, 'compilable executable' rendition of your code?

I realize that I am asking a lot, however, those who don't
ask will never know... Thank you in advance for your time!
Kind regards,
Gerhard

Hi gerhardjl,
I could compile by Delphi 7 the code above ....
I did write it based on my similar code, but didn't test it, so it is possible to contain small mistake or not ....
If you need I will test it under Delphi 7 and will post here working Delphi 7 code.
Sorry, I have not Delphi 6 installed ....

This is tested by Delphi 7 and working code - it writed in a text file List_of_MP3.txt more than 600 *.mp3 files located in my folder J:\_MUSIC in different subfolders :

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure Search_MP3_File(PathName: string; SL: TStringList);
var
  Found:         Integer;
  Attr:          Integer;
  S:             string;
  T:             string;
  FN:            string;
  FName:         string;
  DirName:       string;
  SearchRec:     TSearchRec;
begin
  Attr := faDirectory;
  FName := PathName + '\*.*';
  Found := FindFirst(FName, Attr, SearchRec);
  while (Found=0) do
  try
    if (((FileGetAttr(SearchRec.Name) and faArchive) > 0)
    or ((FileGetAttr(SearchRec.Name) and faAnyFile) > 0)) then
    begin
      S := UpperCase(ExtractFileExt(SearchRec.Name));
      T := '.MP3';
      if (S=T) then
      begin
        // Congratulations, you fond a MP3 file
        FN := PathName + '\' + SearchRec.Name;
        SL.Add(FN);
      end;
    end;
    if ((SearchRec.Attr=2064) or (SearchRec.Attr=16))
    and (not (SearchRec.Name[1]='.')) then
    begin
      DirName := PathName + '\' + SearchRec.Name;
      Search_MP3_File(DirName, SL);
    end;
  finally
    Found := FindNext(SearchRec);
  end;
  FindClose(SearchRec);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  S:             string;
  SL:            TStringList;
begin
  SL := TStringList.Create;
  try
    S := 'J:\_MUSIC';
    Search_MP3_File(S, SL);
  finally
    SL.SaveToFile('List_of_MP3.txt');
    SL.Destroy;
  end;
end;

end.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.