How can I use localStorage to store folder?

var folder = await uxp.storage.localFileSystem.getFolder();

How can I set and get the variable?

Could you explain a bit more what you’re trying to do?
Which variable do you want to set or get? In the variable “folder” you’re storing a reference to an entry (folder) of your file system.
The localStorage is not directly related to the file system, it’s for storing key value pairs. You can store some stringified objects in there, but a folder is probably not serializable. You could store a folder’s name for example.

You want a persistent file token. The documentation has an example of doing exactly this with localStorage. Tokens are strings and will store quite nicely in localStorage.

Then, to get the actual folder, you want to use this part of the documentation.

1 Like

Thank you! :blush::blush::blush::blush::blush::blush::blush::blush::blush::blush:

Thank for the example code on the persistent file tokens.

Just curious… in your example it has a while loop that allows it 3 attempts to get the entry. Why is that needed? Does it sometimes fault out trying to get the entry even if the entry is there?

A persistent token doesn’t guarantee access to the underlying resource – the OS or the user could choose to revoke those permissions or move the file, etc. So the loop is there to catch the failure and then ask the user for the new location in the event of a failure. You could do this any number of ways; a loop is just one of several. You also don’t have to do it for three times or the like – it’s just used as an example here to give the user a couple tries in case something fails (perhaps a permission error).

1 Like