batch-filevbscripttext-to-speech

Hybrid Batch-VBS TTS script not working


I tried to make a hybrid Batch-VBS script out of a VBS script that i already made. It would give a inputBox, and use the results to sapi.spvoice.Speak it. I tried to make it into a batch script (below), but it doesn't work, and tts.vbs ends up containing only sapi.Speak message.


Batch Script:

@echo off
:start
cls
echo Batch Text-To-Speech
echo By SudDaBuilder
:: echo Fixed by %YourNameHere% ::
set /p msg=What do you want your PC to say? 
set /p vce=Choose a Voice (0 - Male, 1 - Female) 
pause
cls
echo Dim message, sapi, voice > tts.vbs
echo message=%msg% > tts.vbs
echo voice=%vce% > tts.vbs
echo Set sapi=CreateObject("sapi.spvoice") > tts.vbs
echo with sapi > tts.vbs
echo Set .voice = .getvoices.item(voice) > tts.vbs
echo .Volume = 100 > tts.vbs
echo end with > tts.vbs
echo sapi.Speak message > tts.vbs
cscript tts.vbs
cls
pause

:again
cls
set /p retry=Again? (y/n) 
if %retry% == y goto start
goto end

:end
echo See you soon!
ping localhost > nul

Solution

  • The `>` needs to become `>>` from the twelfth line onwards.

    Why?
    The > character overwrites the contents of the file and adds the specifie content, so you only end up getting the last line. Whereas, the >> character(s) adds on the specified line to the end of the file's contents.
    You also need to enclose the msg and vce variables in quotations.


    Fixed script:

    @echo off
    :start
    cls
    echo Batch Text-To-Speech
    echo By SudDaBuilder
    :: echo Fixed by SO Suda ::
    set /p msg=What do you want your PC to say? 
    set /p vce=Choose a Voice (0 - Male, 1 - Female) 
    pause
    cls
    echo Dim message, sapi, voice > tts.vbs
    :: THIS IS THE TWELFTH LINE ::
    echo message="%msg%" >> tts.vbs
    echo voice="%vce%" >> tts.vbs
    echo Set sapi=CreateObject("sapi.spvoice") >> tts.vbs
    echo with sapi >> tts.vbs
    echo Set .voice = .getvoices.item(voice) >> tts.vbs
    echo .Volume = 100 >> tts.vbs
    echo end with >> tts.vbs
    echo sapi.Speak message >> tts.vbs
    cscript //NoLogo tts.vbs
    cls
    pause
    
    :again
    cls
    :: ADDED A DELETE FOR THE tts.vbs FILE::
    del tts.vbs
    set /p retry=Again? (y/n) 
    if %retry% == y goto start
    goto end
    
    :end
    echo See you soon!
    ping localhost >> nul