batch result me "echo off" instead the proper result - batch-file

I have a little problem with the next code
#Echo off
Title STARTING
cls
echo.
echo Checking running services...
echo.
timeout /t 2 /nobreak >NUL
tasklist /fi "imagename eq cmd.exe" /v | find /I /N "DATABASESERVER" >NUL
if "%ERRORLEVEL%"=="1" (
cls
echo.
echo Database is not running, now will start!
echo.
start database.bat
echo Database is running!
echo.
timeout /t 4 /nobreak >NUL
)
tasklist /fi "imagename eq cmd.exe" /v | find /i /n "ARMASERVER" >NUL
if "%errorlevel%"=="1" (
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
set /A "ora=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
echo %ora%
pause
cls
echo.
echo Server is not running, now will start!
echo.
start arma.bat
title EPOCHSERVER
timeout /t 39 /nobreak >NUL
)
The problem is that "echo %ora%" it give me the result "Echo off" instead the value of time in seconds
What is the problem?
Thank you in advance!

You need to enable delayed expansion:
tasklist /fi "imagename eq cmd.exe" /v | find /i /n "ARMASERVER" >NUL
if "%errorlevel%"=="1" (
for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (
set /A "ora=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
echo percent inside ^(^)=%ora%
setlocal enabledelayedexpansion
echo exclamation=!ora!
endlocal
)
echo percent outside ^(^)=%ora%
Output:
==>D:\bat\SO\30834591.bat
percent inside ()=
exclamation=214860
percent outside ()=214860
==>

%ora% is empty at that point, which means that the command being executed is just echo, which returns the state of echo.
Are you sure that the quotation marks on the set line should be there?
Someone with better batch skills will speak up; I don't want to risk misleading you with my half-educated guesses.
In the meantime, I recommend that you play with that line directly on the commandline until you get the results you expect. Remember to de-double the % characters outside of the batch script.

Related

Why does the following batch code may exit?

I am using this batch to automatically restart some program, they are running very well, but sometimes the batch exit, which causes process_1 and process_2 also exit. It seems that everything is normal, but the batch itself crashes. Why ?
#echo off
cd %~dp0
set process_1=process_1.exe
set process_2=process_2.exe
set interval=10
:check_service
tasklist > task_tmp.txt
findstr %process1% task_tmp.txt> NUL
if ErrorLevel 1 (
timeout /t 1 /nobreak > NUL
goto start_1
)
findstr %process2% task_tmp.txt> NUL
if ErrorLevel 1 (
timeout /t 1 /nobreak > NUL
goto start_2
)
timeout /t %interval% /nobreak > NUL
goto check_service
:start_1
start /b "" %process_1%
echo %date% %time% %process_1% " down, up it" >> start_note.txt
goto check_service
:start_2
start /b "" %process_2%
echo %date% %time% %process_2% " down, up it" >> start_note.txt
goto check_service
You can maybe simplify like this:
#echo off
Set "MyProcess1=process_1.exe"
Set "MyProcess2=process_2.exe"
:check
%SystemRoot%\System32\tasklist.exe /NH | %SystemRoot%\System32\find.exe /i "%MyProcess1%">nul || echo starting %MyProcess1% && start "process 1" "%MyProcess1%"
%SystemRoot%\System32\tasklist.exe /NH | %SystemRoot%\System32\find.exe /i "%MyProcess2%">nul || echo starting %MyProcess2% && start "Process 2" "%MyProcess2%"
%SystemRoot%\System32\timeout.exe /t 10 /nobreak >nul
goto check
Full qualified file names are used for the commands tasklist, find and timeout to make this batch file independent on the values of the local environment variables PATH and PATHEXT and to avoid that the Windows command processor has to search for these three executables.
You can shorten your code like this:
#echo off
pushd "%~dp0"
:chkservice
tasklist | find /I "Process_1.exe" >nul 2>&1
if errorlevel 1 (
start Process_1.exe
echo %date% %time% Process_1.exe Down up, it >>startnote.txt
)
tasklist | find /I "Process_2.exe" >nul 2>&1
if errorlevel 1 (
start Process_2.exe
echo %date% %time% Process_2.exe Down up, it >>startnote.txt
)
timeout /t 10 /nobreak >nul 2>&1
goto chkservice

Close All Program Except these with CMD

i have this code :
This is a batch file code
#echo off
title Kill all running apps - Bharat Balegere - AgniPulse.com
cd c:\windows\System32
for /f "skip=3 tokens=1" %%i in ('TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running"') do (
if not "%%i"=="svchost.exe" (
if not "%%i"=="explorer.exe" (
if not "%%i"=="cmd.exe" (
if not "%%i"=="tasklist.exe" (
echo.
taskkill /f /im "%%i"
echo.
)
)
)
)
)
pause
but it will close all of the program
i try to edit it and add something like this if not "%%i"=="notepad.exe" (
but its not working anyway and when i click on batch file its nothing
so i don't know how to edit this
and put some except on it!
thanks in advance
You can give a try for this batch file with a whitelist for killing process
Of course you can modify your whitelist variable as you need :
#echo off
Title Kill all running apps - Bharat Balegere - AgniPulse.com
set "whitelist=avast mbam dllhost conhost"
Set "whitelist=%whitelist% taskeng skype"
Set "whitelist=%whitelist% firefox explorer cmd"
Set "whitelist=%whitelist% chrome tasklist taskmgr notepad"
#For /f "skip=3 tokens=1" %%i in (
'TASKLIST /FI "USERNAME eq %userdomain%\%username%" /FI "STATUS eq running" ^| findstr /VI "%whitelist%"'
) Do (
echo Taskkill /IM "%%i" /F
)
pause & exit
You can of course remove the echo command if that you want this list to be killed !
EDIT :
And here is another batch with whitelist using WMIC
#echo off
Title Process killer with a white list
Mode con cols=50 lines=40 & color 9B
setlocal
Set "White_Process_List=%~dpn0_White_Process_List.txt"
set "Ignoring_Process_List=%~dpn0_Ignoring_Process_List.txt"
set "Terimnated_Process_List=%~dpn0_Terimnated_List.txt"
If exist "%Ignoring_Process_List%" Del "%Ignoring_Process_List%"
If exist "%Terimnated_Process_List%" Del "%Terimnated_Process_List%"
:Whitelist
set "whitelist=Microsoft Mozilla Google Avast Panda ESET Kaspersky Avira AVG Bitdefender Malwarebytes Norton McAfee GAS IBM Skype"
If not exist "%White_Process_List%" echo %whitelist% > "%White_Process_List%"
Set /p whitelist=<"%White_Process_List%"
:Analyze
for /f "tokens=2 delims=," %%I in (
'wmic process get executablepath^,status /format:csv ^| find "\"'
) do (
set "proc=%%~I"
setlocal enabledelayedexpansion
wmic datafile where "name='!proc:\=\\!'" get manufacturer 2>nul | findstr /i "%whitelist%" >nul && (
( echo Ignoring : %%~nxI & echo "%%~I"
echo --------------------------------------- )>>!Ignoring_Process_List! 2>&1
) || (
( echo Terminating: %%~nxI )>con
( echo Terminating : %%~nxI & echo "%%~I"
Taskkill /im "%%~nxI" /f /t
echo --------------------------------------- )>>!Terimnated_Process_List! 2>&1
)
endlocal
)
Timeout /T 2 /nobreak>nul

Missing operator error using batch file

I've been trying to create a little batchscript to get the usages of your browser. So far so good everything works, it does what it should. Then I moved the file to another pc and now I'm getting "Missing Operator" errors eventho the program runs like it should. Any idea's?
#echo off
set date = %date
set time = %time
set sum=0
for /f "tokens=5 delims=," %%x in ('tasklist /fo csv /fi "imagename eq firefox.exe"') do (
for /f "tokens=1-5 delims=.K " %%a in ("%%~x") do set /a sum+=%%a%%b%%c%%d
)
echo %date%, %time%, firefox.exe, %sum%K > FirefoxDumpResult.csv
pause
:start
set date = %date
set time = %time
set sum=0
for /f "tokens=5 delims=," %%x in ('tasklist /fo csv /fi "imagename eq firefox.exe"') do (
for /f "tokens=1-5 delims=.K " %%a in ("%%~x") do set /a sum+=%%a%%b%%c%%d
)
echo %date%, %time%, firefox.exe, %sum%K >> FirefoxDumpResult.csv
set choice=
set /p choice="Do you want to log another one? Press 'y' and enter for Yes: "
if not '%choice%'=='' set choice=%choice:~0,1%
if '%choice%'=='y' goto start
You have not placed the closing percent characters on your %DATE% and %TIME% variables.
Additionally you shouldn't be creating variables which already exist and which don't need setting anyhow.
You have also pointlessly repeated a section of your code.
Finally you have not used the simpler code I provided for you in an earlier reply to another similar question using chrome.exe.
Try this:
#Echo Off
If /I Not "%CD%\" Equ "%~dp0" CD /D %~dp0
>FirefoxDumpResult.csv Type Nul
:Start
Set "_sum=0"
For /F "Tokens=6-7 Delims=., " %%a In (
'TaskList /NH /FI "ImageName Eq firefox.exe"') Do Set/A _sum+=%%a%%b
Echo=%DATE%, %TIME%, firefox.exe, %_sum%K>>FirefoxDumpResult.csv
Echo=
Echo=Firefox process information logged
Echo=
Choice /M "Do you want to log another one?"
If ErrorLevel 2 Exit/B
GoTo :Start
These are the outputs from both your and my versions:
::-------------------------------- Akorna Output -------------------------------
Type Nul 1>FirefoxDumpResult.csv
Set _sum=0
For /F "Tokens=6-7 Delims=., " %a In ('TaskList /NH /FI "ImageName Eq firefox.exe"') Do Set/A "_sum+=%a%b"
Set/A "_sum+=414 032K" Missing operator.
Echo=wo 07/09/2016, 16:39:43,48, firefox.exe, 414K 1>>FirefoxDumpResult.csv
::------------------------------------------------------------------------------
::-------------------------------- Compo Output --------------------------------
Type Nul 1>FirefoxDumpResult.csv
Set "_sum=0"
For /F "Tokens=6-7 Delims=., " %a In ('TaskList /NH /FI "ImageName Eq firefox.exe"') Do Set/A _sum+=%a%b
Set/A _sum+=333232
Echo=09/09/2016, 10:23:22.56, firefox.exe, 333232K 1>>FirefoxDumpResult.csv
::------------------------------------------------------------------------------
You have clearly altered the script; please make sure, at least, that line five in the script you're using matches the one I posted.

Batch file check if a program is running only once and close duplicate instances or launch it if none is running

I'm trying to realize a batch file that checks if a program is running and if not launch it, and more if is it launched more than one time close all the duplicate instances.
this is the code I implemented using also some tips found here in stackoverflow:
:check
tasklist /fi "imagename eq notepad.exe" /nh |find /i /c "notepad.exe" > "%temp%\variable.txt" #here I count how many instances are running
set /p value=<"%temp%\variable.txt" #and save the value in the variable.txt file
##check and choose action
if %value% equ nul goto none
if %value% geq 2 goto more
if %value% equ 1 goto one
:more
for /f "tokens=2" %%x in ('tasklist ^| findstr notepad.exe') do set PIDTOKILL=%%x
taskkill /F /PID %PIDTOKILL%
goto check
:none
start notepad.exe
goto check
:one
timeout 10 > nul
goto check
But some strange behavior happens when I test it...
if only one instances is running all fine, but if I close notepad while the batch file is running the routine goes to the :more label apparently without any reason...what I'm doing wrong?
thanks for any help
stupid error ... this is the working code:
:check
timeout 10 > nul
tasklist /fi "imagename eq notepad.exe" /nh |find /i /c "notepad.exe" > "%temp%\variable.txt"
set /p value=<"%temp%\variable.txt"
if %value% equ 0 goto none
if %value% geq 2 goto more
if %value% equ 1 goto check
:more
for /f "tokens=2" %%x in ('tasklist ^| findstr notepad.exe') do set PIDTOKILL=%%x
taskkill /F /PID %PIDTOKILL%
goto check
:none
start notepad.exe
goto check

Combination of `sleep` and `pause` command

Ok, after seeing crazy stuff being completed in so little code, I have high hopes this is possible.
Pretty much, I want to use the pause command normally, however, if the user doesn't input anything for a specified duration of time, it automatically continues.
In pseudo code:
(sleep %sleep-time%&Echo Pass)1>0 & pause
I thought at first I could do this using start /b to create a process that echoed input while being paused i the current thread, but that could cause problems if the user does input something.
Bonus
What would be really cool is if the errorlevel would be changed based on whether the user inputted something, or if the pause command timed out.
I suggest using timeout:
timeout /T 60 >NUL
This will sleep your script for 1 minute, or unless the user hits a key.
#echo off
setlocal
rem TimedPause.bat - Antonio Perez Ayala
if "%1" equ ":PausePart" goto PausePart
if "%1" neq "" goto begin
echo TimedPause.bat seconds
echo/
echo Wait for given seconds or until user press a key
echo At end, the presence of keyPressed.txt file indicate the cause of exit
goto :EOF
:begin
set seconds=%1
start "" /B "%~F0" :PausePart
for /F "skip=2 tokens=2 delims=," %%a in ('tasklist /FI "IMAGENAME eq cmd.exe" /FO CSV') do (
set PausePart=%%a
goto TimePart
)
:TimePart
ping -n 2 localhost > NUL
if exist keyPressed.txt goto :EOF
set /A seconds-=1
if %seconds% gtr 0 goto TimePart
taskkill /PID %PausePart% /F > NUL
goto :EOF
:PausePart
del keyPressed.txt 2> NUL
pause
echo %time% > keyPressed.txt
exit

Resources