I have the following code which tries to delete a file on a windows 7 machine:
// check if this item has an uploaded image file
var imageFullPathName = __dirname + "/../public/images/" + req.params.itemId;
logger.log("imageFullPathName = " + imageFullPathName);
var normalizedPathName = path.normalize(imageFullPathName);
logger.log("normalizedPathName = " + normalizedPathName);
// delete the image if it exists
fs.exists(normalizedPathName, function(exists) {
console.log("Found the file: " + normalizedPathName);
normalizedPathName = normalizedPathName.replace(/\\/g,"\\\\");
console.log("New path name = " + normalizedPathName);
fs.unlink(normalizedPathName, function(err){
if (err){
console.error("Error in call to fs.unlink");
}
});
});
I get the following output:
imageFullPathName = C:\IronKey\hes\cscie-71\project\medical-interchange\routes/../public/images/5658e5612103cb2c41000006
normalizedPathName = C:\IronKey\hes\cscie-71\project\medical-interchange\public\images\5658e5612103cb2c41000006
New path name = C:\\IronKey\\hes\\cscie-71\\project\\medical-interchange\\public\\images\\5658e5612103cb2c41000006
Error in call to fs.unlink
The file in question does indeed exist when I look for it using a DOS shell. It also doesn't matter what I do to the new path name, I can never delete the file. If I leave the path with single back slashes by commenting out the call to normalizedPathName.replace(), it still fails. However, if I manually create a string like this it works:
fs.unlink("c:\\IronKey\\hes\\cscie-71\\project\\medical-interchange\\public\\images\\5658e5612103cb2c41000006", function(err){ ...
What do I have to do to delete this file? I'm totally stumped.
I modified the code as recommended by josh3736 as follows but I get a ENOENT error. The file clearly does exist because I opened it in my editor using the EXACT full path name reported in the error message:
// check if this item has an uploaded image file
var imageFullPathName = path.join(__dirname, "../public/images",
sanitize(req.params.itemId));
logger.log("imageFullPathName = " + imageFullPathName);
fs.unlink(imageFullPathName, function(err){
//if (err.code != 'ENOENT'){
if (err){
logger.log("Error in call to fs.unlink", err);
}
else{
logger.log("No file found");
}
logger.log("Delete Success");
});
And the error message follows: Error in call to fs.unlink { [Error: ENOENT: no such file or directory, unlink 'C:\\IronKey\\hes\\cscie-71\\project\\medical-interchange\\public\\images\\5658fd27fca1b3bc3d00000e']
fs.exists()with one version ofnormalizedPathNameand then you try to change it before deleting? Also,fs.exists()is an anti-pattern. Just attempt the delete and deal with an error if it doesn't exist.fs.unlink()on the desired path with nofs.exists()call. If it doesn't work, then look at exactly what error is returned and diagnose using that.fs.exists()is deprecated because of concurrency issues. In a multi-tasking OS, the file can be removed between the call tofs.exists()and then call tofs.unlink()by some other process. And, you can just callfs.unlink()and handle an error if the file did not exist. There is no reason for thefs.exists()call.