How to remove a directory in Windows even though it is in use?

0

I have a database service running in a directory known as C:\meuBanco\exe\ and now I have developed an updater of this service, which performs the following steps:

  • For service
  • Deletes the exe folder: rd /s /q %DIRSERV%\exe\
  • Unzip the zip with the new service in the exe
  • The problem is in step 2, where you can not possibly delete the folder as it is in use. But when the update is executed, it can delete the folder because it should not have any process locking the exe folder.

    So I ask: Is there a way to force removal of a directory in Windows even though it is in use?

        
    asked by anonymous 11.04.2017 / 19:11

    1 answer

    3

    You can try these two options below. as this answer in SOEn:

      

    Be careful when executing these commands! Once executed, the process is irreversible and can cause damage to an application or operating system if it is run in windows folders.

    takeown /r /f [path da pasta]
    cacls [path da pasta] /c /G "ADMINNAME":F /T
    rmdir /s [path da pasta]
    

    Another alternative for long paths, which can cause errors with the above method:

    mkdir \empty
    robocopy /mir \empty [path da pasta]
    
      

    Note: The user running both commands needs to have elevated administrator privileges.

        
    11.04.2017 / 19:47