简体   繁体   English

fs.rename ENOENT:没有这样的文件或目录

[英]fs.rename ENOENT: no such file or directory

I am getting in trouble then using fs.rename in node.js app. 我在使用node.js应用程序中的fs.rename时遇到了麻烦。 I already use the function below and it works as I would expect it to work. 我已经使用了下面的函数,并且可以正常运行。

 var fs =require("fs"); var path =require("path") module.exports= function(oldPath, newPath){ oldPath=path.join(__dirname, "..", "documents", "bka" , oldPath); newPath=path.join(__dirname, "..", "documents", "bka" , newPath); fs.rename(oldPath, newPath, (err)=>{if (err) console.log(err)}); } 

Then I tried to use the function for another case. 然后我尝试将函数用于另一种情况。 oldPath exists. oldPath存在。 newPath doesn't exists. newPath不存在。 If I don't change newPath no error occurs. 如果我不更改newPath,则不会发生错误。 If I change it the error below occurs and I have no idea why: 如果我更改它,则会发生以下错误,并且我不知道为什么:

 { Error: ENOENT: no such file or directory, rename '/home/ubuntu/workspace/documents/bka/7_Wall Street_1/9_Whg_Nr_22/7_bob' -> '/home/ubuntu/workspace/documents/bka/7_Wall Street_1/9_Whg_Nr_221/7_bob' at Error (native) errno: -2, code: 'ENOENT', syscall: 'rename', path: '/home/ubuntu/workspace/documents/bka/7_Wall Street_1/9_Whg_Nr_22/7_bob', dest: '/home/ubuntu/workspace/documents/bka/7_Wall Street_1/9_Whg_Nr_221/7_bob' } 

would be great if you could help me. 如果您能帮助我,那会很好。 I have seen some other people had similar problems in the past but I didn't find any answer that could make me understand the problem. 我过去曾见过其他一些人也遇到过类似的问题,但没有找到任何可以使我理解该问题的答案。

Thanks amit 谢谢阿米特

You're renaming the file .../9_Whg_Nr_22/7_bob to .../9_Whg_Nr_221/7_bob 您正在将文件.../9_Whg_Nr_22/7_bob重命名为.../9_Whg_Nr_221/7_bob

This will only work if the directory 9_Whg_Nr_221 already exists, and my guess is that it doesn't, and fs.rename will not create that directory for you. 仅当目录9_Whg_Nr_221已经存在时, 9_Whg_Nr_221 ,而我猜测是不存在,并且fs.rename 不会为您创建该目录。

In these sorts of situations, where the target directory may not yet exist, you have to manually create the directory first, for instance using mkdirp . 在这种情况下,目标目录可能还不存在,您必须首先手动创建目录,例如使用mkdirp

thanks for the response. 感谢您的回应。

I kind of fixed it. 我有点修好了。 The code is a mess now but it at least it does what it should. 现在的代码是一团糟,但至少它能完成应有的工作。

 var path =require("path"); var mkdirp =require("mkdirp"); var shell = require("shelljs"); var Promise= require("bluebird"); module.exports= function(oldPath, newPath){ oldPath=path.join(__dirname, "..", "documents", "bka" , oldPath); newPath=path.join(__dirname, "..", "documents", "bka" , newPath); var oldArr= oldPath.split("/"); var newArr= newPath.split("/"); var np=""; var op=""; var createDir=(path)=>{ return new Promise((resolve)=>{ mkdirp(path, (err)=>{ if (err) throw err resolve(path) }); }); } var copy =(from, to)=>{ return new Promise((resolve)=>{ shell.cp("-R",from+"/*", to); resolve(); }); } var rm= (path)=>{ return new Promise((resolve)=>{ shell.rm("-R", path); resolve(); }); } var results=[] for( var i=0; i<oldArr.length; i++){ op += oldArr[i]+"/"; np += newArr[i]+"/"; if(oldArr[i]!= newArr[i]){ Promise.resolve() var result={} result.from=op; result.to=np; results.push(result); } } createDir(results[results.length-1].to) .then(()=>{ copy(results[results.length-1].from, results[results.length-1].to) .then(()=>{rm(results[0].from).then(()=>{ results.pop(); }); }); }); } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM