0

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

Patrick Lafferty
  • 337
  • 1
  • 7
  • 20
  • Please clarify why all of the methods you've tried have failed (by [edit]ing your question). `fs.copyFile()` is the most portable and unless you're trying to access locations which are protected by the OS, there should be no problem. Thus, the error message would be helpful. Also, show us the code you've used. Thanks! – Alexander Leithner May 27 '21 at 15:29
  • @AlexanderLeithner - edits are complete. – Patrick Lafferty May 27 '21 at 15:51
  • Where are you executing this code? Code executed in the renderer can't access modules unless you use `nodeIntegration: true` or `preload` script – cachique May 27 '21 at 17:40
  • I am using `nodeIntegration: true` and I'm using Browserify to bundle my JavaScript code into Bundle.js. This file is then referenced in my html. i.e. `` – Patrick Lafferty May 28 '21 at 09:01

2 Answers2

0

The npm package fs-extra should solve your problem.

It has the move function, which

Moves a file or directory, even across devices

Geshode
  • 3,600
  • 6
  • 18
  • 32
  • this was also tried but has a similar result to fs. I.e. function was not defined. Is using Browserify a round about way and a back door for working with node js in an electron project the right way? – Patrick Lafferty May 27 '21 at 15:59
  • Electron remove is supposedly deprecated according to https://www.electronjs.org/docs/api/remote – Patrick Lafferty May 28 '21 at 09:15
0

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.

Patrick Lafferty
  • 337
  • 1
  • 7
  • 20