Home > Web Front-end > JS Tutorial > body text

Detailed example of node.js using fs.rename to force rename or move a folder

小云云
Release: 2018-05-19 16:28:47
Original
3353 people have browsed it

This article mainly introduces the method of using fs.rename to force rename or move folders in node.js. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

This article introduces the method of node.js using fs.rename to force rename or move a folder. First, it introduces the usage of rename, as follows:

[Rename Folder 】

// rename.js
var fs = require("fs"); 
 
// 重命名a文件夹为b 
fs.rename("c:a","C:b",function(err){ 
 if(err){ 
  console.log("重命名失败!"); 
 }else{ 
  console.log("重命名成功!"); 
 } 
});
Copy after login

[Note: The folder does not exist, then err will have an error message. ]

When we daily operate some files or folders on the computer, the Windows system may pop up a corresponding dialog box, prompting us that the file or folder cannot be renamed. This is because the files or files in the folder are occupied by some running processes on the system. We can find the process occupying the file, end it, and then rename the file or folder. However, this process is relatively cumbersome and difficult to implement through programming.

And when performing a large number of renaming operations in node.js, permission problems may occasionally occur. The solution is simple, use synchronous naming and add a try/catch.

 try {
  fs.renameSync(oldPath, newPath);
 }
 catch (e) {
  fs.renameSync(oldPath, newPath);
 }
Copy after login

It has been tested and effective under win10/node.6.15

Related recommendations:

Teach you how to copy and move folders in the Linux system

php moving folders and files

php moving folders and files program code_PHP tutorial

The above is the detailed content of Detailed example of node.js using fs.rename to force rename or move a folder. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!