Skip to main content

Notice

Please note that most of the software linked on this forum is likely to be safe to use. If you are unsure, feel free to ask in the relevant topics, or send a private message to an administrator or moderator. To help curb the problems of false positives, or in the event that you do find actual malware, you can contribute through the article linked here.
Topic: How to check if file exists? (Read 1479 times) previous topic - next topic
0 Members and 1 Guest are viewing this topic.

How to check if file exists?

I'm trying to set the background of my ELPlaylist as an artist picture that I store only in the root folder of each artist, not under each album, so the path to it is usually (%path%)\..\artist.* . But sometimes I need to go a level higher, when an album I'm currently playing is divided into multiple CDs, where each of them have a separate folder. And sometimes it's in the same level as the song, if it does not belong to any album.

To set an artist picture, this code is being used by default:
Code: [Select]
$set_ps_global(do.artist.pic, $directory_path(%path%)\artist.*)

I've been going through the list of functions but cannot find a single one that would work in my case. What I tried was to check for the file size- if it's greater than 0, or a certain small number, it picks the picture. Since there is probably no way to make a 4-way elseif-like statement, I thought of putting several of those checks under each other, like this (except I have no idea how to get the %filesize% from that selected artist file):
Code: [Select]
$set_ps_global(do.artist.pic,$ifgreater($directory_path(%path%)\..\artist.*,0,$directory_path(%path%)\..\artist.*))
$set_ps_global(do.artist.pic,$ifgreater($directory_path(%path%)\..\..\artist.*,0,$directory_path(%path%)\..\..\artist.*))
Any idea about a solution that might work here? And how to check for if a certain file exists?

Re: How to check if file exists?

Reply #1
If you want to check if a file exist use $findfile function. Also, set_ps_global merely sets a global variable, As such don't set two global variables with the same name, that's not gonna work.

Re: How to check if file exists?

Reply #2
Thanks, I'll try that function when I get home. I'm not setting the same variable twice since only one of the statements will be true, every time.

Re: How to check if file exists?

Reply #3
No, you're setting one global variable which equals an if statement and then you define another global variable with the same name but with another if statement.
When the global variable is called you call it by its name, in your case "do.artist.pic". How is the system to know which if statement to use? It can't. Also, the logic doesn't hold up ,since both can be true if you have files in both directories. Also, $ifgreater shouldn't be used like that. When comparing integers you can use $ifgreater, not when you want to compare string lengths or file size which I what you're trying to do here I guess.

It seems you have multiple things confused here. Starting with the use of global variables and if statements (including $ifgreater).


What you're talking about is this:

Code: [Select]
$set_ps_global(do.artist.pic,$directory_path(%path%)\..\..\artist.*)
$set_ps_global(do.artist.pic2,$directory_path(%path%)\..\artist.*)

$if(statement,%do.artist.pic%,%do.artist.pic2%)


To search a particular file you need to be doing something like this:

Code: [Select]
$if($findfile(file path),true, false)

Re: How to check if file exists?

Reply #4
...

 

Re: How to check if file exists?

Reply #5
You can supply $findfile with multiple paths and it returns the first one that exists...

Code: [Select]
$set_ps_global(do.artist.pic2,$findfile(path1,path2,path3))

To get the parent folder, you'll need to do something like...

Code: [Select]
$replace($directory_path(%path%),%directory%,)artist.*



Re: How to check if file exists?

Reply #6
You can supply $findfile with multiple paths and it returns the first one that exists...

Code: [Select]
$set_ps_global(do.artist.pic2,$findfile(path1,path2,path3))
Thanks, it worked! Now my code that looks for artist pictures in every possible case within my library, looks like this:
Code: [Select]
$set_ps_global(do.artist.pic,$findfile($directory_path(%path%)\..\artist.*,$directory_path(%path%)\..\..\artist.*,$directory_path(%path%)\artist.*,$directory_path(%path%)\..\..\..\artist.*))
I've ordered it by how common each case is in hope that it will cut some precious milliseconds when looking for the picture.

jazzthieve I am not having it confused and I didn't expect that code to work, I only wanted to hint what I had in mind but just missed the function to use in there. I was trying to set the same global variable, not several of them, and I did it so because I was certain that in every case only at most one of the conditions would evaluate as true.

The findfile function you suggested was just what I was looking for, thanks!