Two strings will not combine

Thread Solved

Join Date: Jan 2007
Posts: 8
Reputation: Carson89 is an unknown quantity at this point 
Solved Threads: 0
Carson89 Carson89 is offline Offline
Newbie Poster

Two strings will not combine

 
0
  #1
Jan 25th, 2007
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:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. Procedure TFolderCreator_Form.Create_ButtonClick(Sender: TObject);
  2. var
  3. Current: integer;
  4. OldFilename, NewFilename: String;
  5.  
  6. begin
  7. //Check that files have been found
  8. if SourceFiles_Memo.Lines.Count > 0 then
  9. begin
  10.  
  11. //Run through each file that has been found in a loop
  12. For Current := 0 to SourceFiles_Memo.Lines.Count do
  13. begin
  14.  
  15. //Get The current Song filename
  16. OldFilename := SourceDirectory + '\' + SourceFiles_Memo.Lines.Strings[Current];
  17. Test1.Text := OldFilename;
  18.  
  19. //Get the ID3 tag information for song
  20. Main_Form.GetID3(OldFilename);
  21.  
  22. //Rename the old file with new information
  23. NewFilename := UMain_unit.ID3Artist + ' - ' + UMain_Unit.ID3Title;
  24. Test3.Text := NewFilename;
  25.  
  26. //Create new folder for song
  27. NewDirectory := SourceDirectory + '\' + ID3Artist;
  28. Test2.Text := SourceDirectory;
  29. Test4.Text := NewDirectory;
  30.  
  31. //Check if new folder exists, if not create it
  32. if DirectoryExists(NewDirectory) then
  33. begin
  34. MoveFile(PChar(OldFilename), PChar(NewFilename));
  35. end
  36. else
  37. begin
  38. CreateDir(NewDirectory);
  39. MoveFile(PChar(OldFilename), PChar(NewFilename));
  40. end;
  41. end;
  42. SourceFiles_Memo.Lines.Clear;
  43. end
  44. else
  45. ShowMessage('No files were found');
  46. 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
Last edited by Carson89; Jan 25th, 2007 at 7:07 am. Reason: Forgot to add some useful information
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 171
Reputation: radu84 is an unknown quantity at this point 
Solved Threads: 16
radu84 radu84 is offline Offline
Junior Poster

Re: Two strings will not combine

 
0
  #2
Jan 25th, 2007
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,


Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 8
Reputation: Carson89 is an unknown quantity at this point 
Solved Threads: 0
Carson89 Carson89 is offline Offline
Newbie Poster

Re: Two strings will not combine

 
0
  #3
Jan 25th, 2007
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:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. unit UFolderCreator;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, ExtCtrls, FileCtrl, id3v2, UMain_Unit;
  6. type
  7. TFolderCreator_Form = class(TForm)
  8. Source_GroupBox: TGroupBox;
  9. SourceFolder_Edit: TEdit;
  10. SourceFolder_Button: TButton;
  11. SourceFiles_Memo: TMemo;
  12. SourceFiles_Label: TLabel;
  13. Found_Label: TLabel;
  14. Create_Button: TButton;
  15. Test1: TEdit;
  16. Test2: TEdit;
  17. Test3: TEdit;
  18. Test4: TEdit;
  19. Test5: TEdit;
  20. procedure SourceFolder_ButtonClick(Sender: TObject);
  21. procedure Create_ButtonClick(Sender: TObject);
  22. private
  23. { Private declarations }
  24. public
  25. { Public declarations }
  26. end;
  27. var
  28. FolderCreator_Form: TFolderCreator_Form;
  29. SourceCriteria, SourceDirectory, NewDirectory: String;
  30. SourceFound: integer;
  31. implementation
  32. {$R *.dfm}
  33. procedure TFolderCreator_Form.SourceFolder_ButtonClick(Sender: TObject);
  34. var
  35. SourceResults : TSearchRec;
  36. SourceCriteria, SourceDirectory: String;
  37. SourceFound: integer;
  38. begin
  39. //Opens Up a 'Source Directory' Dialog
  40. if SelectDirectory('Select a Source directory...', '', SourceDirectory) then
  41. begin
  42. SourceFolder_Edit.Text := SourceDirectory;
  43. end;
  44. //Clear last search
  45. SourceFiles_Memo.Clear;
  46. SourceFound := 0;
  47. SourceCriteria := '*.mp3';
  48. SetCurrentDir(SourceDirectory);
  49. // Try to find regular files matching Unit1.d* in the current dir
  50. if FindFirst(SourceCriteria, faAnyFile , SourceResults) = 0 then
  51. begin
  52. repeat
  53. SourceFiles_Memo.Lines.Add(SourceResults.Name);
  54. Inc(SourceFound);
  55. until FindNext(SourceResults) <> 0;
  56. // Must free up resources used by these successful finds
  57. FindClose(SourceResults);
  58. Found_Label.Caption := 'Found: ' + IntToStr(SourceFound);
  59. end;
  60.  
  61. end;
  62. procedure TFolderCreator_Form.Create_ButtonClick(Sender: TObject);
  63. var
  64. Current: integer;
  65. OldFilename, NewFilename: String;
  66. begin
  67. //Check that files have been found
  68. if SourceFiles_Memo.Lines.Count > 0 then
  69. begin
  70. //Run through each file that has been found in a loop
  71. For Current := 0 to SourceFiles_Memo.Lines.Count do
  72. begin
  73. //Get The current Song filename
  74. OldFilename := SourceDirectory + '\' + SourceFiles_Memo.Lines.Strings[Current];
  75. Test1.Text := OldFilename;
  76. //Get the ID3 tag information for song
  77. Main_Form.GetID3(OldFilename);
  78. //Rename the old file with new information
  79. NewFilename := UMain_unit.ID3Artist + ' - ' + UMain_Unit.ID3Title;
  80. Test3.Text := NewFilename;
  81. //Create new folder for song
  82. NewDirectory := SourceDirectory + '\' + ID3Artist;
  83. Test2.Text := SourceDirectory;
  84. Test4.Text := NewDirectory;
  85. //Check if new folder exists, if not create it
  86. if DirectoryExists(NewDirectory) then
  87. begin
  88. MoveFile(PChar(OldFilename), PChar(NewFilename));
  89. end
  90. else
  91. begin
  92. CreateDir(NewDirectory);
  93. MoveFile(PChar(OldFilename), PChar(NewFilename));
  94. end;
  95. end;
  96. SourceFiles_Memo.Lines.Clear;
  97. end
  98. else
  99. ShowMessage('No files were found');
  100. end;
  101. end.

