Search code examples
javapythonfilerenamemove

How to Detect File Rename / Move in Windows


I'm trying to detect when a file is being moved or renamed in windows and I want to then use that change to update a database.

When I say file move: I mean moving from one directory to another from ".../A/foo.txt" to ".../B/foo.txt". When I say file rename: I mean renaming but staying in the same directory ".../A/foo.txt" to ".../A/bar.txt"

I know that linux and most people treat them as the same thing, and for my purposes they are the same thing. I just want to know the actual file path after and be able to match it to the original file path even in circumstances where there is a batch move.

I am using python for the parent program, but I am willing to use any coding language though it preferably is Java/Python/some form of C.


Solution

  • If you use java 7, you can simply use WatchService and WatchKey. This is a observer to watch a directory and each time something is changed, created or deleted you can do an action/file handling.