Author Topic: path to file  (Read 864 times)


  • Guest
path to file
« on: March 16, 2023, 03:17:23 pm »
I'm embarrassed, but can't figzre out the syntax for loading a file from a subdirectory of the project.

Code: Pascal  [Select][+][-]
  1. playsound1.SoundFile:='load.mp3' ;
  2.   playsound1.Execute;

All sound files are in directory /sound which is a subdirectory of the main project.

If I put the file to the main directory /training, then all is OK.
I do not want to clutter trainning with all sorts of files, so I put mp3 files into sound.
What is the correct sytax, put +'/souns/' somewhere? Does not work...

Thanks for enlightening me ......


  • Sr. Member
  • ****
  • Posts: 472
Re: path to file
« Reply #1 on: March 16, 2023, 03:36:12 pm »
Code: Pascal  [Select][+][-]
  1. playsound1.SoundFile:='sound\load.mp3' ;


  • Hero Member
  • *****
  • Posts: 1511
Re: path to file
« Reply #2 on: March 16, 2023, 03:41:48 pm »
You can either use relative paths, or absolute paths. An absolute path is the full path to the file, e.g. on Windows C:\Users\username\Documents\file.txt
A relative path starts without a starting delimiter, and will be resolved from the current working directory. For example "file.txt" will be resolved to WorkingDirectory + "\" + "file.txt". You can also include sub directories, so "folder\file.txt" will be resolved to WorkingDirectory + "\" + "folder\file.txt".

A special function here have the special folders "." and "..", where "." refers  tho the current folder and ".." referrs to the parent folder. E.g. "C:\Windows\..\Users" will be resolved to "C:\Users" because .. will go back one step (i.e. back from C:\Windows to C:\).
So when you want to use a file which is one directory below your current working directly you can use a relative paths with ..: "..\file.txt" for the working directory "C:\Users\Username\Folder" will be resolved to "C:\Users\Username\file.txt" because the .. goes back one step from "(...)Username\Folder" to "(...)Username".

But be careful with relative paths. If you don't know the working directory from which you are called, you might not find the desired files. The working directory is not always (on linux not even most of the time) the directory where you application is located. So it's better to work with absolute paths here.
To get the absolute path to your current application use "ParamStr(0)":
Code: Pascal  [Select][+][-]
  1. filename := IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0))) + 'sounds' + PathDelim + 'sound.mp3';
« Last Edit: March 16, 2023, 03:44:36 pm by Warfley »


  • Guest
Re: path to file
« Reply #3 on: March 17, 2023, 10:15:30 am »
Dear Domasz,
already tried this - no sound.

Dear Warfly, thanks ever so much for the extensive reply, will try and report.

Glad being here, on other forums you often get some bad replies for basic questions ....



  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: path to file
« Reply #4 on: March 17, 2023, 01:00:17 pm »
Code: Pascal  [Select][+][-]
  1. playsound1.SoundFile:='sound\load.mp3' ;
should be:
Code: Pascal  [Select][+][-]
  1. playsound1.SoundFile:='.\sound\load.mp3' ;
and only works if internal you are still in folder of your app.
Warflays way with adding full path infront of your subfolder/file is the only bulletproof way.
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »


  • Hero Member
  • *****
  • Posts: 12069
Re: path to file
« Reply #5 on: March 17, 2023, 02:34:50 pm »
I assume that "Playsound1" is the component in the Playsound package distributed by OPM and wrote the attached demo which works on Windows, Linux and Mac. (The TPlaysound component code itself is included after a small modification of the sources and requires no extra installation this way).

My code introduces a variable "SoundsDir" which points to the sounds directory. A small complication is on mac which does not find it in the original location. But after copying the sounds directory into the Resource folder of the Application Bundle, it works here, too.

Using a separate variable for this directory has the advantage that you may define its location in a configuration file of your application so that the directory can be relocated without modifying the source code. Suppose that you cross-compile and you change the default setting of the Target File Name in the Project Options from "filename" to "bin\$(TargetCPU)-$(TargetOS)\filename" the executable of your application would no longer be in the source directory but in a separate folder for each operating system and cpu. In such a case you 'd have to copy the "sounds" folder into each of these folders for the application to find it. But with the config file approach you could simply redirect it to the old location in the source folder. But note that the attached sample project does not implement this (I could if you ask for it).


  • Full Member
  • ***
  • Posts: 144
Re: path to file
« Reply #6 on: April 14, 2023, 07:22:35 pm »
You can either use relative paths, or absolute paths. An absolute path is ..

thank you Warfley. I did not know how much I don't know.


TinyPortal © 2005-2018