Check for multiple registry keys in batch file - batch-file

I need to check the registry for multiple keys before I start my program (they shouldn't exist). The widely spread solution for checking registry keys works for only one check since it sets global ErrorLevel to 1. The example below won't work correctly.
#echo off
reg query HKEY_LOCAL_MACHINE\SOFTWARE\mykey >nul
if %errorlevel% equ 0 (
echo "mykey exists - do nothing"
) else (
reg query HKEY_LOCAL_MACHINE\SOFTWARE\mykey2 >nul
if %errorlevel% equ 0 (
echo "mykey2 exists - do nothing"
) else (
run my program
)
)

Using errorlevel like this will require delayed expansion.You can try with IF ERRORLEVEL
#echo off
reg query HKEY_LOCAL_MACHINE\SOFTWARE\mykey >nul
if %errorlevel% equ 0 (
echo "mykey exists - do nothing"
) else (
reg query HKEY_LOCAL_MACHINE\SOFTWARE\mykey2 >nul
if errorlevel 1 (
run my program
) else (
echo "mykey2 exists - do nothing"
)
)

Get inspired by this example :
#echo off
cls & color 0A & echo.
Mode con cols=70 lines=5
Title Check Startup Registry Keys
Set TmpLogFile=TmpLogkey.txt
Set LogFile=Startup_key_Log.txt
If Exist %TmpLogFile% Del %TmpLogFile%
If Exist %LogFile% Del %LogFile%
Set mykey="HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run"^
^ "HKCU\Software\Microsoft\Windows\CurrentVersion\Run"^
^ "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon"^
^ "HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run"^
^ "HKCU\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run"^
^ "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options"
Echo.
ECHO **************************************
ECHO Please wait..........
ECHO **************************************
For %%K in (%mykey%) Do Call :Check_Key %%K %TmpLogFile%
Cmd /U /C Type %TmpLogFile% > %LogFile%
Start "" %LogFile%
Exit /b
::********************************************
:Check_Key
reg QUERY %1 >nul 2>&1
(
if %errorlevel% equ 0 ( reg QUERY %1 /s
) else ( echo %1 ===^> Not found
)
) >>%2 2>&1
::********************************************

Related

error loading text as variable from file and check it

this is some code it suppose to check for adb.exe in a location then if it exist will proceed to code if not will ask user to input location manually and check again if location is right will save the input in file "dontremoveoredit" to use it next time user open the bat file
the problem is if the file is empty or contain path it will crash the bat and close
:: #echo off
If exist "C:\Program Files\ui\adb.exe" (
goto yes
) else (
goto adbexist
)
:yes
set PATH=%PATH%;C:\Program Files\ui"
:: #cd/d "%~dp0"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
Echo.
Echo.
Echo.
echo Yes Was Executed
Pause>nul
Exit
:no
msg * "Couldn't find The adb.exe in Default Bath"
ping localhost -n 3 >Nul
goto noadbtext
:adbexist
If exist dontremoveoredit (
goto adbexist1
) else (
goto no
)
:adbexist1
set /p var=<dontremoveoredit
if [%var%] == [] (
goto noadbtext
) else (
goto lala
)
:lala
If exist %var%\adb.exe (
set PATH=%PATH%;%var%"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
goto dns
) else (
msg * "Make Sure You Entered The Right Path To UI"
goto noadbtext
)
:noadbtext
break>"dontremoveoredit"
set /p EmulatorUIBath=Enter Your Emulator's UI folder Path :
If exist "%EmulatorUIBath%\adb.exe" (
#echo %EmulatorUIBath%>> "dontremoveoredit"
goto adbexist2
) else (
msg * "Make Sure You Entered The Right Path To UI"
goto noadbtext
)
:adbexist2
set PATH=%PATH%;%EmulatorUIBath%"
adb.exe kill-server
adb.exe devices
adb connect localhost:5037
echo adb Exist2
Pause>nul
exit
Here you are my laptop is about to die but this should do the needful nicely. :)
Slightly Updated Version I mentioned I woudl post.
#(
SETLOCAL EnableDelayedExpansion
ECHO OFF
SET "_ConfigFilePath=%~0dpn_DO_NOT_REMOVE.dat"
SET "_ADBPath="
SET "_eLvl=0"
SET "ECHO_RW=Call :Echo_Color CF "
SET "ECHO_GB=Call :Echo_Color 20 "
SET "ECHO_AB=Call :Echo_Color B0 "
SET "ECHO_LY=Call :Echo_Color 1E "
SET "ECHO_YL=Call :Echo_Color E1 "
SET "ECHO_YR=Call :Echo_Color EC "
SET "ECHO_RY=Call :Echo_Color CE "
SET "ECHO_BY=Call :Echo_Color 1E "
)
CALL :Main
(
ECHO. Script Completed With ErrorLevel of %_eLvl%
ENDLOCAL
Exit /B %_eLvl%
)
:Echo_Color
ECHO.>"%~2"
FINDStr /A:%~1 /I ".*" "%~2" NUL
DEL /F /Q "%~2" >nul 2>nul
GOTO :EOF
:Main
REM Checing for ADB
CALL :CheckADB
IF /I %_eLvl% NEQ 0 (
ECHO. Error Encountered, Exiting!
) ELSE (
CALL :RunADB
)
PAUSE
GOTO :EOF
:CheckADB
REM Check For ADB Installed in Program Directories
FOR /F "Tokens=1* Delims==" %%A IN ('
SET ProgramFiles
') DO (
FOR /F "tokens=*" %%a IN ('
WHERE /R "%%A" /F "adb.exe" 2^>nul
') DO (
ECHO Found ADB here: "%%~a"
SET "_ADBPath=%%~a"
)
)
REM ECHO.Finished Where Loop
IF NOT DEFINED _ADBPath (
ECHO. ADB Install not Found, checking Config file.
IF EXIST "!_ConfigFilePath!" (
ECHO. Config File Exists checking contents..
FOR /F "Tokens=*" %%A IN ('
TYPE "!_ConfigFilePath!"
') DO (
ECHO. Saved ADB Path Found: "%%~A" in Config file.
IF EXIST "%%~A" (
SET "_ADBPath=%%~A"
) ELSE (
ECHO. Saved ADB Path "%%~A" Does Not Exist! Deleteing Config File.
CALL :Get_ADB_Path_From_User
)
)
) ELSE (
CALL :Get_ADB_Path_From_User
)
)
GOTO :EOF
:Get_ADB_Path_From_User
CLS
REM COLOR CF
ECHO.
COLOR 1E
FOR %%A IN (
"==========================================================="
"= ADB was not detected on your system ="
"==========================================================="
) DO (
REM ECHO.>"%%~A"
REM FINDStr /A:CF /I ".*" "%%~A" NUL
REM DEL /F /Q "%%~A" >nul 2>nul
%ECHO_RY% "%%~A"
)
ECHO.
ECHO.
%ECHO_AB% "Please enter a custom path to ADB below."
ECHO.
ECHO.
SET /P "_TmpADBPath=What Is the Path to your ADB Install? "
CLS
IF NOT EXIST "%_TmpADBPath%" (
%ECHO_RW% "==========================================================="
%ECHO_RW% "== =="
%ECHO_RW% "== Unable to Verify that The path Provided Exists =="
%ECHO_RW% "== =="
%ECHO_RW% "==========================================================="
SET "_eLvl=1"
GOTO :EOF
) ELSE (
COLOR 2F
SET "_ADBPath=%_TmpADBPath%"
ECHO.===========================================================
ECHO. Path Exists:
ECHO.
ECHO. "%_TmpADBPath%"
ECHO.
ECHO. ===========================================================
ECHO.
ECHO.
CHOICE /M "Save this Path for future Use?"
IF /I "%ERRORLEVEL%" EQU "0" (
ECHO.%_TmpADBPath%>"%_ConfigFilePath%"
ECHO.
ECHO. Saved to: "%_ConfigFilePath%"
) ELSE (
ECHO.
ECHO. Okay, Will only use it temporarily.
)
COLOR
)
GOTO :EOF
:RunADB
"%_ADBPath%" kill-server
"%_ADBPath%" devices
"%_ADBPath%" connect localhost:5037
Echo.
Echo.
Echo.
Echo.Yes Was Executed
Pause>nul
Exit

