I need to create a bat that recognizes if a process is running, and if the process is running I need to add a prompt asking if the user wants to end the process to continue with some other tasks.
I have the command to recognize if a process is running(this is the one that worked for me):
#echo off
tasklist /nh /fi "imagename eq vmware-view.exe" | find /i "vmware-view.exe" >nul && (
echo VMWare Horizon is running
) || (
echo VMWare Horizon is not running
)
pause>nul
and I have the command to prompt for an imput:
#echo off
setlocal
:PROMPT
SET /P AREYOUSURE=Are you sure (Y/[N])?
IF /I "%AREYOUSURE%" NEQ "Y" GOTO END
But I don't know how to put those 2 together to say that, if the process is running, display prompt asking user if bat can end process to continue with an update, and if not running just go on with update.
could you please assist?
thanks in advance
I remember from the old DOS days that you could use GOTOs.
for instance a bat file containing the following:
#ECHO OFF
ECHO something
GOTO LABEL0
ECHO something else
GOTO LABEL1
:LABEL0
ECHO some label
:LABEL1
ECHO some other label
will print:
something
some label
some othe label
you will not see
something else
because the first GOTO skipped to LABEL0
You can use a label like :QUIT or :END at the end of your bat and skip at the end if you have nothing to do.
Use ERRORLEVEL to see if TASKLIST and FIND got the process. Afterwards use CHOICE.
#echo off
set task=vmware-view.exe
tasklist | find /i "%task%" > nul
if %errorlevel%==0 (
echo task is running.
choice /c yn /t 10 /d n /m "should we kill it?"
if errorlevel 2 goto end
taskkill /f /im "%task%"
) else (
echo task is not running.
)
:update
echo updating...
:end
Related
Windows 10 I can't seem to get the batch file to activate the second batch file if the process is not running. First part works fine, calling the second batch file is broken. I tried to call, I tried %~dp0, like this: %~dp0MyProgram.bat
#echo off
:start
cls
set proc=zm.exe
set runme=c:\ZM\zcash.bat
set time=30
tasklist /FI "IMAGENAME eq %proc%" 2>NUL | find /I /N "%proc%">NUL
if "%ERRORLEVEL%"=="0" (
echo Everything is OK
timeout /t %time%
goto :start ) || (
echo ERROR! restarting service
taskkill /IM "%proc%" /F
else (start "" "%runme%")
)
timeout /t %time%
goto :start
Despite saying you'd tried it, you should use Call because you're wanting to return to the same script upon it's completion.
Perhaps something like this would work for you?
#Echo off
Set "proc=zm.exe"
Set "runme=C:\ZM\zcash.bat"
Set "secs=30"
:Loop
ClS
TaskList | FindStr /I "\<%proc%\>" >Nul && (Echo Everything is OK) || (
Echo ERROR! starting service
Call "%runme%")
Timeout %secs% /NoBreak >Nul
GoTo Loop
Note: I changed the variable name to %secs% because %TIME% is already a defined system variable.
I have a batch file which makes a .vbs file and is then supposed to open it. They are both in the same folder. I've tried using start, but it simply doesn't work for some reason. I also tried cscript, but since the script is supposed to be hidden, this doesn't work either. If I try to run the vbs file by hand, it works.
code:
#echo off
tasklist | find "wscript.exe" >nul: && goto runningalr
:reset
type nul > sound.vbs
(echo Set Sound = CreateObject("WMPlayer.OCX.7"^))>>sound.vbs
:begin
set /p loop=Do you want the music to repeat? (y/n) ...
if /i %loop%==y goto repeat
if /i %loop%==n goto begin1
goto begin
:repeat
(echo do until false)>>sound.vbs
goto begin1
:begin1
set /p damd=The music's directory? (type "exit" if no more) ...
if /i %damd%==exit goto start
( echo Sound.URL = "%damd%"
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000)>>sound.vbs
goto begin1
:runningalr
set /p kill=Music already playing. Do you want to stop it? (y/n) ...
if %kill%==y taskkill /f /im wscript.exe && exit
if %kill%==n goto gonnaexit
:gonnaexit
<nul set /p "=Press any key to exit ... "
pause >nul
exit
:start
if /i %loop%==y (echo loop)>>sound.vbs
start C:\Users\User\Desktop\server\sound.vbs
Why didn't start work for me and what works?
instead of having just "find wscript" try using "find %WINDIR%/System32/wscript.exe /I"
You might also need to run the script as an administrator, however this is unlikely.
you give a dedicated path with start, but you write the vbs script to the current working directory. The following line starts from that working directory:
start "" "%~dp0sound.vbs"
I have created the below code to execute multiple commands depending on the user input but the input is case-sensitive.
I have tried the /I switch with the if statement but that does not work either.
Below is the code:
#Echo off
SET /P uname=Launch Outlook in safe mode:
IF "%uname%"==Yes GOTO Error
IF "%uname%"==No GOTO start
:Error
taskkill /IM Outlook.exe /f
cd c:\Program Files (x86)\Microsoft Office\root\Office16
start Outlook.exe /safe
#echo The Script is running please wait && timeout /t 20
taskkill /IM Outlook.exe /f
cd %Homepath%
cd Appdata\Local\Temp
echo.>myfile.txt && #echo Thanks for using the script,You may close this window.and continue. > myfile.txt
start outlook.exe
timeout /t 8
start notepad myfile.txt
:End
:start
echo "No valid options"
echo "This window would close in 10 seconds" && timeout /t 10
:End
I would use the choice command instead
echo Y] Yes
echo N] No
choice /c yn /n
if %errorlevel%==1 goto yes
if %errorlevel%==2 goto yes
I have bitlord I want this program to run when i click a batch file and when i don't want this program to run i want to kill it with same batch file. Can anyone please write the script Thanks a lot for giving your time.
I tried it scripting but failed.
If bitlord is not running then this will launch it
If bitlord is running then this will kill it
Change line 2 and line 3 to set the program name and path.
#echo off
set "program=bitlord.exe"
set "folder=c:\program files\bitlord"
tasklist |find /i "%program%" >nul || start "" "%folder%\%program%" & goto :EOF
tasklist |find /i "%program%" >nul && taskkill /f /im "%program%" & goto :EOF
Okay. I think i understand your question now:
#echo off
taskkill.exe /f /im notepad.exe
start calc.exe
The magic is to use the start command which starts a new command asynchronous. Meaning, it doesn't wait for the termination.
i have been trying to write a batch script in windows 7 sp1. After i start my batch script i try to type an option into the input and then i get "goto unexpected at this time" then the command prompt closes. Please help i need this for a personal project.
#echo off
:start
echo Is this the first time you ran this program on this computer
set input=
set /p intput= "y/n"
if%input%==yes goto Yes
if%input%==no goto No
:No
pause
echo Okay skipping the installation process
cd Minecraft_Server
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto serverrestart
:serverrestart
echo Would you like to restart the server?
set input=
set /p intput= y or n
if%input%==y
pause
goto restart
if%input%==n
pause
goto norestart
:restart
echo To restart the server press any key.
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto serverrestart
:norestart
exit
:Yes
pause
echo I will now install the Minecraft Serevr files, please make sure you
echo have me in the "Parent Directory" of the Minecraft_Server folder
echo --If you don't know what a parent directory is GOOGLE it!--
pause
mkdir Minecraft_Server
move files.exe Minecraft_Server
cd Minecraft_Server
start files.exe
timeout /t 3
java -Xmx1024M -Xms1024M -jar minecraft_server*.jar
goto :serverrestart
You misspelled input in the set /p line and you should use quotes. I used /i to make the compare case insensitive and shortened to one key required. Do as you wish with that. Try this:
#echo off
:start
echo Is this the first time you ran this program on this computer
set "input="
set /p input= "y/n"
if /i "%input%"=="y" goto :Yes
if /i "%input%"=="n" goto :No
echo.Enter a valid choice
pause
goto :start