Check directory existence

Hello, i have this code that work properly, but i want to add a check to find if the folder already exist bfore to create it, can someone help me? Thank you

//parte di codice che chiede all'utente la cartella fissa e crea il token persistente

const fs = require('uxp').storage.localFileSystem;

const fixfolder = await fs.getFolder();   //apre una schermata che permette di scegliere la cartella

const persistentToken = await fs.createPersistentToken(fixfolder); //crea un token persistente per la cartella

localStorage.setItem('fixfolder', JSON.stringify(persistentToken));  //associa il token creato alla entry della cartella

//parte di codice che richiama il token salvato per la cartella 

const app = window.require('photoshop').app

const batchPlay = require('photoshop').action.batchPlay;

const fs = require('uxp').storage.localFileSystem;

//seleziona il token salvato

token = await JSON.parse(localStorage.getItem("fixfolder")); 

//estrae il percorso della cartella persistente

const folder = await fs.getEntryForPersistentToken(token); 

//Crea le cartelle desiderate e le sottocartelle)

const AdobeRGB = await folder.createFolder("AdobeRGB");  
const AdobeTIF = await AdobeRGB.createFolder("AdobeTIF");
const AdobeJPEG = await AdobeRGB.createFolder("AdobeJPEG");

//crea il file in modalitĂ  modale

require('photoshop').core.executeAsModal(async () => {  

const newFile = await AdobeTIF.createFile("prova.tif", { overwrite: true });
const saveFile = await fs.createSessionToken(newFile);


const result = await batchPlay(
      "_obj": "save",
        "_obj": "TIFF",
          "_enum": "platform",
          "_value": "IBMPC"
          "_enum": "encoding",
          "_value": "RLE"
      "in": {
        "_path": saveFile,
        "_kind": "local"
  ], {

  "synchronousExecution": false,
  //"modalBehavior": "fail"


You can use getEntries to get any files and sub-folders in your folder and then use .filter() to filter the returned array to only sub-folders (note the use of UXP’s isFolder method), and then use find() to search for the specific subfolder by name. If it exists then you now have it assigned to a variable, and if not it returns undefined
Finally, you can test the folder variable’s truthiness with an if statement.

const folder = await fs.getFolder();

const entries = await folder.getEntries();

const AdobeRgbFolder = entries.filter((entry) => entry.isFolder).find((subFolder) => === "AdobeRGB");

if (!AdobeRgbFolder) {
    // create folder
1 Like

i have tested it, but on’t work with subfolder of other subfolder, have you solution?

Do you mean that you also want to check if any sub-folders contain your named folders?

1 Like

yes, its right

thank you

AbobeRGB contain two subfolder, AdobeTIF and AdobeJPEG.

I need to check also if these sub-subfolder exist

That would be exactly the same as my example but with your AdobeRGB folder as the parent.
You’ll also need to tweak the find() for whichever folder you are trying to find.

1 Like

If i want to find the AdobeTIF folder, that is inside AdobeRGB folder, what code i have to write?

Thank you

Perhaps take some time to study the example?


This work:

const entries = await folder.getEntries();

const AdobeRgbFolder = entries.filter((entry) => entry.isFolder).find((subFolder) => === "AdobeRGB");

if (!AdobeRgbFolder) {
    const AdobeRGB = await folder.createFolder("AdobeRGB");

const entries2 = await AdobeRGB.getEntries();

const AdobeTIFFolder = entries2.filter((entry) => entry.isFolder).find((subFolder) => === "AdobeTIF");

if (!AdobeTIFFolder) {
    const AdobeTIF = await AdobeRGB.createFolder("AdobeTIF");

is it correct?

Thank you for your help

Looks good to me.

As you’re now duplicating code it would be better to wrap it all up into a helper function.

const findOrCreateFolder = async (parentFolder, folderName) => {
   const entries = await parentFolder.getEntries();

    const foundFolder = entries.filter((entry) => entry.isFolder).find((subFolder) => === folderName);

    if (foundFolder) {
        return foundFolder

    return await parentFolder.createFolder(folderName)

1 Like

the function work only with the “folder” how parent folder argoument, if i create a folder with

findOrCreateFolder = async (folder, "AdobeRGB")

it works, after this i use

findOrCreateFolder = async (AdobeRGB, "AdobeTIF")

gave me an error, AdobeRGB is not defined

I’m not sure you understand how to use functions?

You need to call the function elsewhere in your code and pass the real values to it.
Those arguments you have changed are proxies for those values so that the function becomes re-usable with different values.

To call the function you just use the function name and parentheses:
findOrCreateFolder(folder, "folder name")

But as this particular function returns a value you want to assign it to a variable:
const theFolder = findOrCreateFolder (folder, "folder name")

The final consideration is that it is an asynchronous function and so will need to be called from within another async function and pre-pended with await:

const main = async () => {
   // The  findOrCreateFolder function and all other code goes here - including whatever defines the folder variable that is going to be passed to findOrCreateFolder 
   const theFolder = await findOrCreateFolder(folder, "folder name");

1 Like

Thank you very much, now i understand why was undefined

1 Like