Example of NodeJS's function of traversing the file production file list

Source: Internet
Author: User
Tags function examples readfile
This article mainly introduces the node. js file list traversal function, and analyzes the node. js file and directory traversal, Judgment, read/write, and other related operation skills in the form of examples, for more information about how to use NodeJS to traverse the file production file list, see the following example. We will share this with you for your reference. The details are as follows:

Functional requirements: in our work, we may often need to know about the release of the static file list in the project. It is too hard to retrieve and write the static file list one by one.

If you want to know whether the file list is very painful, we may also have a dos tree to get it.

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); // return the extension name of the specified file name // console. log (path. extname ("pp/index.html"); 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 ");});}

When we execute

node flielist.js

The filelist.txt file contains the desired path.

Sometimes, we often encounter files that cannot be deleted. Because the file depth is too long, rm _ rf cannot be deleted, but we can do it through nodejs:

Var fs = require ("fs"); var path = require ("path"); deleteFolderRecursive = function (url) {var files = []; // determine whether the specified path exists if (fs. existsSync (url) {// array files of returned files and subdirectories = fs. readdirSync (url); files. forEach (function (file, index) {// var curPath = url + "/" + file; var curPath = path. join (url, file); // fs. statSync synchronously reads folder files. if it is a folder, trigger the function if (fs. statSync (curPath ). isDirectory () {// recurse deleteFolderRecursive (curPath); // yes file delete file} else {fs. unlinkSync (curPath) ;}}); fs. rmdirSync (url);} else {console. log ("the specified path does not exist, please provide the correct path") ;}}; deleteFolderRecursive (". /grunt ");

I hope this article will help you design nodejs programs.

For more articles about node. js's function examples for traversing the file production file list, refer to the PHP Chinese website!

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.