I am trying to close an exe process located in a specific directory, using the %appdata%
variable, but it doesn't work.
WMIC Process Where "ExecutablePath='%APPDATA%\\Adobe\\screenrecorder.exe'" Call Terminate
If I try to close the process without %appdata%
it works as intended.
WMIC Process Where "ExecutablePath='C:\\Users\\Admin\\AppData\\Roaming\\Adobe\\screenrecorder.exe'" Call Terminate
It is essential that it must work using %appdata%
, does someone know how to close an exe file using %appdata%?
CodePudding user response:
You should have noted that backward slashes in a path require escaping in the WHERE clause of WMIC, so you simply need to expand the environment variable, and substitute the backward slashes for escaped backward slashes. The general method of doing that is %VariableName:CharToSubstitute=SustitutionChars%
WMIC Process Where "ExecutablePath='%AppData:\=\\%\\Adobe\\screenrecorder.exe'" Call Terminate
Or more robustly
%SystemRoot%\System32\wbem\WMIC.exe Process Where "ExecutablePath='%AppData:\=\\%\\Adobe\\screenrecorder.exe'" Call Terminate
CodePudding user response:
With a Batch file ,you can try like this way :
@echo off
Title Kill Application using WMIC
Set Application=%AppData%\Adobe\screenrecorder.exe
Call :Add_backSlash %Application%
echo %Application%
pause
WMIC Process Where "ExecutablePath='%Application%'" Call Terminate
pause
EXIT
::---------------------------------------------------------------------
:Add_backSlash <String>
Rem Subroutine to replace the simple "\" by a double "\\" into a String
Set "Application=%1"
Set "String=\"
Set "NewString=\\"
Call Set "Application=%%Application:%String%=%NewString%%%"
Exit /b
::---------------------------------------------------------------------