NodeJS traverses the file production file list
Here we will share with you that you can use node to know the file path, configure the remote path, and copy and publish the file directly. The new filelist. js file contains comments.
Var fs = require ("fs"); // path module, which can produce relative and absolute paths var path = require ("path "); // configure the remote path var remotePath = "/resource/fd/promote/201507/qixi/"; // obtain the absolute path of the current directory. Here, resolve () do not input the parameter var filePath = path. resolve (); // read the file storage array var fileArr = []; // read the file directory fs. readdir (filePath, function (err, files) {if (err) {console. log (err); return;} var count = files. length; // console. log (files); var results ={}; files. forEach (function (filename) {// filePath + "/" + filename cannot be connected directly with/. The Unix system is "/", and the Windows system is "\" fs. stat (path. join (filePath, filename), function (err, stats) {if (err) throw err; // file if (stats. isFile () {if (getdir (filename) = 'html ') {var newUrl = remotePath + filename; fileArr. push (newUrl); writeFile (fileArr);} // (getdir (filename) = 'html ') & (fileArr. push (filename); writeFile (newUrl); // console. log ("% s is file", filename);} else if (stats. isDirectory () {// console. log ("% s is a directory file directory", filename); if (filename = 'css '| filename = 'images ') {// var readurl = filePath + '/' + filename; // filePath + "/" + filename cannot be connected directly with/. The Unix system is "/", the Windows system is "\" // console. log (path. join (filePath, filename); var name = filename; readFile (path. join (filePath, filename), name) ;}}) ;}); // obtain the extension name function getdir (url) {var arr = url. split ('. '); var len = arr. length; return arr [len-1];} // get the file array function readFile (readurl, name) {console. log (name); var name = name; fs. readdir (readurl, function (err, files) {if (err) {console. log (err); return;} files. forEach (function (filename) {// console. log (path. join (readurl, filename); fs. stat (path. join (readurl, filename), function (err, stats) {if (err) throw err; // if (stats. isFile () {var newUrl = remotePath + name + '/' + filename; fileArr. push (newUrl); writeFile (fileArr) // is a subdirectory} else if (stats. isDirectory () {var dirName = filename; readFile (path. join (readurl, filename), name + '/' + dirName); // use arguments. callee (path. join () when using its own functions, an error is reported. // arguments. callee (path. join (readurl, filename), name + '/' + dirName) ;}});}) ;}// write to the filelisttxt file function writeFile (data) {var data = data. join ("\ n"); fs. writeFile (filePath + "/" + "filelist.txt", data + '\ n', function (err) {if (err) throw err; console. log ("successfully written ");});}