0

I'm using node.js for converting text files to CSV. It works for one file, but when i try process more files fileDestination variable doesn't change. Why? Input files like this: r10_1C_BP1-11_e41-81_10_5X1x9_05_train2.res I got following console output:

./1_train.csv has been written successufully! r10_1C_BP1-11_e41-81_10_5X1x9_05_train2.res ./1_train.csv has been written successufully! r10_1C_BP1-11_e41-81_1_5X1x9_05_train2.res

/*
 * lee archivos *.dat y los convierte *.csv
 */

const fs = require('fs');
const inputDir = './';
const outputDir = './';


function readFiles(inputDir, onError) {
  fs.readdir(inputDir, function(err, filenames) {
    if (err) {
      onError(err);
      return;
    }
    filenames.forEach(function(inputFile) {
      // first we arre looking for "right" file name
      if (inputFile.search(/res/) != -1) {
        console.log('Starting processing ' + inputFile);
        convert2csv(inputFile, function(error) {
          throw err;
        });

      }
    });
  });
}

function convert2csv(filename, onError) {
  arrayFromFilename = filename.split('_');
  epoca = arrayFromFilename[4];
  trainORval = arrayFromFilename[7].replace('2.res', '');
  console.log("from convert " + filename + " " + epoca);
  fs.readFile(inputDir + filename, 'utf-8', function(err, content) {
    if (err) {
      onError(err);
      return;
    }
    content = content.replace(/^[^0].*\n/mg, '');
    arr = content.split('\n');
    pares = arr.filter(function(d, i) {
      return i % 2 == 1;
    });
    content = pares.join('\n');
    content = content.replace(/(^[\d.]*) ([\d.]*)/gm, '$1,$2');
    fileDestination = outputDir + epoca + '_' + trainORval + '.csv';
    console.log("filedestination :" + fileDestination);
    fs.writeFile(fileDestination, 'y,x\n', function(err) {
      if (err) {
        return console.error(err);
      }
      fs.appendFile(fileDestination, content, function(err) {
        if (err) {
          return console.error(err);
        }
        console.log(fileDestination + " has been written successufully!", filename);
      });
    });
  });
}
cacoch
  • 87
  • 2
  • 4
  • 2
    Declare your variables. Without using [`var`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var) or [`let`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/let), they [automatically become globals](https://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it), so you only have one `fileDestination` for all of the calls to `convert2csv()` to share and reassign. – Jonathan Lonowski Jun 08 '16 at 03:54
  • Since most of your variables are globals and only exist once, they have the same behavior as a [loop counter when using asynchronous functions](https://stackoverflow.com/questions/750486/javascript-closure-inside-loops-simple-practical-example). `convert2csv` can already be a closure, if the variables are declared local to it. – Jonathan Lonowski Jun 08 '16 at 04:02
  • Prepend `var` to `epoca` and `trainORval` doesn't works. i got `./10_train.csv has been written successufully` twice. – cacoch Jun 08 '16 at 04:03
  • You should also declare `arrayFromFilename`, `arr`, `pares`, and `fileDestination` along with `epoca` and `trainORval`. – Jonathan Lonowski Jun 08 '16 at 04:06
  • Use `var` or `let` when you define a variable, otherwise you are making them global and I'm pretty sure that's the problem – Molda Jun 08 '16 at 04:16
  • magic `let` for all *variables* in function `convert2csv` solve problem – cacoch Jun 09 '16 at 01:03

0 Answers0