Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Cornbeetle
- Posts: 31
- Joined: 23 Nov 2015 09:34
#1
Post
by Cornbeetle » 30 Sep 2016 11:39
Credit to Aicini for using his idea for voice synthesis in this post:
http://www.dostips.com/forum/viewtopic.php?f=3&t=6841&p=44362&hilit=sapi#p44362 Code: Select all
@echo off
:: create random string of numbers to ensure no duplicate file names
set numbers=%random%%random%%random%
:: echo "voice script" commands into batch file
echo @set @a=0 /*>%temp%\speak_%numbers%.bat
echo @CScript //nologo //E:JScript "%%~F0" "%%1 %%2 %%3 %%4 %%5" ^& goto :EOF */>>%temp%\speak_%numbers%.bat
echo WScript.CreateObject("SAPI.SpVoice").Speak(WScript.Arguments(0))>>%temp%\speak_%numbers%.bat
:start
set /p address="Address: "
cls
SET /P X=Pinging "%address%" - Status: <NUL
set oldstate=neither
:loop
set state=down
for /f "tokens=4,7" %%G in ('ping %address% -n 5 ^| find /i "sent"') do (
if "%%G%%H"=="5,5," set state=up
)
if not %state%==%oldstate% (
echo.Link is %state%
set oldstate=%state%
)
if %state%==up goto:linkup
ping -n 2 localholst >nul: 2>nul:
goto :loop
:: Display timestamp and execute "voice script" with variables
:linkup
echo %time% %date%
echo.
call %temp%\speak_%numbers%.bat Connection established with, %address%
call %temp%\speak_%numbers%.bat Repeat. Connection established with, %address%
pause>nul
del %temp%\speak_%numbers%.bat
goto:start
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#2
Post
by Squashman » 30 Sep 2016 12:51
Is there a question you need answered?
Not quite understanding why you are creating a batch file to execute jscript code?
It would make more sense to do one of two things.
1) Just create a jscript file and execute it from your main batch file.
2) Make your main batch file a hybrid batch/jscript file.
-
Cornbeetle
- Posts: 31
- Joined: 23 Nov 2015 09:34
#3
Post
by Cornbeetle » 30 Sep 2016 13:09
Squashman wrote:Is there a question you need answered?
Not quite understanding why you are creating a batch file to execute jscript code?
It would make more sense to do one of two things.
1) Just create a jscript file and execute it from your main batch file.
2) Make your main batch file a hybrid batch/jscript file.
Hybrids have always been my downfall with batch scripting. I usually resort to outputting commands to a file and then having the main script execute that newly create file.
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#4
Post
by Squashman » 30 Sep 2016 13:30
Cornbeetle wrote:Hybrids have always been my downfall with batch scripting. I usually resort to outputting commands to a file and then having the main script execute that newly create file.
That is ironic because the batch file you are creating in your main code is creating a hybrid batch/jscript file.
-
Cornbeetle
- Posts: 31
- Joined: 23 Nov 2015 09:34
#5
Post
by Cornbeetle » 30 Sep 2016 13:58
Squashman wrote:Cornbeetle wrote:Hybrids have always been my downfall with batch scripting. I usually resort to outputting commands to a file and then having the main script execute that newly create file.
That is ironic because the batch file you are creating in your main code is creating a hybrid batch/jscript file.
You are correct [face palm]. What I mean is that I would like to learn to create a hybrid independent (without outputting or using temp files.)
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#7
Post
by aGerman » 30 Sep 2016 16:20
Maybe that explanation helps you to understand how it works:
viewtopic.php?t=7209&p=46984#p46984I think your script could be changed like that
Code: Select all
@if (@a)==(@b) @end /* Batch part:
@echo off &setlocal EnableDelayedExpansion &set "address="
for /l %%i in () do (
if not defined address set /p "address=Address: " &cls
>nul 2>&1 ping -n 1 !address! && (
echo !time! !date! &echo(
cscript //nologo //e:jscript "%~fs0" "Connection established with, !address!. Repeat. Connection established with, !address!."
set "address="
) || (echo Ping failed. &>nul timeout /t 1 /nobreak)
)
JScript part: */
WScript.CreateObject('SAPI.SpVoice').Speak(WScript.Arguments(0))
EXIT /B or GOTO :EOF is unnecessary here because the Batch part runs an infinite loop and will never reach the JScript part.
Steffen
-
Cornbeetle
- Posts: 31
- Joined: 23 Nov 2015 09:34
#8
Post
by Cornbeetle » 01 Oct 2016 09:53
@aGerman
2 things:
Could you explain how it runs the JScript code and knows where to find it embedded within the batch?
Also, what is happening when there is nothing in the parenthesis in this line
for /l %%i in ()
?
-
aGerman
- Expert
- Posts: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#10
Post
by aGerman » 01 Oct 2016 10:51
Cornbeetle wrote:Could you explain how it runs the JScript code and knows where to find it embedded within the batch?
My link should have explained it
You pass the script to cscript.exe in order to execute it as JScript code. Everything between /* and */ is a JScript comment and will be ignored. Only the code after */ will be executed.
Cornbeetle wrote:Also, what is happening when there is nothing in the parenthesis in this line
for /l %%i in ()
?
It's an infinite loop. The code in the body of the loop will be repeatedly executed.
Steffen
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#11
Post
by Squashman » 01 Oct 2016 16:35
Cornbeetle wrote:Also, what is happening when there is nothing in the parenthesis in this line
for /l %%i in ()
?
Well you could put that code in another batch file and see what it does.