0

I'm comparing the output of childprocess.exec to a string, but I must have overlooked something since I didn't get the expected result.

function download_all(list, callback){
    var i=0, cmd="";

    function afterDownload(){...}

    while(i<list.length)
    {
        cmd="[ -f ./downloads/"+list[i]+" ] && echo \"E\" || echo\""+list[i]+"\"";
        exec(cmd, function(error, stdout, stderr){
            if(stdout=="E")
            {
                console.log("Already Exist");
            }else{
                console.log("download "+LINK+""+stdout);
                download(LINK+stdout, afterDownload());
            }
        });
        i=i+1;
    }

Basically, I check if a file exist, look at the output of the command, and if it is not E (which sign the file exist), download it. The problem is, even when the file exist, the app try to download LINK+E, which doesn't exist and of course fail.

I've tried with === instead of ==, and " instead of ', but it didn't changed anything.

Is there some character in stdout other than E?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • Why not use http://stackoverflow.com/questions/4482686/check-synchronously-if-file-directory-exists-in-node-js instead of shelling out from JS to Bash? – John Zwinck Aug 02 '13 at 11:40
  • Because I need to check the existence of the file, if there s a JS function for that, it would be perfect, but the only way I found is to use the shell – DrakaSAN Aug 02 '13 at 12:44

1 Answers1

1

NodeJS has the "fs" module which takes care of that for you. The documentation is at http://nodejs.org/api/fs.html

You can do this:

fs.exists(list[i], function (exists) {
  console.log("exists = ", exists);
});
randunel
  • 8,917
  • 1
  • 26
  • 24
  • Used fs.existsSync instead because I had parameters to send into the callback function, but work like a charm, thanks you a lot. – DrakaSAN Aug 02 '13 at 15:39