Link to home
Start Free TrialLog in
Avatar of geewizzz
geewizzz

asked on

Is there a way to delete a locked file without using a 3rd party app, a script perhaps?

I have done extensive research on this and cant seem to find a way to delete a locked file without using a 3rd party app. I know I can rename it, then on reboot delete but this is not enough. I have also tried the trick of closing 'explorer.exe'- delete the file via cmd prompt, then relaunching 'explore.exe'---- no dice..

tx
Avatar of Shift-3
Shift-3
Flag of United States of America image

The trick is to find out which process has the file locked.  You can find this using the Microsoft utility Process Explorer.

Then either get it to release the file gracefully or kill the process.
Avatar of geewizzz
geewizzz

ASKER

I dont want to rely on a third party app. Issue is some files run under the 'svrhost' process and it is not so easy to pinpoint which 'svrhosts' to kill. Some even relaunch themselves as soon as you kill them.


Process Explorer was developed by Sysinternals, but it's under the Microsoft umbrella now.  If you're running Windows then Microsoft isn't third-party.

You can see which processes are running under which instance of svchost by using tasklist with the /svc switch.
To clarify, I know I can locate a process, kill it and delete a file. (tasklist and taskkill..)

So specifically for a specific example now. Oracle locks the file oci.dll, it runs under a svchost service- I can pinpoint the one using a number of different tools. Problem is, by the time you kill the process and delete the file it is already relaunched, coming up under a different PID.
When I use the tool 'Unlocker' it tell me the process and gives me the option to kill it quickly and delete the file successfully. If a program like that can do it, then why not a script?
Another issue is I want to be able to delete it on a remote computer as well, unfortunately the PID will be different on each machine.
To understand it further, what are these tools tapping into to know which process has a file locked?

Avatar of sirbounty
I would suspect there's some details being pulled from wmi that could determine this, though I have no way of testing it.

However, when you say the process 'restarts',I'd imagine that is coming from the service definition.

Start->Run->services.msc <Enter>
Locate the service, double-click it and under Recovery tab, change all actions to "Take No Action" and then click OK.
Ahhh
As an example I am 'playing' with an uninstall script for Oracle. Cannot delete the oci.dll file (its locked) and when I trackdown the process id, it's a 'service host' one not listed on the services console.
But that's a dll - some service would be loading the overlay, no?
Some research online pointed to the possibility of the 'Distributed Transaction Coordinator' service. However even after I stop the service, set it to 'manual' startup and reboot, still no go.. No other oracle/database services are running.
Is there a way to disable windows file proctection on a single file?
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry for not checking back up on this one sooner.. I will test it on monday. Thanks again for your help.
Have a great weekend!
Thank you again for your help. Will definitely copy you on future questions if you are interested.

Glad I could help - thanx for the grade! :^)