2016-09-20 3 views
1

現在、Windowsの特定のフォルダにあるいくつかのファイルをnodegruntで検索しようとしています。Node.jsでdirのファイルを検索する

私はJSONファイルとディレクトリを読み出す機能を持っていますが、問題は、私はタスクを実行すると、ファイルを読み込むためのコードが何もしないということで、完璧なものgrunt task実行上の他のすべてgrunt taskを持っています、 でもあの。パスの参照が正しいかどうかはわかりませんが、path.normalize()も使用しており、エラーは発生しません。

これは、コードのスニペットです:

..// 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 
} 

私は、タスクを実行すると、コードのこのスニペットはスキップされている理由、誰もがアイデアを持っていますか?私はおそらくいくつかの基本的なものを見逃す可能性があります。ありがとう!

+0

スラッシュがパス名をエスケープしています –

+0

"C:\ Users \\ ..."または "C:/ Users /"を使用 – Draykos

答えて

2

readdirSyncを試して、機能がまだ動作していないかどうかを確認してください。私はあなたのプロセスがコールバックの前に終わったと思います。

+0

あなたは正しいと思われます、ベストプラクティスのためにAsyncを読む代わりの方法があります。 –

0

あなたが任意のオペレーティングシステム上で、Windowsのファイルパスを扱うとき

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

またpath.win32を使用し、一貫性のある結果を得るために、あなたのパスを変更必要があります。

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

あなたはおよそhttps://nodejs.org/api/path.html#path_windows_vs_posix

を読むことができます
0

パスのスラッシュがエスケープされています。

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

で問題を解決する必要があります。

1

あなたは、単に現在のスクリプトが実行されているパスを取得するには__dirnameオブジェクトを使用することができます。

..// 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 
} 

あなたはより多くの情報hereを見つけることができます。

関連する問題