How do I find the path to documents folder? NW.JS

0 favourites
  • 5 posts
From the Asset Store
Move around the rectangular and travel as much as you can!
  • Most modern games create save files in the default Documents folder, for example \Documents\StudioName\GameName\save01.dat

    How do I find the path to this folder with NWJS?

    NWJS.UserFolder returns the path to my user folder, which is "C:\Users\dop2000\"

    The documents folder is there, but depending on Windows version it may be named "Documents" or "My Documents" or may be even be in some other language. So how do I locate it?

    And what is the best location for game saves on Mac?

    Tagged:

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • I found this which is for desktop but is likely the same for documents. On windows at least, any language will have a "desktop" folder which points to the same folder with the translated version.

    stackoverflow.com/questions/57742157/node-js-how-to-find-the-desktop-path

    So for windows at least it would be this. Which will work for any language.

    NWJS.UserFolder&"Documents\"

    or you can also do a check to see if that folder exists and use this if it doesn't.

    NWJS.UserFolder&"my documents\"

    For mac and linux you can try. Notice the other slash. Actually you could probably use this for windows too. Windows doesn't care what slash you use.

    NWJS.UserFolder&"Documents/"

    I'm not sure if mac and linux do the same thing with different languages as windows. It looks like those are valid folder on those platforms like windows too.

    Worst case you can first see if the folder exists, and create it if it doesn't.

  • Thanks, this is really helpful!

    There is one problem though - you can change the location of Documents folder in Windows. I don't think many people do this, but still.. In that case the folder may not be in the home directory at all.

    I'm surprised there is no default environment variable in Windows like %DOCUMENTS%.

    It is possible to get the folder location from windows registry, but I'm not sure how to do this in Construct. Run File "reg.exe query key_name >> file.txt", and then read that file?

  • I think we are limited but what modules are included in nwjs. Unless you know how to add more?

    Running regedit to find the documents folder is one option. But seems messy.

    There is a winapi function that you can use to get the location of the documents folder, but it's only accessible from c. Or we could get it with the node ffi module if it was included with nwjs.

    Anyways here is the source of a simple c program that just prints the users documents folder location.

    getDocumentsFolder.c

    #include <stdio.h>
    #define CSIDL_PERSONAL 5
    #define WIN32_LEAN_AND_MEAN
    #include <windows.h>
    #include <tchar.h>
    
    int main()
    {
    	TCHAR DocumentsPath[MAX_PATH];
    	SHGetSpecialFolderPathW(NULL, DocumentsPath, CSIDL_PERSONAL, 1);
    	wprintf(L"%ls", DocumentsPath);
    	return 0;
    }

    To build that with the tiny c compiler

    tcc getDocumentsFolder.c -l"shell32"

    Then to run it you need to run basically this javascript:

    require("child_process").execFileSync("getDocumentsFolder.exe",null,{encoding:"utf8"})

    Basically it synchronously runs a file and returns the output of the program as utf8 text. I had to modify it slightly as it needs the full path of the program.

    Anyways here is my test of it working in nwjs from c2. It only works on export at the moment though. It includes that exe compiled from the source above.

    dropbox.com/s/cpwuxmmj7ekocxx/nwjs_getDocumentsFolder.capx

  • This is great, thank you so much!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)