Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make the platform independent filepath slash "/" with GLib? (GJS)

Tags:

gtk

glib

gjs

gtk3

I have a folderPath which has a directory string:

/home/bastian/Pictures

and I have a variable fileName which contains the name. I can concatenate the two strings together like this, but it only works on UNIX systems:

let filePath = folderPath + '/' + fileName;

Is there a way with GLib I can concatenate the two to each other without making assumptions about the slash or backslash (to stay fx Windows-compatible)?

like image 363
Bastian Avatar asked Jan 28 '26 19:01

Bastian


1 Answers

With help from guadec, I found out I could use GLib's g_build_filenamev () function.

let filePath = GLib.build_filenamev([folderPath, fileName]);

This builds a path to the file and respects the platform at the same time.

Note: it requires that you import GLib first at the top of your GJS file, like this:

const { GLib } = imports.gi;
like image 187
Bastian Avatar answered Feb 01 '26 22:02

Bastian