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("重命名成功!"); } });
[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); }
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 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!