Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
delete multiple folder with prefix using javascript / node js

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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))
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!