1

I dont know why ,but i am not able to delete file using unlink() and unlinksynk().

I have stored file using multiparty.Form() method. It will generate file with random name and returns filePath and originalFileName. And after this i have renamed file with originalFileName.

Code:

      await form.parse(fileData, (err: Error, fields: any, res: any) => {
        if (err) {
          callback(err, null);
        } else {
          let file_path = res.file[0].path;
          let assignedFilePath = file_path.substr(res.file[0].path.lastIndexOf('\/') + 1);
          let originalFilePath = res.file[0].originalFilename;
          originalFilePath = path.dirname(assignedFilePath) + '\\' + originalFilePath;
          fs.rename(assignedFilePath, originalFilePath, function (err: any) {
            if (err)
              callback(err, null);
            else
              callback(err, JSON.stringify({'path': originalFilePath}));
          });
        }
      }); 

Code for deleteFile()

 deleteTempFiles(tempFolderPath: string) {
    return new Promise(function (resolve: any, reject: any) {
      fs.readdir(tempFolderPath, (err: any, files: any) => {
        if (err) {
          reject();
        } else {
          for (let file of files) {
            fs.unlink(tempFolderPath + file, (err: any) => {
              if (err) {
                reject();
              }
            });
          }
          resolve();
        }
      });
    }).catch(function (e) {
      Promise.reject(e.message);
    })
  }

EDIT :

ERROR is, while deleting file ,compiler says Error: EBUSY: resource busy or locked, unlink.

And I think i am not using the file anywhere in program ?

Thanks in advance .

vivek
  • 87
  • 10
  • Your code example doesn't contain a delete call - did you forget to add it? An unrelated advice, use `path.join(...parts)` to construct paths, and use `path.separator` instead of the `'\\'` here. – Zlatko Mar 09 '20 at 11:34
  • hi Zlatko ,of-course i have added delete call but in other function. This is my generic method to add any type of file on server. but i think the root cause is getting from this code only ;) – vivek Mar 09 '20 at 11:49
  • for delete purpose i have used FileSystem (fs) to perform operations on file. ```unlink(filePath)``` for delete file. and thanks for advice, i will do it !! – vivek Mar 09 '20 at 11:51

1 Answers1

1

==== 2nd answer ===

Your deleteTempFiles function is utterly wrong read this removing of each file in directory (and directories) inside a dir

You have many logical errors in the code.

  1. You use a synchronius for loop, to queue tasks of removing list of files (And directories) from given directory.
  2. You unlinks all - even directories . and .. - not possible
  3. You ALWAYS resolves after that loop, and rejects after 1st error - but usually that reject should be much after resolve (loop is fast, it only queues io access, not executing the unlinks yet, then resolve is executed).

==== 1st answer ===

You have code for renameFile, which moves file form old name(and location) into new one. Therefore deleting old file would not work at all so you should delete new file.

Seti
  • 2,169
  • 16
  • 26
  • hi Seti, it seems like, i am storing new file in other location, but actually i am changing it in same folder .only file name's are changed from both input path's. – vivek Mar 09 '20 at 13:21
  • then you need to remove the ,,new'' file – Seti Mar 09 '20 at 13:25
  • yes i am doing that but getting one error , i have updated question just now :) – vivek Mar 09 '20 at 13:36
  • add the code for removal +execution of that code to the question – Seti Mar 09 '20 at 13:48
  • 1
    Resource busy etc, means that some process can still have the file opened (for example node itself). – Seti Mar 09 '20 at 15:32
  • Also i see some errors in your code: for example your removable code : 1. read all files (also file .. and .) in directory, Also read all directories. 2. in loop - set all of them to be unlinked. 3. when loop finishes - `resolve` how you even get the reject i dont know... – Seti Mar 09 '20 at 15:34
  • yep, i have updated my code as you mentioned and also i found location, where exactly my file is locked(used) using debugger . Issue is solved now . Thank you for help :) – vivek Mar 10 '20 at 05:25
  • Glad i could help! - can you mark the question as answered (you can answer on it yourself with the answer) – Seti Mar 10 '20 at 07:53