NodeJS traverses the file production file list

Source: Internet
Author: User

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 ");});}

 


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.