Forum > Beginners

path to file

(1/2) > >>

pentilisea:
Hi,
I'm embarrassed, but can't figzre out the syntax for loading a file from a subdirectory of the project.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---playsound1.SoundFile:='load.mp3' ;  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 ......

domasz:
try:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---playsound1.SoundFile:='sound\load.mp3' ;

Warfley:
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---filename := IncludeTrailingPathDelimiter(ExtractFileDir(ParamStr(0))) + 'sounds' + PathDelim + 'sound.mp3';

pentilisea:
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 ....

Klaus

KodeZwerg:

--- Quote from: domasz on March 16, 2023, 03:36:12 pm ---try:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---playsound1.SoundFile:='sound\load.mp3' ;
--- End quote ---
should be:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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.

Navigation

[0] Message Index

[#] Next page

Go to full version