Best way to copy a directory from an external drive to a local folder with electronjs?

815 views Asked by At

Just wondering if anyone has ever attempted to copy a directory from an external drive (connected via USB) to a local folder.

I am using ElectronJS so I can use my JavaScript, HTML/CSS skills to create a desktop application without utilising a C language. (i.e. C# or C++) With ElectronJS there's a lot less to worry about.

Here is the list of things I've tried so far:

  • basic fs.copyFile (using copyFile intially and will then loop round the directory to copy all files)

     var fs = require('fs');
    
     window.test = () => {
    
     fs.moveSync("targetFile","destDir", function(err) {
         if(err){
           console.log(err);
         }else{
           console.log("copy complete")
         }
       });
      }
    

fs.moveSync is not a function even though Visual Studio Code brought up moveSync as a suggestion when I entered fs. (ctrl + space)

  • using child_process functions to copy files using the command line. Code is:

     var process = require('child_process')
    
     window.test = function(){
     process.exec('ipconfig', function(err, stdout, stderr){
         if(err){
             console.log(err);
         }else{
             console.log(stdout)
         }
     })
    }
    

Then bundled with browserify. Bundle.js is then imported into the html file and the test function is called on the click of a button. I'm aware the command is ipconfig for now, this was merely used to see if a command could be executed. It appears it could because I was getting process.exec is not defined.

  • use the node-hid node module to read and trasfer data from the external drive.

The exposed functions within this module were also reported as being undefined. And I thought about the use case longer I thought a simple copy process would suffice because external drive can be accessed like any other folder in the file explorer.

Unfortunately, all of the above have failed and I've spent the most part of the day looking for alternative modules and/or solutions.

Thanks in advance because any help to achieve this would be much appreciated.

Thanks

Patrick

2

There are 2 answers

0
Patrick Lafferty On BEST ANSWER

Ended up adding this to my preload.js for:

window.require = require;

It will work for now but is due to be depreciated. I'll use this for now and make other updates when I have to.

2
Geshode On

The npm package fs-extra should solve your problem.

It has the move function, which

Moves a file or directory, even across devices