0

I´ve tried to copy a lot of files in folder by "ncp", but every time I check if the files are copied, not all of them are there. There are always some files are missing.

I´ve tried to use fs.readdir to check that are all files there but fs gives me the same result there are some files are missing

my ncp code

ncp(path.join(req.body.uploadPath,req.body.packageName,resourceDirectoryName), path.join(req.body.uploadPath,req.body.packageName),  (err) => {
    if (err) {
     //devmode report
     devMode.report('**error** '+err); 
    }
})

my fs code

fs.readdir(path.join(req.body.uploadPath,req.body.packageName,resourceDirectoryName), (err, files)=>{
              console.log('####', files)
})

And this is the fs.readdir result

enter image description here

This is the folder with all files that I want to copy

enter image description here

This is the list of files it didn't copy

enter image description here

Mahmoud Niypoo
  • 1,598
  • 5
  • 24
  • 41

1 Answers1

1
  • CLUE 1:

    Check the order reported on your fs.readdir

  • CLUE 2:

    Now, as it´s alphabetical order, look for the next ocurrence in your list (alphabetically)

  • CLUE 3:

    Check for special characters on it´s name.

  • CLUE 4 - TEST A POSSIBLE SOLUTION*

    As you can see, there is a space on the name of `new dictionary.json". Remove the spaces on this name and try again.

  • CLUE 5 - Change your call, and add StopOnError

Add an StopOnError option to show if it stops there and why

ncp ( 
  path.join(req.body.uploadPath, req.body.packageName,   resourceDirectoryName),
  path.join(req.body.uploadPath,req.body.packageName), 
  {stopOnErr: true},
  (err) => {
        if (err) 
        {
          return console.error(err);
        }
      console.log("ok");
   }
)

SOLUTION

After dealing with this for a little of time, the problem was located: The user was executing the fs.readdir right after the execution of NCP. As NCP is asyncronous, fs.readdir was not reading the whole bunch of copied files, as they were still not copied.

Then, just a delay before fs.readdir was the solution to see all of them. Not a Spaces or special characters thing.

FINALLY, A REAL SOLUTION

As async programming has a first-day-of-school where everybody learns how to deal with callbacks, there is an example to deal with callbacks, and solve the question.

var ncp = require('ncp').ncp;
var fs = require('fs');


ncp.limit = 16;
var source="dir1"
var destination="dir2"
console.log('Starting');
console.log(new Date().toISOString());

ncp(source, destination, mycallback );

function mycallback(err)
{
 //here we are inside a callback declared in an outer function
 if (err) {
   return console.log(err);
 }
 console.log('done!');
 console.log(new Date().toISOString());
 fs.readdir(destination, function (err, files){
            console.log("Files Copied to Dir2 "+ files.length);
            //here we are inside fs.readdir callback
            copyTwo();
              });
 //as fs.readdir is also async, I cannot execute copyTwo() here, and I must execute inside fs.reddir callback
}


function copyTwo()
{
console.log('Starting CopyTwo');
console.log(new Date().toISOString());
source="dir2"
destination="dir3"
  //This function uses an embedded callback
  ncp(source, destination, function (err)
  {
   // here we are inside the function declared (with-no-name) to use directly asembedded  callback
   if (err) {
     return console.log(err);
   }
   console.log('done!');
   console.log(new Date().toISOString());
   fs.readdir(destination, function (err, files){
              console.log("Files Copied to Dir3 "+ files.length);
                });
  });
}




[admin-sp@localhost pruebaApp]$ ls dir1 | wc -l
7777
[admin-sp@localhost pruebaApp]$ ls dir2 | wc -l
0
[admin-sp@localhost pruebaApp]$ ls dir3 | wc -l
0
[admin-sp@localhost pruebaApp]$ node index.js
Starting
2018-05-01T00:55:50.683Z
done!
2018-05-01T00:55:57.063Z
Files Copied to Dir2 7777
Starting CopyTwo
2018-05-01T00:55:57.069Z
done!
2018-05-01T00:56:03.030Z
Files Copied to Dir3 7777
[admin-sp@localhost pruebaApp]$
  • I tried this but give me "ok" on console and the same issue is exist some file not copied – Mahmoud Niypoo Apr 25 '18 at 11:20
  • Did you tried deleting the space on the `new dictionary.json` , renaming as `new_dictionary.json` for example? – Alejandro Teixeira Muñoz Apr 25 '18 at 12:31
  • no , i didn't because in normal i don't know files's name in folder , i just after that issue i open folder and check files's name – Mahmoud Niypoo Apr 25 '18 at 12:33
  • Here, they also say you need the filename on destination. Maybe this need is related to the space on filename.. ? https://stackoverflow.com/a/38639253/3617531 – Alejandro Teixeira Muñoz Apr 25 '18 at 12:37
  • I don't guess that because there is a file also sw.js it hasn't space and not copied – Mahmoud Niypoo Apr 25 '18 at 12:39
  • stackoverflow.com/a/38639253/3617531 in this ask topic he copy one single file he know its name already but i copy whole folder – Mahmoud Niypoo Apr 25 '18 at 12:42
  • Did you checked it? That is an important point. If you Guess you dont need to, you should not ask for help.... – Alejandro Teixeira Muñoz Apr 25 '18 at 13:26
  • sir did you see this img "https://i.stack.imgur.com/uWGSH.png" this is a list of files npc and fs cant read and copy them , it isn't case of space in name it's something else – Mahmoud Niypoo Apr 25 '18 at 13:37
  • ok, and..... can you guess why are exactly the next ones to “new dictionary.json”???? – Alejandro Teixeira Muñoz Apr 25 '18 at 13:59
  • i don't know why , but i try it in Linux and fs.reddir catch these files [ '.DS_Store', 'appmanifest.json', 'box2d.asm.js', 'c2runtime.js', 'data.js', 'data.json', 'game.html', 'icon.png', 'icons', 'images', 'media', 'new dictionary.json' ] but still the files in the image fs can't read why !! i don't know – Mahmoud Niypoo Apr 25 '18 at 14:15
  • @MahmoudNiypoo I Told You: The files BEFORE the file with the space in the name, are copied. The files AFTER the file with the space in the name are NOT COPIED. **That shows, at least, a clear relation between the problem you report and this file**. .. If you are still thinking there is no relation... I don´t understand what you guess is the problem.... – Alejandro Teixeira Muñoz Apr 25 '18 at 15:01
  • i did what did you say and it's work but still some files not copy !! i don't know why i check all files's name to remove a space or simple but not found , what you guess ? – Mahmoud Niypoo Apr 25 '18 at 17:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169784/discussion-between-alejandro-teixeira-munoz-and-mahmoud-niypoo). – Alejandro Teixeira Muñoz Apr 25 '18 at 17:01
  • thank you very much you inspired me i figure out where is a problem , it isn't space , i read files before write them at all i delay the read step and yeaah I catch all files – Mahmoud Niypoo Apr 26 '18 at 10:32
  • :) I told you!!! It was so extrange. Dealing with async code is not as easy as it´s expected to!! :) – Alejandro Teixeira Muñoz Apr 26 '18 at 14:00
  • :( i didn't attention about async cause of I used callback from function , so i guess it's done and i ready to handle this callback , how can i avoid that error that association with async – Mahmoud Niypoo Apr 29 '18 at 13:41
  • added an answer about handling the callback!! :) hope it helps! – Alejandro Teixeira Muñoz May 01 '18 at 01:26