1

This is my batch script:

@echo off
title 

setlocal enabledelayedexpansion
:a
set /p a=
!d!
for %%G in (%a%) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
echo wscript.createobject("sapi.spvoice").speak "!c!">a.vbs
start a.vbs

exit

For every time that this program runs, it overwrites the a.vbs file with the new code as variable c. Is it possible to have "wscript.createobject("sapi.spvoice").speak "!c!"" preexisting in a VBScript and simply have batch assign the variable and execute it instead of overwriting and then executing?

With the help of aphoria, I tweaked my scripts to this: VBScript:

wscript.createobject("sapi.spvoice").speak wscript.arguments(0)

Batch Script:

@echo off
title 

setlocal enabledelayedexpansion
set /p a=
for %%G in (!a!) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
cscript //nologo b.vbs !c!

exit

4 Answers 4

1

May I suggest you another solution?

JScript language is similar to VBScript, but have an advantage in this case: the JScript code can be placed inside the Batch file itself via a very simple trick. This way, it is not necessary to create a separated file with the JScript code:

@if (@CodeSection == @Batch) @then

:: Previous line is:
:: - in Batch: a valid IF command that does nothing
:: - in JScript: a conditional compilation IF statement that is false
::   so the following code is omitted until the next atSign-end

@echo off
title 

setlocal enabledelayedexpansion
set /p a=
for %%G in (!a!) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
rem Execute this Batch file as a JScript one:
cscript //nologo //E:JScript "%~F0" !c!

exit

@end

WScript.CreateObject("sapi.spvoice").Speak(WScript.Arguments(0));

In this case the original VBScript code is so simple that the JScript translation is immediate; just note that the uppercase letters are needed in JScript. However, I am not entirely sure that spvoice execute the same in JScript than in VBScript; you must do a test...

6
  • I get BAT(22, 44) Microsoft JScript compilation error: Expected ';'
    – foxidrive
    Commented Jan 27, 2014 at 7:38
  • Yes, it is caused by the difference between VBScript and JScript syntax. Perhaps this is the right one?: WScript.CreateObject("sapi.spvoice").Speak(WScript.Arguments(0));
    – Aacini
    Commented Jan 27, 2014 at 7:52
  • Yes, that works fine. I edited your answer to use that.
    – foxidrive
    Commented Jan 27, 2014 at 15:09
  • This is actually far better for the lack of multiple files, thank you! Commented Jan 27, 2014 at 21:34
  • And I understand the first if statement, but why does JScript register it as false and how does this make it ignore script inside as batch? Also, is 'cscript' a redirection to @end so that the voice feature can run? Commented Jan 27, 2014 at 21:38
1

Create a script file SpeakNumber.vbs (call it whatever you want).

Put this inside it:

Set args = Wscript.Arguments

WScript.CreateObject("sapi.spvoice").Speak args(0)

Then, change your batch file like this:

@echo off
title 

setlocal enabledelayedexpansion
:a
set /p a=
!d!
for %%G in (%a%) do (set /a b+=1
if !b! neq 1 (set c=!c!-%%G) else (set c=%%G))
START SpeakNumber.vbs !c!

exit
0

This is my batch script:

@echo off title setlocal enabledelayedexpansion :a set /p a= !d! 
for %%G in (%a%) 
  do (set /a b+=1 
     if !b! neq 1 
         (set c=!c!-%%G) 
    else 
        (set c=%%G)) 
    echo wscript.createobject("sapi.spvoice").speak "!c!">a.vbs 
    start a.vbs 
exit
-1

You dont have use a hybrid as you can embed the vbs file inside the batch file.

@echo off
set /p "Voice= Enter what you would like to say : "
echo dim speechobject >> sapi.vbs
echo set speechobject=createobject("sapi.spvoice") >> sapi.vbs
echo speechobject.speak "%Voice%" >> sapi.vbs
start sapi.vbs
timeout /t 1 /nobreak
del sapi.vbs

Not the answer you're looking for? Browse other questions tagged or ask your own question.