How to force exit command line bat file when occur download error

Dears
Here is my .bat command line
I use bitsadmin /transfer command download from url.
but got error(like disconnect network...etc) I need continue execute remain command.
But now I can't get achievement... what should I do
#echo off
:: Filter updater for HCK and HLK
:::::::::::::::::::::::::: Settings :::::::::::::::::::::::::::::::::
:: Notice: As of July 2015, the HCK and the HLK filter updates are the exact same file, downloaded from the same location!
SET "source=https://sysdev.microsoft.com/member/SubmissionWizard/LegalExemptions/HCKFilterUpdates.cab"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
SET "destination=C:\FilterUpdates.cab"
if not exist "%DTMBIN%\" (
echo ERROR: folder "%DTMBIN%"
echo does not exist! Please verify that you have the controller installed.
pause
exit /B 1
)
echo Please make sure that all instances of the Studio are turned OFF!
echo Downloading Filters...
bitsadmin /transfer "Downloading Filters" "%source%" "%destination%"
if errorlevel 1 goto end
echo Extracting...
expand -i "%destination%" -f:UpdateFilters.sql "%DTMBIN%\"
if not errorlevel 0 echo ERROR & exit /B 1
echo Installing...
pushd "%DTMBIN%\"
if not errorlevel 0 echo ERROR & exit /B 1
"%DTMBIN%\updatefilters.exe " /s
if not errorlevel 0 echo ERROR & exit /B 1
popd
:end
exit
Your problem could be caused by the errorlevels generated by bitsadmin: some of them are negative values and the test if errorlevel 1 will be evaluated as false (if errorlevel n is true for values greater than or equal to n)
You will need to read and test the value of the errorlevel variable
if not %errorlevel%==0 exit /b 1
But sometimes, bitsadmin will have errors and will generate a errorlevel 0, so, you need to manualy check the status
#echo off
setlocal enableextensions disabledelayedexpansion
:: Filter updater for HCK and HLK
:::::::::::::::::::::::::: Settings :::::::::::::::::::::::::::::::::
:: Notice: As of July 2015, the HCK and the HLK filter updates
:: are the exact same file, downloaded from the same location!
SET "source=https://sysdev.microsoft.com/member/SubmissionWizard/LegalExemptions/HCKFilterUpdates.cab"
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
if not exist "%DTMBIN%\" (
echo ERROR: folder "%DTMBIN%"
echo does not exist! Please verify that you have the controller installed.
pause
exit /B 1
)
SET "destination=C:\FilterUpdates.cab"
if exist "%destination%" del /q "%destination%"
echo Please make sure that all instances of the Studio are turned OFF!
echo Creating download task...
set "taskName=[HCK_FilterUpdater]"
>nul (
rem remove task if already present
bitsadmin /list | find "%taskName%" && bitsadmin /cancel "%taskName%"
rem create the task
bitsadmin /create "%taskName%"
rem include our file in the task
bitsadmin /addfile "%taskName%" "%source%" "%destination%"
rem start the download
bitsadmin /resume "%taskName%"
)
echo Downloading...
set "exitCode="
for /l %%a in (1 1 500) do if not defined exitCode for /f "delims=" %%a in ('
bitsadmin /info "%taskName%"
^| findstr /b /l /c:"{"
') do for /f "tokens=3,*" %%b in ("%%a") do (
if "%%~b"=="TRANSFERRED" (
set "exitCode=0"
>nul bitsadmin /complete "%taskName%"
echo ... done
)
if "%%~b"=="ERROR" (
set "exitCode=1"
bitsadmin /geterror "%taskName%" | findstr /b /c:"ERROR"
>nul bitsadmin /cancel "%taskName%"
)
if not defined exitCode (
echo(%%b %%c
timeout /t 2 >nul
)
)
if not defined exitCode ( echo TIMEOUT & exit /b 1 )
if not exist "%destination%" ( echo ERROR & exit /b 1 )
echo Expanding...
>nul expand -i "%destination%" -f:UpdateFilters.sql "%DTMBIN%"
if errorlevel 1 ( echo ERROR & exit /b 1 )
echo Installing...
pushd "%DTMBIN%" || ( echo ERROR & exit /b 1 )
".\updatefilters.exe " /s || ( echo ERROR & exit /b 1 )
popd
It seems like once command shell is in bitsadmin mode you can't take it out of it. My solution unfortunately is two seperate .bats.

Pinging Multiple PCs and Adding Text

I'm pretty new to this so please bear with me, and if you require anymore information from me please say. Thanks in advance for your help.
I have this code that pings different PCs then returns back if they are online/offline. I wanted to know if you could add another column once the bat file has ran its ping test so it has the computer name next to it.
#echo off
if exist C:\tools\computers.txt goto Label1
echo.
echo Cannot find C:\tools\computers.txt
echo.
Pause
goto :eof
:Label1
echo PingTest executed on %date% at %time% > C:\tools\z.txt
echo ================================================= >> C:\tools\z.txt
for /f %%i in (C:\tools\computers.txt) do call :Sub %%i notepad C:\tools\z.txt
goto :eof
:Sub
echo Testing %1 set state=alive ping -n 1 %1 | find /i "bytes=" || set state=dead echo %1 is %state% >> C:\tools\z.txt
The bat file creates a document that shows the following;
PingTest executed on 28/07/2016 at 13:10:28
99.1.82.28 is alive
99.1.82.100 is alive
ect.
If possible I would like the bat file to run so it displays this;
The bat file creates a document that shows the following;
PingTest executed on 28/07/2016 at 13:10:28
Computer 1 : 99.1.82.28 is alive
Computer 2 : 99.1.82.100 is alive
ect.
--
Would appreciate any help & guidance on this.
Thanks.
You can try this solution :
#echo off
Title Ping Test
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
(
echo ******************************************************
echo PingTest executed on %Date% # Time %Time%
echo ******************************************************
echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
ping -n 1 %%a>nul && set "msg=%%a : !ip! ALive ok" || set "msg=%%a : !ip! Dead failed to respond"
echo !msg!
echo !msg! >> %LogFile%
)
)
EndLocal
Start "" %LogFile%
pause>nul & exit
EDIT : on 29/07/2016 # 12:48
Another version with multi-colors : Special thanks goes to ICARUS for the color function (-_°)
#echo off
Rem Special thanks goes to Iracus for the color function (-_°)
mode con cols=60 lines=20
Title Multi-Ping hosts Tester with Multi-colors by Hackoo
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
call :init
echo(
call :color 0E "------- Ping Status of Computers hosts -------" 1
echo(
(
echo ******************************************************
echo PingTest executed on %Date% # Time %Time%
echo ******************************************************
echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
ping -n 1 %%a>nul && set "msg=%%a - !ip! ALive ok" && Call :Color 0A "!msg!" 1 || set "msg=%%a - !ip! Dead failed to respond" && Call :Color 0C "!msg!" 1
echo !msg! >> %LogFile%
)
)
EndLocal
Start "" %LogFile%
pause>nul & exit
:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b
:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
<nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b
EDIT : Update On 23/08/2016
http://pastebin.com/zjYwSqUM

Batch errorlevel is not working properly

so i have really problems with the ERRORLEVEL of batch. Its just not working for me.
I have a big own ms build batch script and i always get 0 back from ERRORLEVEL, whatever I do (eg. msbuild, tf get, tf checkout, copy, xcopy,...)
so i did a small example to post it here:
#echo off
set Update=1
IF %Update% == 1 (
echo.
set /p "=- Copy stuff..." <NUL
xcopy /R /Y C:\test\2.lib C:\test1
if %ERRORLEVEL% neq 0 (echo FAILED!) ELSE (echo SUCCEED!)
echo -^> done
pause
)
so its always returning succeed and printing 0 when i do: echo %ERRORLEVEL%
can you please help me with that? I really would like to use that errorlevel
you need delayed expansion here or to use IF ERRORLEVEL :
#echo off
set Update=1
IF %Update% == 1 (
echo.
set /p "=- Copy stuff..." <NUL
xcopy /R /Y C:\test\2.lib C:\test1
IF ERRORLEVEL 1 (echo FAILED!) ELSE (echo SUCCEED!)
echo -^> done
pause
)
with IF ERRORLEVEL 1 you can check if the errorlevel is 1 or bigger .
As npocmaka says, you have a delayed expansion issue.
An alternative is to ditch ERRORLEVEL and use the && and || conditional command concatenation operators instead.
#echo off
set Update=1
IF %Update% == 1 (
echo.
set /p "=- Copy stuff..." <NUL
xcopy /R /Y C:\test\2.lib C:\test1 && (echo SUCCEED!) || (echo FAILED!)
echo -^> done
pause
)
Edit showing use of multiple lines
#echo off
set Update=1
IF %Update% == 1 (
echo.
set /p "=- Copy stuff..." <NUL
xcopy /R /Y C:\test\2.lib C:\test1 && (
echo First success command
echo SUCCEED!
) || (
echo First failure command
echo FAILED!
)
echo -^> done
pause
)

There has to be a better way to do this. (Batch file help)

Alright, so I'm looking for a better way to do this and I know there has to be since this is getting ridiculous. I want to add a 3rd service to the file, but that is a lot of fiddling around with the numbers I was using and I'll surely break something.
I'm really wanting to learn, but I'm really new to this so don't be too harsh!
#echo off
echo This will start your SERVICES.
echo If you do not wish to do this, please close this window, otherwise:
pause
:CORE
:DISABLED
for /F "tokens=3 delims=: " %%H in ('sc qc "SERVICE1" ^| findstr " START_TYPE"') do (
if /I "%%H" EQU "DISABLED" (
echo.
echo SERVICE1 is disabled!
echo.
set A=1
set C=3
set E=0
set M=1
goto FILE
)
if /I "%%H" NEQ "DISABLED" (
set A=0
set M=0
)
)
:STATUS
for /F "tokens=3 delims=: " %%H in ('sc query "SERVICE1" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
echo.
echo Starting SERVICE1... & SC Start "SERVICE1" | findstr /i /c:"1069" && (Echo.
echo ERROR 1069: SERVICE1 failed to start due to a unknown
echo username or bad password.
echo.
set E=3
set C=3
) || (
echo Successful
echo.
set C=0
set E=0
)
)
if /I "%%H" EQU "RUNNING" (
echo.
echo SERVICE1 is already started!
echo.
set C=1
set E=0
)
)
:FILE
:DISABLED
for /F "tokens=3 delims=: " %%H in ('sc qc "SERVICE2" ^| findstr " START_TYPE"') do (
if /I "%%H" EQU "DISABLED" (
echo.
echo SERVICE2 is disabled!
echo.
set B=1
set D=3
set F=0
set N=1
goto FIX
)
if /I "%%H" NEQ "DISABLED" (
set B=0
set N=0
)
)
:STATUS
for /F "tokens=3 delims=: " %%H in ('sc query "SERVICE2" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
echo Starting SERVICE2... & SC Start "SERVICE2" | findstr /i /c:"1069" && (Echo.
echo ERROR 1069: SERVICE2 failed to start due to a unknown
echo username or bad password.
echo.
set F=3
set D=3
) || (
echo Successful
echo.
set D=0
set F=0
)
)
if /I "%%H" EQU "RUNNING" (
echo.
echo SERVICE2 is already started!
echo.
set D=1
set F=0
)
)
:MESSAGE
set /a G=%C%+%D%
if "%G%" LEQ "1" (
echo.
echo Your services should now be started!
echo.
)
if "%G%" EQU "2" (
echo.
echo No problem found!
echo.
)
:FIX
set /a O=%M%+%N%
if "%O%" EQU "2" (set S=Services&& goto HELP)
if "%M%" EQU "1" (set S=SERVICE1&& goto HELP)
if "%N%" EQU "1" (set S=SERVICE2&& goto HELP)
:HELP
set /a H=%A%+%B%
if "%H%" NEQ "0" (
echo.
echo To enable the %S%, follow these steps:
echo ^(1^) Open your start menu.
echo ^(2^) Type in ^"Services.msc" and click on the top option.
echo ^(3^) Scroll down in the list to find the ^"SERVICE1" and
echo the "SERVICE2".
echo ^(4^) Right-click on each respective service and select properties.
echo ^(5^) In the middle of the screen, change ^"Startup type" to "Automatic".
echo ^(6^) Once this is done for the respective service^(s^), run this file again.
echo.
set P=1
)
set /a I=%E%+%F%
if "%I%" GEQ "3" (
echo.
echo To correct the 1069 error, follow these steps:
echo ^(1^) Open your start menu.
echo ^(2^) Type in ^"Services.msc" and click on the top option.
echo ^(3^) Scroll down in the list to find the ^"SERVICE1" and
echo the "SERVICE2".
echo ^(4^) Right-click on each respective service and select properties.
echo ^(5^) At the top click on the ^"Log On" tab.
echo ^(6^) Type in the correct password for the named user and confirm it just below.
echo ^(7^) Once this is done for the respective service^(s^), run this file again.
echo.
set P=1
)
if "%P%" EQU "1" (
echo.
echo If any additional errors came up or if this same message comes up after
echo following the above steps, be sure to give Tech Support a call at
echo 1-800-XXX-XXXX.
echo.
)
:FOR DEBUG USE
:echo A^=%A%
:echo B^=%B%
:echo C^=%C%
:echo D^=%D%
:echo E^=%E%
:echo F^=%F%
:echo G^=%G%
:echo H^=%H%
:echo I^=%I%
:echo M^=%M%
:echo N^=%N%
:echo O^=%O%
:echo P^=%P%
:echo S^=%S%
echo To close this window:
pause
Thanks in advance! - Trevor
The principal is functions. You are doing the same thing over and over again.
Create a file with a list of services.
win32time
wscsvc
wuauserv"
To start get a list of services.
for /f %%A in (c:\services.txt) do call Main %%A
goto :EOF
Then the main worker function I just changed service name to %1 and added :main at top and goto :eof at bottom. This will be called three times which each of the three service above as %1. I haven't checked your logic though.
:MAIN
for /F "tokens=3 delims=: " %%H in ('sc qc "%1" ^| findstr " START_TYPE"') do (
if /I "%%H" EQU "DISABLED" (
echo.
echo %1 is disabled!
echo.
set A=1
set C=3
set E=0
set M=1
goto FILE
)
if /I "%%H" NEQ "DISABLED" (
set A=0
set M=0
)
)
:STATUS
for /F "tokens=3 delims=: " %%H in ('sc query "%1" ^| findstr " STATE"') do (
if /I "%%H" NEQ "RUNNING" (
echo.
echo Starting %1... & SC Start "%1" | findstr /i /c:"1069" && (Echo.
echo ERROR 1069: %1 failed to start due to a unknown
echo username or bad password.
echo.
set E=3
set C=3
) || (
echo Successful
echo.
set C=0
set E=0
)
)
if /I "%%H" EQU "RUNNING" (
echo.
echo %1 is already started!
echo.
set C=1
set E=0
)
)
goto :eof
In programming we don't check something then do, we do then check if it worked. It usually takes a similar time to check as to do. Therefore you are using twice the battery power and putting twice the carbon in the atmosphere, etc. If on a server you may need a bigger one.
This is what you seem to want to do.
sc config %1 start= auto
sc start %1
Then do your test for wrong password (1069), and if already running you'll get error 1056.

Resources