Search code examples
gtkgtk3glibgjs

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


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)?


Solution

  • 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;