1

I'm currently trying to search for a few files in a specific folder on Windows using node and grunt.

I have a grunt task that has a function to read a dir with JSON files, but the problem is that when I run the task, the code to read the file doesn't do anything, everything else on that grunt task runs perfect, but that. I'm not sure if the reference for the path is correct, but I'm also using path.normalize() and it does not throws any error.

This is snippet of the code:

..// Some other code
var fs = require('fs'),
path = require("path");

grunt.registerTask('separate', function() {
var filePath = path.normalize("C:\Users\jbernhardt\Desktop\testkeeper\jenkinsReports");

fs.readdir(filePath, function(err, filenames) {

  //This log doesn't show as it the function is not running
  grunt.log.writeln("Testing");

    if (err) {
        grunt.log.writeln("Error");
        return;
    }
    filenames.forEach(function(filename){
        grunt.log.writeln("Testing");

    });

  });
...//Some more code below for the same task
}

Does anyone has an idea why this snippet of the code is being skipped when I run the task? I could probably be missing some basic stuffs. Thanks!

2
  • your forward slashes are escaping your path name Commented Sep 20, 2016 at 14:59
  • Use "C:\\Users\\..." or "C:/Users/" Commented Sep 20, 2016 at 15:00

4 Answers 4

2

Try readdirSync and check if your function still not working. I guess your process is finished before the callback.

Sign up to request clarification or add additional context in comments.

1 Comment

Seems that you are correct, I tried that and its working!, I'll see for an alternative to read Async for best practice.
1

You can simply use the __dirname object to get the path where the current script is running:

..// Some other code
var fs = require('fs'),
path = require("path");

grunt.registerTask('separate', function() {

fs.readdir(__dirname, function(err, filenames) {

  //This log doesn't show as it the function is not running
  grunt.log.writeln("Testing");

    if (err) {
        grunt.log.writeln("Error");
        return;
    }
    filenames.forEach(function(filename){
        grunt.log.writeln("Testing");

    });

  });
...//Some more code below for the same task
}

You can find more info here.

Comments

0

you need change your path

var filePath = path.normalize("C:\\Users\\jbernhardt\\Desktop\\testkeeper\\jenkinsReports");

Also To achieve consistent results when working with Windows file paths on any operating system, use path.win32:

path.win32.basename('C:\\Users\\jbernhardt\\Desktop\\testkeeper\\jenkinsReports"');

You can read about https://nodejs.org/api/path.html#path_windows_vs_posix

Comments

0

The slash in path are being escaped.

"C:\\Users\\jbernhardt\\Desktop\\testkeeper\\jenkinsReports" 

should solve your issue.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.