I am trying to delete directories so i wrote a code
const path= require("path"),
const fs= require("fs");
let dir= path.join(process.cwd(),"tested folders");
if(fs.existsSync(dir)){
fs.rmSync(dir, { recursive: true, force: true });
}
and It is helping me to delete the desire directories
but now I am trying to delete multiple directories as I want to delete all the files who starts with _testFolder_ as there are multiple files with this prefix
so I made an array and pass the value like this
let dir=[path.join(process.cwd(),"tested folders"),path.join(process.cwd(),"_testFolder_*")]
if(fs.existsSync(dir)){
fs.rmSync(dir, { recursive: true, force: true });
}
and trying to delete multiple folder but it not letting me delete even my first folder also how can I delete multiple folder which start with this prefix _testFolder_ and tested folder too
here is the code, you can delete all folders
NOTE:- here command is for Linux for another os you can replace it
const { exec } = require('child_process');
const path = require("path")
let dir = [path.join(process.cwd(), "tested folders"), path.join(process.cwd(), "_testFolder_*")]
// linux command rm -rf
exec(`rm -rf "${dir.join('" "')}"`);
** choosing child process because it reduces code and speed
using Node js
let fs = require('fs')
// your dir array should be (simple regex)
let regex = [/tested folders/, /_testFolder_/]
let scanDir = process.cwd()
fs.readdirSync(scanDir)
.filter(f => regex.filter(r => r.test(f)).length)
.map(f => fs.rmdirSync(scanDir + "/" + f))