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

Asked

Viewed 11,399 times

1

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

  1. For service
  2. Excludes the folder exe: rd /s /q %DIRSERV%\exe\
  3. Unzip the zip with the new service in the folder exe

The problem is in step 2, where eventually it is not possible to delete the folder as it is in use. But when the update runs, it can delete the folder as it should not have any process blocking the folder exe.

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

  • I believe you need to elevate privileges to the level of the system, after all, this feature is for security, for no one to erase something that is in use and compromise another application that is making this use.

  • @diegofm I already run the update as administrator. There is another level possible?

  • Yes, there are other system levels above the administrator, so much so that there are folders and files that you, even as admin, can’t access in windows

  • 1

    Try this: http://stackoverflow.com/a/98069/5524514

  • 1

    Terminating executables or files that are open in the directory does not resolve? to end with the takkill example: taskkill /f /im notepad.exe then delete the directory...

  • @diegofm The workaround with Robocopy worked!! Awesome. If you want to answer..

  • @Laérciolopes I don’t know what the executables are (they might be external applications), so I would have to first figure this out.

Show 2 more comments

1 answer

3


You can test these two options below. as per this answer in the Soen:

Be careful when executing these commands! After running, the process is irreversible and may cause damage to an application or operating system if it runs 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 to 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 high administrator privileges.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.