Here is the GetID3 procedure and any relevant variables for te procedure:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. Var
  2. Main_Form: TMain_Form;
  3. //Directory Variables
  4. RootDirectory,
  5. CurrentMedia,
  6. CurrentFile: String;
  7.  
  8. //ID3 Tag Variables
  9. ID3: Tid3v2Tag;
  10. ID3Title,
  11. ID3Artist,
  12. ID3Album,
  13. ID3Year,
  14. ID3Genre,
  15. ID3Track,
  16. ID3Comment: String;
  17.  
  18. --------------------------------------------------------
  19.  
  20. Procedure TMain_Form.GetID3(Filename:String);
  21. var
  22. tempStr : string;
  23. tempInt : word;
  24. myCOMM : COMM;
  25. begin
  26. tempInt := ID3.loadFromFile(Filename, 0);
  27. if (tempInt > 255) then
  28. BackupGetID3(Filename)
  29. else
  30. begin
  31. ID3.getAsciiText('TIT2', tempStr); //Get Song Title
  32. ID3Title := tempStr;
  33. ID3.getAsciiText('TPE1', tempStr); //Get Artist Name
  34. ID3Artist := tempStr;
  35. ID3.getAsciiText('TALB', tempStr); //Get Album Name
  36. ID3Album := tempStr;
  37. ID3.getAsciiText('TYER', tempStr); //Get Release Year
  38. ID3Year := tempStr;
  39. ID3.getAsciiText('TCON', tempStr); //Get Genre
  40. ID3Genre := tempStr;
  41. ID3.getAsciiText('TRCK', tempStr); //Get Track #
  42. ID3Track := tempStr;
  43. ID3.getCOMM(myCOMM, ''); //Get basic comment (no description)
  44. ID3Comment := myCOMM.body;
  45. end;
  46. 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:
