Unable to write to text file inside plugin folder

async function writeFile() {
   try {
      let myFolder = await require('uxp').storage.localFileSystem.getPluginFolder()
      let newFile = await myFolder.createFile("textFile.txt", { overwrite: true });
      console.log(newFile.mode)
      await newFile.write("It was a dark and stormy night."); 
   } catch (error) {
      console.log(error)
   }
}

console.log(newFile.mode) outputs

Symbol(readWrite)

yet I get the following error:

Error: The file uses a storage provider that is read-only.

1 Like

Plugin folder, as stated in the docs, is read-only. I believe you should use data folder. At least I don’t have any issues when using getDataFolder()

3 Likes

you’re right, I actually missed that. thank you!

1 Like

Hi @Maher, regardless of where I save the text file, would you know if it is possible to provide ANSI encoding? Unfortunately I need to run a dynamic vbs file and it only works with this encoding.

I tried this, but without success:

myFolder.createFile("myFile.vbs", { overwrite: true, Encoding: "ANSI" })

hello, sorry for the late reply.
createFile does not provide an option to set encoding.

I thought of using require(‘fs’).writeFile() / writeFileSync(), but the docs does not mention “ANSI”

[options.encoding]
string
the encoding of the file can be “utf-8”, “utf-16be” or “utf-16le”

but I’d try anyway

resources that might help :
NodeJS - Saving TXT file with ANSI encoding
and if you don’t mind running a cmd:
UTF-8 to ANSI /file

1 Like