Thursday, 25 May 2017

javascript: Creating directories from file path recursively adding timestamp to the filename

I am using the below code to split up a user provided path and attach a timestamp to the ending file name. I am splitting the path with / first and then using forEach over the resulting array. Is there any better way to do this in javascript / nodejs.

function test(usrPath) {
    var locMatches = usrPath.split("/")     
    locMatches.forEach (function (item) {
        location = pathUtils.join(location,item)
        if (!fs.existsSync(location)) {
            fs.mkdirSync(location)
        }
    })      
    return pathUtils.join (location,usrPath + (new Date).toISOString().replace(/(^\d\d\d\d)|-|:|(\..*)/g,"").replace("T","_")+".log")
}



via StillNestling

No comments:

Post a Comment