Search code examples
windowspowershellshared-directory

How should I mark a folder as processed in a script?


A script shall process files in a folder on a Windows machine and mark it as done once it is finished in order to not pick it up in the next round of processing.

My tendency is to let the script rename the folder to a different name, like adding "_done".

But on Windows, renaming a folder is not possible if some process has the folder or a file within it open. In this setup, there is a minor chance that some user may have the folder open.

Alternatively I could just write a stamp-file into that folder.

Are there better alternatives? Is there a way to force the renaming anyway, in particular when it is on a shared drive or some NAS drive?


Solution

  • You have several options:

    1. Put a token file of some sort in each processed folder and skip the folders that contain said file
    2. Keep track of the last folder processed and only process ones newer (Either by time stamp or (since they're numbered sequentially), by sequence number)
    3. Rename the folder

    Since you've already stated that other users may already have the folder/files open, we can rule out #3.

    In this situation, I'm in favor of option #1 even though you'll end up with extra files, if someone needs to try and figure out which folders have already been processed, they have a quick, easy method of discerning that with the naked eye, rather than trying to find a counter somewhere in a different file. It's also a bit less code to write, so less pieces to break.

    Option #2 is good in this situation as well (I've used both depending on the circumstances), but I tend to favor it for things that a human wouldn't really need to care about or need to look for very often.