Reputation:
I've checked other tons of answers related to my issue and none worked. I suppose it's a syntax typo, but I cannot seem to find it.
My script should simply tell me if a process is running, then save the path into a variable, go to that path and delete the .exe
.
Unfortunately, I get ECHO is OFF
. Any ideas why?
@echo off
setlocal EnableDelayedExpansion
set /p PROGRAM=NAme of the exe:
tasklist /FI "IMAGENAME eq %PROGRAM%" 2>NUL | find /I /N "%PROGRAM%">NUL
if "%ERRORLEVEL%"=="0" echo. %PROGRAM% is running
for %%i in (%PROGRAM%) do (
echo %%~$PATH:i
set PROGRAM_PATH=%%~$PATH:i
)
cd %PROGRAM_PATH%
del /F %PROGRAM_PATH%
pause
Upvotes: 2
Views: 108
Reputation:
So, I realized that my program wasn't doing what I wanted because the .exe
didn't exist.
So, I modified a little bit the script, so that I will handle that case:
.exe
is running, exit because I won't be able to delete a already in use .exe
anyway;My final program:
@echo off
setlocal EnableDelayedExpansion
goto start_program
:start_program
set /p PROGRAM=Name of exe(ex:cmd.exe):
tasklist /FI "IMAGENAME eq %PROGRAM%" 2>NUL | find /I /N "%PROGRAM%">NUL
if "%ERRORLEVEL%"=="0" do (
goto enter_program
)
if "%ERRORLEVEL%"=="1" do (
goto exit_program
)
:enter_program
for %%i in (%PROGRAM%) do (
set PROGRAM_PATH=%%~$PATH:i
)
cd %PROGRAM_PATH%
del /f %PROGRAM_PATH%
goto:eof
:exit_program
goto:eof
Thanks for tips Ryan and jeb
Upvotes: 2