PickOne
PickOne

Reputation: 37

batch result me "echo off" instead the proper result

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!

Upvotes: 1

Views: 69

Answers (2)

JosefZ
JosefZ

Reputation: 30113

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

==>

Upvotes: 1

Edward
Edward

Reputation: 1000

%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.

Upvotes: 1

Related Questions