Home > Enterprise >  How to check if folder/file is deleted at runtime? nodejs
How to check if folder/file is deleted at runtime? nodejs

Time:12-08

At the beginning of a program, I can easily check if a folder exists or not and handle it.

But when it comes to runtime, thats a tricker question. If a folder is deleted, thats when a ton of data never gets to where it's supposed to go.

How do I check if a folder has been deleted at runtime?

How do I remake the folder if it's deleted?

(This is an nodejs question)

CodePudding user response:

Well, this problem can be easily solved with some npm modules.

You need a npm module to track file deletions etc

https://github.com/paulmillr/chokidar is an great resource.

According to the npm module:

// Initialize watcher.
const watcher = chokidar.watch('file, dir, glob, or array', {
  ignored: /(^|[\/\\])\../, // ignore dotfiles
  persistent: true
});

Then:

watcher
    .on('unlinkDir', path => {
        if (path == "/target/path/to/check/" && makeSureThisIfThisIsAnDirectory){
            //do something to fix this before files never reach their destination
        }
    })

  • Related