Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

fs.readdir permission denied to read directory, but I can read files from directory

Explorer ,
Jan 13, 2023 Jan 13, 2023
const fs = require("fs");
const paths = fs.readdirSync("test");
console.log(paths);

above gives the error: "Permission denied to read  directory. Engine: UXPScript File: I (uxp://uxp-internal/webfs-scripts.js)"

The below works fine:

const fs = require("fs");
let txt = fs.readFileSync("test\\test.txt", {encoding: "utf-8"});
console.log(txt);

lstatSync gives [console] {"_info":{"isFile":false,"isDirectory":true}}
Giving it all possible permissions, and opening InDesign as admin didn't help.

I'm on Windows 10 Pro

 

Is there any other way to grep the content of the directory?

TOPICS
UXP Scripting
1.0K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Jan 14, 2023 Jan 14, 2023

You may need to specify a "file:" scheme.
In my case the following UXP Script worked.

const myFs = require("fs");
const myFilesAndDirectoriesList  = myFs.readdirSync("file:C:/Users/user/Desktop");

 

 

Translate
Engaged ,
Jan 14, 2023 Jan 14, 2023

You may need to specify a "file:" scheme.
In my case the following UXP Script worked.

const myFs = require("fs");
const myFilesAndDirectoriesList  = myFs.readdirSync("file:C:/Users/user/Desktop");

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 14, 2023 Jan 14, 2023

Thank you very much! It works if I provide a full path.

However, it reads files from the local path, but returns a blank error message and  "-4058" in the console if  I try local path to the folder like this:

"file:test"
"file:\\test"
"file:test\\"
"file:\\test\\"

 What is "4085"? Is there any way around it?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Jan 14, 2023 Jan 14, 2023

I don't know if local path can be used.

You can get home directory path.

require("fs").readdirSync(`file:${require("os").homedir()}/Desktop`);

 

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Jan 15, 2023 Jan 15, 2023
LATEST

Thank you very much! It solves it. Also, I discovered another method that gives me the whole path:

app.activeScript

On Widows:

C:\Users\<User>\AppData\Roaming\Adobe\InDesign\Version 18.0\en_US\Scripts\Scripts Panel\test.idjs

I just have to replace to remove the text after the last slash and add the folder name.

let scriptPath = app.activeScript;
if (/\//.test(scriptPath)) {
    scriptPath = scriptPath.replace(/(.*\/).*$/, "$1");
}else{
    scriptPath = scriptPath.replace(/(.*\\).*$/, "$1");
}const paths2 = fs.readdirSync(`file:${scriptPath}test`);

Hope it will be useful for others too.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines