0

I am trying to delete a folder with all the subfolder and contains. But there are jar files used by another process. So it fails. I tried:-

Remove-Item -LiteralPath "C:\tools\wlp-8.5.5.9\usr\servers\dev\workarea" -Force -Recurse

But it fails:-

Remove-Item : Cannot remove item C:\tools\wlp-8.5.5.9\usr\servers\dev\workarea\org.eclipse.osgi\142\0.cp\lib\cxf-core-3.0.3.jar: The process cannot access the file 'cxf-core-3.0.3.jar' because it is being used by another process.

How can I forcefully delete this folder by PowerShell script?

mklement0
  • 382,024
  • 64
  • 607
  • 775
masiboo
  • 4,537
  • 9
  • 75
  • 136
  • Your only option is to identify the specific processes that are using the `.jar` files and _kill_ (forcefully terminate) them with, e.g., `Stop-Process`. – mklement0 Aug 24 '19 at 22:29
  • I'm not sure which process to kill. – masiboo Aug 24 '19 at 23:36
  • Well, `java` processes would be the obvious candidates, but if that is too broad, more work is needed - see https://stackoverflow.com/q/958123/45375 – mklement0 Aug 25 '19 at 00:00

1 Answers1

0

Directly getting the files which are opened by java then kill the required process :

(Get-WmiObject Win32_Process -Filter "name = 'Java.exe'") | Select-Object handle, CommandLine

then stop it:

Stop-Process -Id XXXXX -force -whatif 
# remove -whatif , if you want to execute the command
  • handle CommandLine ------ ----------- 10368 "C:\tools\java\jre\bin\java" "-javaagent:C:\tools\wlp-8.5.5.9\bin\tools\ws-javaagent.jar" -Djava.awt.headl... So how do I kill this process by force? – masiboo Aug 25 '19 at 09:55
  • Stop-Process -Id handle -force -whatif Got errro:- Stop-Process : Cannot bind parameter 'Id'. Cannot convert value "handle" to type "System.Int32". Error: "Input string was not in a correct format.". I have to convert the handle . Later tried Stop-Process -Id 10368 -force -whatif. Output:- What if: Performing the operation "Stop-Process" on target "java (10368)". But after that I tried again (Get-WmiObject Win32_Process -Filter "name = 'Java.exe'") | Select-Object handle, CommandLine. It was showing the same output of the Get-WmiObject. Meaing it was not stop – masiboo Aug 25 '19 at 11:09
  • Handle= your process id ~ 10368 – Mahmood Shehab Aug 25 '19 at 11:11
  • To make a script, I have to convert Handle to int. How to do it? Also even if I tried Stop-Process -Id 10368 -force -whatif . It doesn't stop the process. Coz I get same output when tried (Get-WmiObject Win32_Process -Filter "name = 'Java.exe'") | Select-Object handle, CommandLine handle CommandLine ------ ----------- 10368 "C:\tools\java\jre\bin\java" "-javaagent:C:\tools\wlp-8.5.5.9\bin\tools\ws-javaagent.jar" -Djava.awt.headl... – masiboo Aug 25 '19 at 11:14