0

So I have the following code

var processed;
fs.readFile(path, 'utf-8', function(err, data) {
    processed = false;
    //checking if text is in file and setting flag
    processed = true;
});

if (processed == true) {
    try {
        var fname = path.substring(path.lastIndexOf("\\") + 1);
        fs.moveSync(path, './processedxml/' + fname, {
            overwrite: true
        })
    } catch (err) {
        console.log("Error while moving file to processed folder " + err);
    }

}

But I don't get the desired output. Because looks like the readfile is executed by a separate thread and so the value of "processed" is not reliable.

I am not very familiar with nodejs so any help will be greatly appreciated.

Vasan
  • 4,810
  • 4
  • 20
  • 39
Aparna
  • 835
  • 2
  • 23
  • 47

2 Answers2

0

Yes, you are right, your executions are performed by different threads.

In this scenario, you'll need to use promises.

You can solve your need easily by using "Promise FS" (you can use any other promise solution anyway).

Your code would be something like the following:

fs = require('promise-fs');

var fname = 'test.txt' ;
var toMove = false ;

fs.readFile('test.txt','utf8')
    .then (function (content) {
        if(content.indexOf('is VALID') !== -1) {
            console.log('pattern found!');
            toMove = true ;
        }
        else { toMove = false
        }
        return toMove ;
    }).
    then (function (toMove) {
           if(toMove) {
              var oldPath = 'test.txt'
              var newPath = '/tmp/moved/file.txt'
              fs.rename(oldPath, newPath, function (err) {
                if (err) throw err
                console.log('Successfully renamed - moved!')
              }) ;
           }
    })
    .catch (function (err) {
        console.log(err);
    })

Create a file "test.txt" and add the following contents:

this is text.file contents
token is VALID

The code above will evaluate if "is VALID" is present as content and if it does then it will move the file "test.txt" from your current folder to a new one called "moved" in "/tmp" directory. It will also rename the file as "file.txt" file name.

Hope it helps you.

Regards

Daniel Vukasovich
  • 1,692
  • 1
  • 18
  • 26
0

It looks like you're shadowing path, trying to use it as a variable and as a node module. The easiest way to make this work is to choose a different variable name for the file and move the processing logic into the callback of fs.readFile.

var path = require('path');
var fs = require('fs-extra');

var file = 'some/file/path/foo.xml';
var text = 'search text';

fs.readFile(file, 'utf-8', function (err, data) {
    if (err) {
        console.error(err);
    } else {
        //checking if text is in file and setting flag
        if (data.indexOf(text) > -1) {
            try {
                var fname = path.basename(file);
                fs.moveSync(file, './processedxml/' + fname, {
                    overwrite: true
                })
            } catch (err) {
                console.log("Error while moving file to processed folder " + err);
            }
        }
    }
});
Jake Holzinger
  • 5,783
  • 2
  • 19
  • 33