Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

223
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda