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
Related
When i use it, it will only speak when i teach it another word, i can't get it to speak a specific answer in the directory, do you guys have any opinions on how to go about fixing this, i'm stumped..
#ECHO OFF
#mode con cols=55 lines=10
if not exist data.txt echo.>data.txt
:begin
set /p text="Talk: "
for /f "tokens=1,* delims=#" %%i in (data.txt) do (
if /i "%text%"=="%%i" (
echo Visu: %%j
goto begin
)
)
:Learn
echo --- I don't know that.
set /p answer=--- What would be a good answer to '%text%'?
echo %text%#%answer%>>data.txt
echo Visu: thank you
goto Aye
:Aye
echo set speech = Wscript.CreateObject("SAPI.spVoice") >> "temp.vbs"
echo speech.speak "%answer%" >> "temp.vbs"
start temp.vbs
pause
del temp.vbs
goto begin
Make Aye a subroutine/function you can call with the text to speak.
In a called sub the first argument is %1
#ECHO OFF
mode con cols=55 lines=10
if not exist data.txt Type Nul >data.txt
:begin
cls& Echo Talk to Visu
set "text=" & set /p text="Talk: "
if not defined text exit /B
for /f "tokens=1,* delims=#" %%i in (data.txt
) do if /i "%text%"=="%%i" Call :Aye "%%~j" & Goto :begin
:Learn
echo --- I don't know that.
echo --- What would be a good answer to '%text%'
set /p answer=?
echo %text%#%answer%>>data.txt
Call :Aye "%answer%"
goto :begin
:Aye
echo Visu: %~1
echo Visu: thank you
echo set speech = Wscript.CreateObject("SAPI.spVoice") > "temp.vbs"
echo speech.speak "%~1" >> "temp.vbs"
cscript //Nologo temp.vbs
Timeout /T 3 /Nobreak >Nul
del temp.vbs
goto :Eof
replace start temp.vbs with cscript /nologo temp.vbs.
And probably you want
echo set speech = Wscript.CreateObject("SAPI.spVoice") > "temp.vbs"
(> instead of >>) to overwrite the file every time?
I've recently changed jobs and I get to get my hands dirty on some scripting which I've always wanted to learn. I was given an existing batch file and wanted to gussy it up. Previously, this batch file would scan an IP addressed that you are prompted to enter. I want to change this to loop the command based on a list of IP addresses from a text file, only I'm having problems doing that.
I figured that I can do this one of two ways:
1) Run a batch file that will get the IP address, then run the 2nd batch based on that IP address.
OR
2) Just use the one existing batch file and change that to loop based on the IP address on each line of the text file.
What would be the better way to go, and how would you accomplish both?
For #1 I tried to do this, but I don't know how to run the command based on what I'm entering. An example of this would be to run batch.bat 192.168.1.1, which in batch.bat it would attempt to ping 192.168.1.1 (Or whatever entered).
Suppose that you have already a text file named : IP_List.txt with this contents :
192.168.1.1
192.168.1.2
192.168.1.3
192.168.1.4
192.168.1.5
192.168.1.6
192.168.1.7
192.168.1.8
192.168.1.9
192.168.1.10
192.168.1.11
192.168.1.12
192.168.1.13
192.168.1.14
192.168.1.15
192.168.1.16
192.168.1.17
192.168.1.18
192.168.1.19
192.168.1.20
www.google.com
www.stackoverflow.com
You can give a try for this batch file : MultiPingTester.bat
#echo off
Title Multi-Ping hosts Tester with colors by Hackoo 2016
call :init
set "MyFile=IP_List.txt"
If Not exist %MyFile% goto error
mode con cols=65 lines=30
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
echo(
call :color 0E " ------- Ping status of targets hosts -------" 1
echo(
(
echo ******************************************************
echo PingTest executed on %Date% # Time %Time%
echo ******************************************************
echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%MyFile%") do (
ping -n 1 %%a | find "TTL=" >nul
if errorlevel 1 (
call :color 0C " Host %%a not reachable KO" 1 & echo Host %%a not reachable KO >>%LogFile%
) else (
call :color 0A " Host %%a reachable OK" 1 & echo Host %%a reachable OK >>%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
::*************************************************************************************
:error
mode con cols=70 lines=3
color 0C
cls
echo(
echo ATTENTION !!! Check if the file "%MyFile%" exist !
pause>nul & exit
::*************************************************************************************
I have a block of code that is being executed anywhere from 0-100,000 times. The problematic block appears as so:
FOR /R %%f IN (*.*) DO (
CLS
ECHO Del 2.0
ECHO Files Done^: !FILES_DONE! / !FILES_TOTAL!
ECHO Missing^: !FILES_MISSING!
SETLOCAL DisableDelayedExpansion
SET "FILES_TEMP=%%f"
SETLOCAL EnableDelayedExpansion
IF EXIST "!FILES_TEMP!" (
!DELITE! -p 13 -q "!FILES_TEMP!" >NUL
SET /A FILES_DONE += 1
) ELSE (
SET /A FILES_MISSING += 1
)
)
In this block: !FILES_DONE!, !FILES_TOTAL!, and !FILES_MISSING! are all integer vars.
!DELITE! is a reference to an executable: "!CD!\_sdelete.exe" where "!CD!" is the root folder the batch is executed in.
Note: be advised that the DelayedExpansion being turned on/off is necessary as I need to capture file names that may or may not have special characters such as: "!" and "^".
So my question is: how can I reformat this block of code to not throw the Maximum Recursion Level Reached error? (I realize how frequently this question is asked and this is only half my question, so bear with me please)
Secondly, can someone explain how this block of code would act in terms of local environment?
SETLOCAL EnableDelayedExpansion
FOR /R %%f IN (*.*) DO (
ECHO !FILES_DONE! / !FILES_TOTAL!
ENDLOCAL & (
SET "FILES_TEMP=%%f"
)
[etc...]
)
For instance, once it loops through the code once, will the 'endlocal' still be in effect or will it be reinstated since it's a new loop? Also, will it contribute to the Maximum Setlocal Level Reached?
###########################################################################################
Del 2.0 - Full File Contents For Anyone That Wants To Use It
Note: If anyone wants an updated copy of the Del Batch, message me; it is currently on V3.0-R0, you need to use the sdelete.exe command-line utility from Microsoft. I may end up hosting this on a site as a free extension for anyone that wants a secure deletion program.
#ECHO OFF
SETLOCAL EnableDelayedExpansion
TITLE Del 2.0
GOTO PRECHECK
:PRECHECK
ECHO Del 2.0
ECHO Checking...
IF NOT EXIST "!CD!\To-Do" (
ECHO To-Do Folder Not Found
ECHO.
ECHO Nothing To Delete
PAUSE >NUL
GOTO END
)
IF NOT EXIST "!CD!\_sdelete.exe" (
ECHO SDelete Program Not Found
ECHO.
ECHO Core File "_sdelete.exe" Missing
PAUSE >NUL
GOTO END
) ELSE (
ATTRIB +S +H "!CD!\_sdelete.exe" >NUL
)
TIMEOUT /T 1 /NOBREAK >NUL
GOTO SETVARS
:SETVARS
CLS
ECHO Del 2.0
ECHO Writing Variables...
SET TAB=
SET FILES_TEMP=0
SET FILES_TOTAL=0
SET FILES_DONE=0
SET FILES_MISSING=0
SET ROOTCD=!CD!
SET HOMECD=!CD!\To-Do
SET DELITE="!CD!\_sdelete.exe"
TIMEOUT /T 1 /NOBREAK >NUL
GOTO START
:START
CLS
ECHO Del 2.0
ECHO Starting...
ATTRIB -S -H ** /S /D
CD To-Do
TIMEOUT /T 1 /NOBREAK >NUL
CLS
ECHO Del 2.0
ECHO Counting Files...
FOR /R %%f IN (**) DO (
CLS
SET /A FILES_TOTAL += 1
ECHO Del 2.0
ECHO Files Found^: !FILES_TOTAL!
)
TIMEOUT /T 1 /NOBREAK >NUL
GOTO ERASEFILES
:ERASEFILES
CLS
ECHO Del 2.0
ECHO Erasing Files...
SETLOCAL DisableDelayedExpansion
FOR /R %%f IN (*.*) DO (
SET "FILES_TEMP=%%f"
CALL :SUBERASE
)
GOTO FINISHERASE
:SUBERASE
CLS
ECHO Del 2.0
ECHO Files Done^: %FILES_DONE% / %FILES_TOTAL%
ECHO Missing^: %FILES_MISSING%
IF EXIST "%FILES_TEMP%" (
%DELITE% -p 13 -q "%FILES_TEMP%" >NUL
SET /A FILES_DONE += 1
) ELSE (
SET /A FILES_MISSING += 1
)
EXIT /B
:FINISHERASE
SETLOCAL EnableDelayedExpansion
CLS
ECHO Del 2.0
ECHO Files Done^: !FILES_DONE! / !FILES_TOTAL!
ECHO Missing^: !FILES_MISSING!
TIMEOUT /T 3 /NOBREAK >NUL
GOTO CLEANUP
:CLEANUP
CLS
ECHO Del 2.0
ECHO Cleaning Up...
IF !FILES_MISSING! EQU 0 (
FOR /D %%f in (*.*) DO (
SETLOCAL DisableDelayedExpansion
SET "FILES_TEMP=%%f"
SETLOCAL EnableDelayedExpansion
RD "!FILES_TEMP!" /S /Q >NUL
)
)
TIMEOUT /T 1 /NOBREAK >NUL
GOTO END
:END
CLS
ECHO Del 2.0
ECHO Complete^^!
TIMEOUT /T 3 /NOBREAK >NUL
EXIT
Keep Delayed Expansion disabled using call command as follows:
#ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion
FOR /R %%f IN (*.*) DO (
SET "FILES_TEMP=%%f"
CALL :doAll
)
goto :skipSubroutine
:doAll
CLS
ECHO Del 2.0
ECHO Files Done^: %FILES_DONE% / %FILES_TOTAL%
ECHO Missing^: %FILES_MISSING%
IF EXIST "%FILES_TEMP%" (
%DELITE% -p 13 -q "%FILES_TEMP%" >NUL
SET /A FILES_DONE += 1
) ELSE (
SET /A FILES_MISSING += 1
)
goto :eof
:skipSubroutine
If FILES_TEMP variable requires Delayed Expansion enabled then the :doAll subroutine could look as follows (Note that ENDLOCAL command is used twice to update variables FILES_DONE and FILES_MISSING correctly in script scope):
:doAll
CLS
ECHO Del 2.0
ECHO Files Done^: %FILES_DONE% / %FILES_TOTAL%
ECHO Missing^: %FILES_MISSING%
SETLOCAL EnableDelayedExpansion
IF EXIST "!FILES_TEMP!" (
%DELITE% -p 13 -q "!FILES_TEMP!" >NUL
ENDLOCAL
SET /A FILES_DONE += 1
) ELSE (
ENDLOCAL
SET /A FILES_MISSING += 1
)
goto :eof
I wrote a windows batch script to check and move files to another directory based on the list I put in a notepad file named list.txt. But I have no idea that how to set the while-loop. Only to jump out of the subroute when the condition fulfill.
In C Programming, we could write like this by setting a while-loop direcly. But seems in windows batch is quite different.
All I want is like this:
Directory A:
1. A.txt
2. B.txt
3. list.txt (By line sequential with filename want to move)
4. process.bat
Directory B:
None of files (Then move a file by order of line set in list.txt) OR
A.txt (If already existed a text file in directory, process.bat will pause before A.txt disappear)
Process.bat
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
if exist D:\DirectoryA\*.txt (
echo %time% >> D:\AutoLog\Log.txt
echo Previous job did not finished yet. >> D:\AutoLog\Log.txt
Timeout /t 30
echo.
)
set name=%*
if exist %name%.txt (
echo %time% >> D:\AutoLog\Log.txt
echo File found and processing %name%.txt now... >> D:\AutoLog\Log.txt
copy "%~dp0\%name%.txt" "D:\DirectoryB"
echo Transfer %name%.txt completed!! >> D:\AutoLog\Log.txt
echo. >> D:\AutoLog\Log.txt
Timeout /t 790
echo.
echo ==============================================================
)
:eof
Please guide me to finish the script by using a while-loop method. Thanks
I change some script sequence and it works now.
#echo off
:readline
for /f "tokens=*" %%a in (list.txt) do call :processline %%a
goto :eof
:processline
set name=%*
if exist C:\Test2\*.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo Previous job did not finished yet. >> C:\Test2\Log.txt
Timeout /t 5
echo.
echo. >> C:\Test2\Log.txt
goto :processline
)
if exist %name%.txt (
echo %date% %time% >> C:\Test2\Log.txt
echo File found and processing %name%.txt now... >> C:\Test2\Log.txt
copy "%~dp0\%name%.txt" "C:\Test2"
echo Transfer %name%.txt completed!! >> C:\Test2\Log.txt
echo. >> C:\Test2\Log.txt
Timeout /t 10
echo.
echo ==============================================================
)
:eof
This will copy as well as count the number of lines from your text file..
# echo off
:TextPath
cls
set /p Input=#1 Enter the full path of the text file :
set /p Source=#2 Enter the full path of Source :
set /p Target=#3 Enter the full path of Destination :
:choice
set /P c=Ready to Continue[Y/N]?
if /I "%c%" EQU "Y" goto :Yes
if /I "%c%" EQU "N" goto :No
goto :choice
:Yes_Local
for /f "delims=" %%i in (%Input%) do echo f| xcopy /f /y "%Source%\%%i" "%Target%\%%i"
for /f %%C in ('Find /V /C "" ^< %Input%') do set Count=%%C
msg * No of Lines executed= %Count%
exit
:No
cls
color e
echo Redirecting to Main....
PING 127.0.0.1 -n 2 >NUL
cls
echo Please wait
PING 127.0.0.1 -n 4 >NUL
goto TextPath
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.