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

659
Views
Cambie el nombre de un archivo si ya existe en javascript incrementando file_1.txt, file_2.txt así. Este código no funciona

Probé este código, crea un archivo como file.txt como file_1.txt después de eso no lo incrementó, nuevamente di un nombre de archivo como file.txt, nuevamente muestra file_1.txt. Necesito aumentar el valor como file_1.txt,file_2.txt,file_3.txt así.

 while (await client.exists(path+'/'+filename)) { var ext = filename.split('.'); if (i == 0) { i = i + 1; filename = ext[0] + "_" + i + "." + ext[1] i++; var newfilename = filename // await client.put(buff,path+'/'+newfilename) console.log(newfilename) } else { file = ext[0].substr(0, ext[0].lastIndexOf("_" + i)); i = i + 1; filename = filename + "_" + i + "." + ext[1] } }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

No puedo ver en el código de dónde obtienes tu índice o yo

 var newIndex var newFilename function addNewFilename(fileName) { // find index with regex match number let number = fileName.match(/\d+/) // if number exist just increase the index if (number != null) { let currentIndex = number[0] newIndex = +currentIndex + 1 return newFilename = fileName.replace(/\d+/, newIndex); } // if number is null we add '_1' else { newIndex = '_1' let ext = fileName.split('.') return newFilename = ext[0] + newIndex + '.' + ext[1] } } let file1 = addNewFilename('file.txt') let file2 = addNewFilename('file_1.txt') let file3 = addNewFilename('file_31.txt') console.log('file1:', file1) console.log('file2:', file2) console.log('file3:', file3)

about 4 years ago · Juan Pablo Isaza Report

0

Puedes probar esto

 function getNewFileName(fileName){ const [name, ext] = fileName.split('.'); const nameParts = name.split('_'); let suffixNumber = ''; if(nameParts.length > 1){ //name has _n suffix const n = Number(nameParts.pop()); suffixNumber = n+1; fileName = nameParts.pop()+'_'+suffixNumber; } else{ //name does not have number suffix, so assign it to 1, so that fileName would be file_1.ext suffixNumber = 1; fileName = nameParts.pop()+'_'+suffixNumber; } console.log(fileName); } // Just to show the output in the console // Remove these lines once adding this code to your file getNewFileName('file.jpg'); getNewFileName('file_1.jpg'); getNewFileName('file_2.jpg'); getNewFileName('file_3.jpg'); //----------

Y puedes usar esto de la siguiente manera

 while (await client.exists(path+'/'+filename)) { filename = getNewFileName(filename); }
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!