Please help me with Delphi 7 file IO
October 1, 2005 2:27 AM   Subscribe

Please help me with Delphi 7.

I have never done any programming in Delphi or Pascal and I only have a little experience with programming in general. Now I need to be able to write a simple program for a school project.

The program I have in mind is a playlist generator for iTunes. It is supposed to create some sort of database of the songs in the user's iTunes library, let the user specify some data about each song (mood, tempo etc.) and generate a playlist based on the data (20 random songs with the mood "party", for example).

For now I won't worry about outputting the data into a format which iTunes will be able to understand. A simple text file with the names of the songs in the generated playlist will be fine.

So I guess the questions are: How do I make Delphi generate a list of files with the extensions mp3 or aac in a given directory and its subdirectories and feed this into a database? And how do I make Delphi read from that database and output some data to, say, a text file based on conditions given by the user?

If you think this program will be to much of a challenge to write for a complete Delphi newbie, please let me know. Also, the assignment is not for a programming course, so it's not cheating.
posted by sveskemus to Computers & Internet (2 answers total) 1 user marked this as a favorite
 
Best answer: Naw, this is a perfect beginners project.

To generate the list of songs, just create a recursive find feature.
PROCEDURE FindSongs(RootDir : String;  SongList : TStringList);
VAR
  SR   : TSearchRec;
  sts  : Integer;
BEGIN
   TRY
      sts := FindFirst(RootDir + '\*.mp3',  faAnyFile, SR);
      WHILE (sts = 0) DO
      BEGIN
         IF ((SR.Attr AND faDirectory) AND (SR.Name[1] <> '.')) THEN
            FindSongs(RootDir + '\' + SR.Name)
         ELSE SongList.Add(RootDir + '\' + SR.Name);
         sts := FindNext(SR);
      END;
   FINALLY
      FindClose(SR);
   END;
END;
The Stringlist will contain a list of all the MP3 songs found under the directory given. So "FindSongs('C:\', SL) will fill the "SL" stringlist with the name of every MP3 file no your C: drive.

As far as the database, you probably want a record structure like the following:
TYPE
   TSongTempo   = 1..10;
   TSong = RECORD
      SongID         : AutoInc;
      Title          : Char[50];
      ArtistName     : Char[60];
      Tempo          : TSongTempo;
      Genre          : String[20];
      Keywords       : String[100];
   END;
You can use the "keywords" to code song attributes in an upper-case, comma-delimited list like:
   Song.keywords = 'FUNKY,UPTEMPO,CHEESY';
Processing the list would be simple and sequential. You could evaluate 5,000 songs in a couple seconds. You could disaggregate individual keywords into a separate relational table, which would allow for greater efficiency in searching and retrieving, but I think this would be too advanced for a beginner's project.
   SongTable.First;
   WHILE NOT SongTable.Eof DO
   BEGIN
      S := ',' + SongTable.FieldByName('Keywords').AsString + ',';
      IF (POS(',FUNKY,' S) > 0) THEN
         AddToSonglist;
      SongTable.Next;
   END;
I've just sketched out the basics, but processing an entire table of, say, 5,000 songs would take, maybe, 3 seconds.
posted by curtm at 5:43 AM on October 1, 2005


I'm not a Delphi user (I'm a cook), but many folks over on the Joel on Software forum are pretty good at giving you Delphi advice. May come in handy in the future.
posted by madman at 11:21 PM on October 1, 2005


« Older Are there any cheap places to stay in downtown...   |   PC video graphics ongoing error Newer »
This thread is closed to new comments.