Hi there,

Im writing a program that reads ID3 tag information of songs in a given directory and then creates corresponding folders for that Song. (Ie the folder is called the ID3 artist's name). The song is then renamed and placed into the new folder.

However, i have come across both an anoying and totally mind bogoling problem which i just cant get my head around. I am trying to take a Directory (such as 'C:\Music\MP3') as one string in a variable ('RootDirectory'), a backslash and finally, another string (such as'Some Artist - Some Title.mp3') as another string variable('SourceFiles_Memo.Lines.Strings[Current]'), and then combine the two strings to get something like this:
'C:\Music\MP3\SomeArtist - Some Title.mp3'

However, what i am actually getting is this:
'\Some Artist - Some Title.mp3'

Now, the obvious thing here would be that the 'RootDirectory' string has nothing in it, but heres where it gets interesting. The 'RootDirectory' varable DOES have a string within it, and the 'SourceFiles_Memo.Lines.Strings[Current]' variable also has a string in it.

The Code for this procedure is shown below:

Procedure TFolderCreator_Form.Create_ButtonClick(Sender: TObject);
var
  Current: integer;
  OldFilename, NewFilename: String;
 
begin  
  //Check that files have been found
  if SourceFiles_Memo.Lines.Count > 0 then
    begin
 
      //Run through each file that has been found in a loop
      For Current := 0 to SourceFiles_Memo.Lines.Count do
        begin
 
          //Get The current Song filename
          OldFilename := SourceDirectory + '\' + SourceFiles_Memo.Lines.Strings[Current];
          Test1.Text := OldFilename;
 
          //Get the ID3 tag information for song
          Main_Form.GetID3(OldFilename);
 
          //Rename the old file with new information
          NewFilename := UMain_unit.ID3Artist + ' - ' + UMain_Unit.ID3Title;
          Test3.Text := NewFilename;
 
          //Create new folder for song
          NewDirectory := SourceDirectory + '\' + ID3Artist;
          Test2.Text := SourceDirectory;
          Test4.Text := NewDirectory;
 
          //Check if new folder exists, if not create it
          if DirectoryExists(NewDirectory) then
            begin
              MoveFile(PChar(OldFilename), PChar(NewFilename));
            end
          else
            begin
              CreateDir(NewDirectory);
              MoveFile(PChar(OldFilename), PChar(NewFilename));
            end;
         end;
      SourceFiles_Memo.Lines.Clear;
    end
  else
    ShowMessage('No files were found');
end;

Each 'TestN.text' edit box has been used to display the variables for troubleshooting only.

I have also tried using the 'Concat(String1,... ,StringN)' procedure, but i got exactly the same result.

Thank you in advance for any help anyone may be able to give,

Carson

UMain_unit i believe is some kind of structure(record,type etc)

NewFilename := UMain_unit.ID3Artist + ' - ' + UMain_Unit.ID3Title;
try to add the '-' to id3artist field in your GetID3 procedure.
variable OldFilename have something in it?
another thing
"For Current := 0 to SourceFiles_Memo.Lines.Count do" i will not do it in this way
try in thi way
"For Current := SourceFiles_Memo.Lines.Count downto 0 "

OldFilename, NewFilename - should be initiated in every step of the loop with ''

i am in a hurry and i wasn't able to take a closer look to your code but try this things. also if you do not get any result try to upload your entire code(i mean the ide3 procedure and anything that is used by this procedure)

best regards,


hi,
Thanks for your reply. I shall try out what you said shortly and see if it makes any difference.

The program i am writing is effectively a media player with playlists, etc but has some aditional features such as physically organising and renaming music on the hard disk. Because of this the program is made up of several units, this music folder creator unit being one. So procedures such as 'GetID3()' are used by lots of other bits of the program and so cannot easily be modified.

I dont want to post the whole of the code for my program because it is around 1500 lines, and only a small portion of that is relevant.

Here is some information that may help you to understand more about the program:

There are 4 main units:
UMain_unit, UOptions_Form, UFolderCreator, UAbout.

UMain_Unit is the main unit to which the whole program is written around. The other units are for other forms within the program.

Here is the whole of the UFolderCreator unit:

unit UFolderCreator;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, FileCtrl, id3v2, UMain_Unit;
type
  TFolderCreator_Form = class(TForm)
    Source_GroupBox: TGroupBox;
    SourceFolder_Edit: TEdit;
    SourceFolder_Button: TButton;
    SourceFiles_Memo: TMemo;
    SourceFiles_Label: TLabel;
    Found_Label: TLabel;
    Create_Button: TButton;
    Test1: TEdit;
    Test2: TEdit;
    Test3: TEdit;
    Test4: TEdit;
    Test5: TEdit;
    procedure SourceFolder_ButtonClick(Sender: TObject);
    procedure Create_ButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;
var
  FolderCreator_Form: TFolderCreator_Form;
  SourceCriteria, SourceDirectory, NewDirectory: String;
  SourceFound: integer;
implementation
{$R *.dfm}
procedure TFolderCreator_Form.SourceFolder_ButtonClick(Sender: TObject);
var
  SourceResults : TSearchRec;
  SourceCriteria, SourceDirectory: String;
  SourceFound: integer;
begin
  //Opens Up a 'Source Directory' Dialog
  if SelectDirectory('Select a Source directory...', '', SourceDirectory) then
    begin
      SourceFolder_Edit.Text := SourceDirectory;
    end;
  //Clear last search
  SourceFiles_Memo.Clear;
  SourceFound := 0;
  SourceCriteria := '*.mp3';
  SetCurrentDir(SourceDirectory);
  // Try to find regular files matching Unit1.d* in the current dir
  if FindFirst(SourceCriteria, faAnyFile , SourceResults) = 0 then
  begin
    repeat
      SourceFiles_Memo.Lines.Add(SourceResults.Name);
      Inc(SourceFound);
    until FindNext(SourceResults) <> 0;
    // Must free up resources used by these successful finds
    FindClose(SourceResults);
    Found_Label.Caption := 'Found: ' + IntToStr(SourceFound);
  end;
  
end;
procedure TFolderCreator_Form.Create_ButtonClick(Sender: TObject);
var
  Current: integer;
  OldFilename, NewFilename: String;
begin
  //Check that files have been found
  if SourceFiles_Memo.Lines.Count > 0 then
    begin
      //Run through each file that has been found in a loop
      For Current := 0 to SourceFiles_Memo.Lines.Count do
        begin
          //Get The current Song filename
          OldFilename := SourceDirectory + '\' + SourceFiles_Memo.Lines.Strings[Current];
          Test1.Text := OldFilename;
          //Get the ID3 tag information for song
          Main_Form.GetID3(OldFilename);
          //Rename the old file with new information
          NewFilename := UMain_unit.ID3Artist + ' - ' + UMain_Unit.ID3Title;
          Test3.Text := NewFilename;
          //Create new folder for song
          NewDirectory := SourceDirectory + '\' + ID3Artist;
          Test2.Text := SourceDirectory;
          Test4.Text := NewDirectory;
          //Check if new folder exists, if not create it
          if DirectoryExists(NewDirectory) then
            begin
              MoveFile(PChar(OldFilename), PChar(NewFilename));
            end
          else
            begin
              CreateDir(NewDirectory);
              MoveFile(PChar(OldFilename), PChar(NewFilename));
            end;
         end;
      SourceFiles_Memo.Lines.Clear;
    end
  else
    ShowMessage('No files were found');
end;
end.

Here is the GetID3 procedure and any relevant variables for te procedure:

Var
  Main_Form: TMain_Form;
  //Directory Variables
  RootDirectory,
  CurrentMedia,
  CurrentFile: String;
 
  //ID3 Tag Variables
  ID3: Tid3v2Tag;
  ID3Title,
  ID3Artist,
  ID3Album,
  ID3Year,
  ID3Genre,
  ID3Track,
  ID3Comment: String;
 
--------------------------------------------------------
 
Procedure TMain_Form.GetID3(Filename:String);
var
tempStr : string;
tempInt : word;
myCOMM : COMM;
begin
  tempInt := ID3.loadFromFile(Filename, 0);
  if (tempInt > 255) then
    BackupGetID3(Filename)
  else
    begin
      ID3.getAsciiText('TIT2', tempStr); //Get Song Title
        ID3Title := tempStr;
      ID3.getAsciiText('TPE1', tempStr); //Get Artist Name
        ID3Artist := tempStr;
      ID3.getAsciiText('TALB', tempStr); //Get Album Name
        ID3Album := tempStr;
      ID3.getAsciiText('TYER', tempStr); //Get Release Year
        ID3Year := tempStr;
      ID3.getAsciiText('TCON', tempStr); //Get Genre
        ID3Genre := tempStr;
      ID3.getAsciiText('TRCK', tempStr); //Get Track #
        ID3Track := tempStr;
      ID3.getCOMM(myCOMM, ''); //Get basic comment (no description)
        ID3Comment := myCOMM.body;
    end;
end;

Finally, I have the begining part of the main unit for the program and the folder creator unit which may help you to understand more about whats going on:

unit UMain_Unit;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Menus, MPlayer, StdCtrls, AppEvnts, FileCtrl, id3v2, WMAFile, ShellApi,
  MMSystem, ComCtrls, Math, ExtCtrls, Grids;
type
  TMain_Form = class(TForm)
    Menu_MainMenu: TMainMenu;
    File_Menu: TMenuItem;
    Exit_File: TMenuItem;
    Break3_File: TMenuItem;
    Open_File: TMenuItem;
    Break1_File: TMenuItem;
    OpenPlaylist_File: TMenuItem;
    EditPlaylist_File: TMenuItem;
    Library_Main: TMenuItem;
    View_Library: TMenuItem;
    Break1_MusicLibrary: TMenuItem;
    Help_Main: TMenuItem;
    About_Help: TMenuItem;
    Break2_File: TMenuItem;
    Options_File: TMenuItem;
    Player_MediaPlayer: TMediaPlayer;
    Playlist_Label: TLabel;
    OpenMedia_OpenDialog: TOpenDialog;
    MediaList_ListView: TListView;
    Main_StatusBar: TStatusBar;
    MediaProgress_ProgressBar: TProgressBar;
    ProgressTimer_Timer: TTimer;
    Playlist_StringGrid: TStringGrid;
    Playlist_OpenDialog: TOpenDialog;
    SavePlaylist_File: TMenuItem;
    Playlist_SaveDialog: TSaveDialog;
    VolumeControl_TrackBar: TTrackBar;
    BottomBackground_Image: TImage;
    Next_Normal_Image: TImage;
    Previous_Normal_Image: TImage;
    Stop_Normal_Image: TImage;
    Play_Normal_Image: TImage;
    Pause_Normal_Image: TImage;
    Next_Mouse_Image: TImage;
    Previous_Mouse_Image: TImage;
    Stop_Mouse_Image: TImage;
    Play_Mouse_Image: TImage;
    Pause_Mouse_Image: TImage;
    AddMusic_Library: TMenuItem;
    procedure FormActivate(Sender: TObject);
    procedure SetMPVolume(MP: TMediaPlayer; Volume: Integer);
    function GetMPVolume(MP: TMediaPlayer): Integer;
    procedure ShowFolder(strFolder: string);
    procedure Options_FileClick(Sender: TObject);
    procedure Exit_FileClick(Sender: TObject);
    procedure Open_FileClick(Sender: TObject);
    procedure View_LibraryClick(Sender: TObject);
    Procedure FillMediaList;
    procedure GetID3(Filename: String);
    procedure SaveID3(Filename: String);
    Procedure BackupGetID3(Filename:String);
    Procedure GetWMA(Filename: string);
    procedure FileSearch(const RootDirectory, Criteria : string);
    procedure MediaList_ListViewDblClick(Sender: TObject);
    Procedure PlayMedia(CurrentMedia: String);
    procedure ProgressTimer_TimerTimer(Sender: TObject);
    Procedure AddToPlaylist(Artist, Title, Filename: String);
    procedure Playlist_StringGridDblClick(Sender: TObject);
    Procedure ReadSettingsFromFile;
    Procedure WriteSettingsToFile;
    procedure OpenPlaylist_FileClick(Sender: TObject);
    Procedure SavePlaylist_FileClick(Sender: TObject);
    procedure About_HelpClick(Sender: TObject);
    procedure VolumeControl_TrackBarChange(Sender: TObject);
    Procedure PlayPlaylist(CurrentPlaylistRow: integer);
    Procedure NextSong;
    Procedure PreviousSong;
    procedure Previous_Normal_ImageMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Previous_Normal_ImageMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Next_Normal_ImageMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Next_Normal_ImageMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Stop_Normal_ImageMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Stop_Normal_ImageMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Play_Normal_ImageMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Play_Normal_ImageMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Pause_Normal_ImageMouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure Pause_Normal_ImageMouseUp(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure MediaList_ListViewColumnClick(Sender: TObject;
      Column: TListColumn);
    procedure AddMusic_LibraryClick(Sender: TObject);
  private
    { Private declarations }
  public
  end;
unit UFolderCreator;
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, FileCtrl, id3v2, UMain_Unit;
type
  TFolderCreator_Form = class(TForm)
    Source_GroupBox: TGroupBox;
    SourceFolder_Edit: TEdit;
    SourceFolder_Button: TButton;
    SourceFiles_Memo: TMemo;
    SourceFiles_Label: TLabel;
    Found_Label: TLabel;
    Create_Button: TButton;
    Test1: TEdit;
    Test2: TEdit;
    Test3: TEdit;
    Test4: TEdit;
    Test5: TEdit;
    procedure SourceFolder_ButtonClick(Sender: TObject);
    procedure Create_ButtonClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

Thanks again for any help you may be able to offer,

Carson

Ok, i have found the problem. I had declared one of the strings variables both globally and privately. So it is all sorted now.

Thanks for the help i received, much appreciated.

Carson

Wow... That seems like an interesting program (and helpful, too)! Can you send me the exe in a zip file to my email address or send me the link to it? I could use some organization...

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.