Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
How could I duplicate/copy file in an automatized way with JavaScript?

I have an gif file that is stored in a directory call assets on my computer. I would like to create X amount of duplicates and they should be stored in the same directory and each of them should have a different file name.

Example:

I in the assets directory is the gif file call 0.gif I would like to duplicate this gif file 10 times and The duplicates should be called 1.gif, 2.gif, 3.R and so on.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

const fs = require("fs")
const filename = "index.js".split(".") //filename like 0.gif to gif
const times = 10 // number of times to duplicate

for(var int = 1; int < times; int++){
    const newFilename = `${(parseInt(filename[0]) + init)}.${filename[1]}`  //new filename like 0.gif to 1.gif
    fs.copyFileSync(filename, newfilename)
}

use the write file and read file from the fs module and a simple for loop

about 4 years ago · Juan Pablo Isaza Relatório

0

The simplest option is to use fs and using copyFile function available

const fs = require("fs");
const path = require("path");

let copyMultiple = (src, count) => {
 let initCount = 0;

 while (initCount < count) {
   initCount++;// you can put this at bottom too acc to your needs
   const newFileName = `${initCount}_${initCount}${path.extname(src)}`;
   console.log(newFileName, "is new file name");
   fs.copyFile(src, newFileName, (error) => {
     // if errors comes
     if (error) {
       console.log(error);
     }
   });
 }
};
copyMultiple("1.gif", 3);

Another elegant way of doing this is

const util = require("util");
const fs = require("fs");
const path = require("path");
const copyFilePromise = util.promisify(fs.copyFile);

function copyFiles(srcFile, destDir, destFileNames) {
  return Promise.all(
    destFileNames.map((file) => {
      return copyFilePromise(srcFile, path.join(destDir, file));
    })
  );
}

const myDestinationFileNames = ["second.gif", "third.gif"];
const sourceFileName = "1.gif";

copyFiles(sourceFileName, "", myDestinationFileNames)
  .then(() => {
    console.log("Copying is Done");
  })
  .catch((err) => {
    console.log("Got and Error", error);
  });

Using this will also give upperhand of knowing when it is done.

You can read docs here

about 4 years ago · Juan Pablo Isaza Relatório

0

not sure which framework you're on but fs.copyFile() is the standard way for node.js https://nodejs.org/api/fs.html#fscopyfilesrc-dest-mode-callback

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda