Doctor Pinocchio
Doctor Pinocchio

Reputation: 35

how to copy an image and save it in a new folder in electron

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')

Upvotes: 0

Views: 1289

Answers (1)

programmerRaj
programmerRaj

Reputation: 2058

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)

Upvotes: 1

Related Questions