Pascal and Delphi Syntax (Toggle Plain Text)
  1. unit UMain_Unit;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, Menus, MPlayer, StdCtrls, AppEvnts, FileCtrl, id3v2, WMAFile, ShellApi,
  6. MMSystem, ComCtrls, Math, ExtCtrls, Grids;
  7. type
  8. TMain_Form = class(TForm)
  9. Menu_MainMenu: TMainMenu;
  10. File_Menu: TMenuItem;
  11. Exit_File: TMenuItem;
  12. Break3_File: TMenuItem;
  13. Open_File: TMenuItem;
  14. Break1_File: TMenuItem;
  15. OpenPlaylist_File: TMenuItem;
  16. EditPlaylist_File: TMenuItem;
  17. Library_Main: TMenuItem;
  18. View_Library: TMenuItem;
  19. Break1_MusicLibrary: TMenuItem;
  20. Help_Main: TMenuItem;
  21. About_Help: TMenuItem;
  22. Break2_File: TMenuItem;
  23. Options_File: TMenuItem;
  24. Player_MediaPlayer: TMediaPlayer;
  25. Playlist_Label: TLabel;
  26. OpenMedia_OpenDialog: TOpenDialog;
  27. MediaList_ListView: TListView;
  28. Main_StatusBar: TStatusBar;
  29. MediaProgress_ProgressBar: TProgressBar;
  30. ProgressTimer_Timer: TTimer;
  31. Playlist_StringGrid: TStringGrid;
  32. Playlist_OpenDialog: TOpenDialog;
  33. SavePlaylist_File: TMenuItem;
  34. Playlist_SaveDialog: TSaveDialog;
  35. VolumeControl_TrackBar: TTrackBar;
  36. BottomBackground_Image: TImage;
  37. Next_Normal_Image: TImage;
  38. Previous_Normal_Image: TImage;
  39. Stop_Normal_Image: TImage;
  40. Play_Normal_Image: TImage;
  41. Pause_Normal_Image: TImage;
  42. Next_Mouse_Image: TImage;
  43. Previous_Mouse_Image: TImage;
  44. Stop_Mouse_Image: TImage;
  45. Play_Mouse_Image: TImage;
  46. Pause_Mouse_Image: TImage;
  47. AddMusic_Library: TMenuItem;
  48. procedure FormActivate(Sender: TObject);
  49. procedure SetMPVolume(MP: TMediaPlayer; Volume: Integer);
  50. function GetMPVolume(MP: TMediaPlayer): Integer;
  51. procedure ShowFolder(strFolder: string);
  52. procedure Options_FileClick(Sender: TObject);
  53. procedure Exit_FileClick(Sender: TObject);
  54. procedure Open_FileClick(Sender: TObject);
  55. procedure View_LibraryClick(Sender: TObject);
  56. Procedure FillMediaList;
  57. procedure GetID3(Filename: String);
  58. procedure SaveID3(Filename: String);
  59. Procedure BackupGetID3(Filename:String);
  60. Procedure GetWMA(Filename: string);
  61. procedure FileSearch(const RootDirectory, Criteria : string);
  62. procedure MediaList_ListViewDblClick(Sender: TObject);
  63. Procedure PlayMedia(CurrentMedia: String);
  64. procedure ProgressTimer_TimerTimer(Sender: TObject);
  65. Procedure AddToPlaylist(Artist, Title, Filename: String);
  66. procedure Playlist_StringGridDblClick(Sender: TObject);
  67. Procedure ReadSettingsFromFile;
  68. Procedure WriteSettingsToFile;
  69. procedure OpenPlaylist_FileClick(Sender: TObject);
  70. Procedure SavePlaylist_FileClick(Sender: TObject);
  71. procedure About_HelpClick(Sender: TObject);
  72. procedure VolumeControl_TrackBarChange(Sender: TObject);
  73. Procedure PlayPlaylist(CurrentPlaylistRow: integer);
  74. Procedure NextSong;
  75. Procedure PreviousSong;
  76. procedure Previous_Normal_ImageMouseDown(Sender: TObject;
  77. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  78. procedure Previous_Normal_ImageMouseUp(Sender: TObject;
  79. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  80. procedure Next_Normal_ImageMouseDown(Sender: TObject;
  81. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  82. procedure Next_Normal_ImageMouseUp(Sender: TObject;
  83. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  84. procedure Stop_Normal_ImageMouseDown(Sender: TObject;
  85. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  86. procedure Stop_Normal_ImageMouseUp(Sender: TObject;
  87. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  88. procedure Play_Normal_ImageMouseDown(Sender: TObject;
  89. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  90. procedure Play_Normal_ImageMouseUp(Sender: TObject;
  91. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  92. procedure Pause_Normal_ImageMouseDown(Sender: TObject;
  93. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  94. procedure Pause_Normal_ImageMouseUp(Sender: TObject;
  95. Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  96. procedure MediaList_ListViewColumnClick(Sender: TObject;
  97. Column: TListColumn);
  98. procedure AddMusic_LibraryClick(Sender: TObject);
  99. private
  100. { Private declarations }
  101. public
  102. end;

Pascal and Delphi Syntax (Toggle Plain Text)
  1. unit UFolderCreator;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, ExtCtrls, FileCtrl, id3v2, UMain_Unit;
  6. type
  7. TFolderCreator_Form = class(TForm)
  8. Source_GroupBox: TGroupBox;
  9. SourceFolder_Edit: TEdit;
  10. SourceFolder_Button: TButton;
  11. SourceFiles_Memo: TMemo;
  12. SourceFiles_Label: TLabel;
  13. Found_Label: TLabel;
  14. Create_Button: TButton;
  15. Test1: TEdit;
  16. Test2: TEdit;
  17. Test3: TEdit;
  18. Test4: TEdit;
  19. Test5: TEdit;
  20. procedure SourceFolder_ButtonClick(Sender: TObject);
  21. procedure Create_ButtonClick(Sender: TObject);
  22. private
  23. { Private declarations }
  24. public
  25. { Public declarations }
  26. end;

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

Carson
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 8
Reputation: Carson89 is an unknown quantity at this point 
Solved Threads: 0
Carson89 Carson89 is offline Offline
Newbie Poster

Re: Two strings will not combine

 
0
  #4
Jan 31st, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 917
Reputation: linux is an unknown quantity at this point 
Solved Threads: 27
linux's Avatar
linux linux is offline Offline
Posting Shark

Re: Two strings will not combine

 
0
  #5
Mar 5th, 2007
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...
Toshiba M1151.49 GB DDR-2 RAM1.6 GHz Centrino Duo80GB HDDWindows XP Media Center Edition
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC