Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to delete a folder by nodejs

2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to delete folders in nodejs". In daily operation, I believe many people have doubts about how to delete folders in nodejs. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to delete folders in nodejs". Next, please follow the editor to study!

Nodejs delete folder methods: 1, to create a js sample file; 2, the introduction of fs module; 3, through the "function deleteall (path) {...}" method to delete folders.

This article operating environment: Windows7 system, nodejs10.16.2 version, DELL G3 computer

How to delete a folder from nodejs?

Node.js deletes folders and files:

Node.js 's fs module only provides the function of deleting file unlink folder and directory rmdir, so we need to delete it through traversal. The code is as follows

Var fs = require ('fs'); / / introduce fs module function deleteall (path) {var files = []; if (fs.existsSync (path)) {files = fs.readdirSync (path); files.forEach (function (file, index) {var curPath = path + "/" + file;if (fs.statSync (curPath). IsDirectory ()) {/ / recursedeleteall (curPath);} else {/ / delete filefs.unlinkSync (curPath);}); fs.rmdirSync (path);}

Use

Deleteall (". / dir") / / just pass in the folder

Update:

Haha, I found a more convenient code to use the ability of nodejs to call system commands and use system commands to delete. I used commands in npm run xxx to delete, but due to compatibility problems with different commands in the system, I have to come to nodejs to judge the system.

Var exec = require ('child_process'). Exec;var cmdStr =' rm-rf xxhbg_app_src/webapp';exec (cmdStr, function (err, stdout, srderr) {if (err) {console.log (srderr);} else {console.log (stdout);}}); at this point, the study on "how nodejs deletes folders" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report