0

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.

4
  • are you using node.js? Commented May 22, 2022 at 6:39
  • @steve yes I am Commented May 22, 2022 at 6:41
  • use fs (file system) module to accomplish this. You can duplicate your files by fs.copyFile(). Commented May 22, 2022 at 6:48
  • What problem are you actually trying to solve by duplicating files? You could serve incoming requests for 1.gif, 2.gif, etc... all by serving the same single file if that's really what you want. The client wouldn't know the difference and then you wouldn't actually need duplicate files on your server. Commented May 22, 2022 at 7:00

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

3 Comments

This code provides no way to communicate back the caller when it's done.
@jfriend00 I have added the one which shows message when it is done
FYI, in modern nodejs, there is already a promisified version of copyFile fs.promises.copyFile here so no need to create your own.
2
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

2 Comments

Kind of inefficient from both code execution and memory usage to read the same file from disk into memory over and over again.
thanks i didn't know about the copyfile methord
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.