0

I am trying to make an image organizer app , which searches images using tag's ,

So I want the user to select the image they want, so far I have done this by the following code

// renderer process

$("#uploadImage).on("click", (e) => {
    ipcRenderer.send('dialoguploadImage')
});

this is the main process

ipcMain.on('dialoguploadImage', (e) => {
    dialog.showOpenDialog({
        properties: ['openFile']
      }).then(result => {
        sendBackimagePathFromMain(result.filePaths[0])
      }).
      catch(err => {
        console.log(err)
      })
});
function sendBackimagePathFromMain(result) {
    mainWindow.webContents.send('imagePathFromMain',result)
}

so I have the image path, and the only thing I want to know is how can I duplicate this image, rename it, cerate a new folder and save the image in that folder like for example to this folder

('./currentDirectory/imageBackup/dognothapppy.jpg')

1 Answers1

1

You can use fs.mkdirSync() to make the folder and fs.copyFileSync() to 'duplicate and rename' the file (in a file system, you don't need to duplicate and rename a file in two different steps, you do both at once, which is copying a file), or their async functions.

const { mkdirSync, copyFileSync } = require('fs')
const { join } = require('path')

const folderToCreate = 'folder'
const fileToCopy = 'selectedFile.txt'
const newFileName = 'newFile.txt'
const dest = join(folderToCreate, newFileName)

mkdirSync(folderToCreate)
copyFileSync(fileToCopy, dest)
programmerRaj
  • 1,810
  • 2
  • 9
  • 19
  • what if the folder already exists? should i use if statement to check if it exists ?? – Doctor Pinocchio Jun 23 '21 at 08:22
  • You can use [`@appgeist/ensure-dir`](https://www.npmjs.com/package/@appgeist/ensure-dir) or [`fs-extra`](https://github.com/jprichardson/node-fs-extra/blob/HEAD/docs/ensureDir-sync.md). Or you can handle and ignore `EEXIST` errors when creating the folder. – programmerRaj Jun 23 '21 at 19:17