none
Can I determine if a folder is in use with PowerShell? RRS feed

  • Question

  • I have a PS script that will filter out specific folders at a set folder depth and move them to an archive area  (which appears to be working fine). I am using robocopy for the move as we need to maintain the folder attributes like [Create Date] on the folder after the move.

    Where I am having an issue is if a subfolder or file of the folder to be moved is in use, it ends up skipping those files. I have done a bit of reading and I am not finding a clear way to check the folder to see if its in use, if so then I would like to log that and skip that folder, moving on to the next folder at that same level.

    Can someone confirm if there is a way in this scenario how I can accomplish this, thanks.


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Tuesday, August 23, 2016 4:56 PM

Answers

  • There is no PS switch. You can use the open handles to search for open files.  "Folders" are never open/closed.

    Post in OS forum for your system to get instructions on how t modify a system to enable reporting of open files.

    Note that a text file will never appear to be open even if it is in use.  Log files are the same in most cases.

    In the newest versions of Windows the "openfiles" program is available.


    \_(ツ)_/


    • Edited by jrv Tuesday, August 23, 2016 6:49 PM
    • Marked as answer by Stunpals Tuesday, August 23, 2016 7:32 PM
    Tuesday, August 23, 2016 6:48 PM

All replies

  • The RoboCopy log will have all of that information.  You can also have RC wait on the file.

    Since you have a Windows utilities question please post in the Windows forum for your version of Windows.  This is not a RoboCopy support forum.


    \_(ツ)_/

    Tuesday, August 23, 2016 5:14 PM
  • Thanks jrv for the response.

    I posted it here as I assumed it would be a PS switch or function that I would need to use to confirm if the folder was in use and since other than 1 line it is all PS commands.

    As for the RC wait, I don't think that will do what we want. I don't want the PS script to stop completely waiting for a file to be closed (if I am understanding the RC switch). Staff with a file open could be left in that state overnight or even a couple of days (even though we tell staff to close out when done at the end of the day).

    We want to run this PS "Archive" script monthly and want it to complete that night. If there were folders in use then skip and log and we'll deal with those instances the next day as needed. 


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Tuesday, August 23, 2016 5:32 PM
  • As I posted.  Use the log to see which files were skipped.

    This is not a scripting issue.


    \_(ツ)_/

    Tuesday, August 23, 2016 6:23 PM
  • Sorry, maybe I am not stating the issue clearly. I am NOT looking for which files are being skipped. 

    The goal is to move all specified folders (filtered using RegEx and Create Date) but if its in use, not move it (or its subfolders/files), because the folder and its contents must stay intact.

    I want to move the entire folder so I need to check if the folder is in use, which I assumed this could be done via a PS switch? Maybe I was wrong to assume PS could determine this before I take any action. If that's the case I will need to abandon PS and find a different way. 

    I originally was using the Move-Item but was unable to maintain the [Create Date] so I changed the PS script to use RoboCopy. My focus of this question as I stated is (everything else is irrelevant) how to use my PS script to determine if a folder is in use. Forget about the move, delete, etc.


    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Tuesday, August 23, 2016 6:42 PM
  • There is no PS switch. You can use the open handles to search for open files.  "Folders" are never open/closed.

    Post in OS forum for your system to get instructions on how t modify a system to enable reporting of open files.

    Note that a text file will never appear to be open even if it is in use.  Log files are the same in most cases.

    In the newest versions of Windows the "openfiles" program is available.


    \_(ツ)_/


    • Edited by jrv Tuesday, August 23, 2016 6:49 PM
    • Marked as answer by Stunpals Tuesday, August 23, 2016 7:32 PM
    Tuesday, August 23, 2016 6:48 PM
  • Thanks, sorry for the confusion.

    Stunpals - Disclaimer: This posting is provided "AS IS" with no warranties.

    Tuesday, August 23, 2016 7:32 PM