Search code examples
node.jsarrayscreate-directory

NodeJS - Create a new numbered subdirectory based on the last element of an array based subdir list (Closed)


I have this function that reads a specified working directory using fs.readdir, which then filters out all files and returns an array based list of the first set of subdirectories within the specified working directory, which then grabs the last element (in my case the last subfolder) from the bottom of the array based list.

This list of subdirectories is always returned in order, it's never jumbled.

The way I'm achieving this is with the following code:

var workDir = 'just/some/folder/location';

// Read the specified working directory 
fs.readdir(workDir, { withFileTypes: true }, (error, files) => {
    if (error) {
        //Throw an error if the folder can't be read
        console.log('Unable to read the Working Directory! \n\n')
        //Print the reason for the error
        console.log('REASON: ' + error);
        return;
            
    } else {
        // Filter out files and return only subdirectories
        var subdirList = files
            .filter((item) => item.isDirectory())
            .map((item) => item.name);
        // Get the last subdirectory from the returned list of subdirectories
        var lastFolder = subdirList[subdirList.length -1];
        
        // Function to create a new subdir 
        // Based on the last element/folder
        // Returned by the function, needs to go here
        // But I can't figure it out
    };

});

What I want to be able to do now, is create a new numbered subdirectory within the specified working directory based on the output of the function above.

So for example if the function above ends up detecting folder4 as the last subdirectory, how would I allow it to create folder5? or if the function detects folder5 as the last subdirectory, then how do we allow it to create folder6 and so on??

It's important to note that in my case, the subdirectories inside of my working Directories will always follow a number pattern of 2 through 1000.

Any help on this would be greatly appreciated.


Solution

  • EDIT : You just have to play a bit with the regex pattern, to match your exact requirement. I updated the regex to follow your convetion folder_classes[number].

    /[a-zA-Z]+|[0-9]+/g -> /[a-zA-Z_]+|[0-9]+/g

    To know how to name your new folder from the previous one If you have a strict convention like this for folder name [folder][number] : folder1, folder2, folder3,etc..

    You can use this sample to extract the number from the last folder and increase it

    const lastFolderName='folder_class123'
    /*
    You can use match function with a regex like this
    It will give you an array with the prefix string, and the number part as a string
    */
    const extract=lastFolderName.match(/[a-zA-Z_]+|[0-9]+/g)
    console.log(extract)
    // You should put some control, like is extract array length =2, etc..
    
    // You can convert the extracted string number of the folder as a number
    const lastFolderNumber=parseInt(extract[1])
    
    // Same here you can put some business rule, lastFolderNumber less than 1000
    const newFolderNumber=lastFolderNumber+1
    console.log(`New folder number : ${newFolderNumber}`)

    This should work for any number after the prefix : folder1, folder10, folder222, etc..