0

I'm trying to create a batch script which will create another batch script. The problem is that one of the lines doesn't add and instead redirects to another file when it encounters the redirect that I want to create.

@echo off
:: Pulls name variable from text file
set /p UN=<name.txt
::Begins creating sec.bat
echo @echo off >sec.bat
:: Creates a vbs script to say whatever the %UN% variable is previously set as to the user
echo echo set speech = wscript.CreateObject("SAPI.spVoice") >>temp.vbs >>sec.bat
echo set text=%UN%! >>sec.bat
:: This is where my error is happening. I want it to write the entire line
:: "echo speech.speak "%text%" >> temp.vbs" to sec.bat but it won't. It
:: instead writes the line in a file called temp.vbs
echo echo speech.speak "%text%" >> temp.vbs >>sec.bat
echo start temp.vbs >>sec.bat
echo timeout /t 1 >>nul >>sec.bat
echo del temp.vbs >>sec.bat

I've tried putting the lines individually in quotations but that causes it to write the quotations to the bat file as well and it does not work as intended.

Daniel S
  • 13
  • 3
  • 3
    Possible duplicate of [what does symbol ^ means in Batch script](http://stackoverflow.com/questions/20342828/what-does-symbol-means-in-batch-script) or any other question and answer found with [\[batch-file\] redirect echo create](http://stackoverflow.com/search?q=%5Bbatch-file%5D+redirect+echo+create) – Mofi Mar 23 '17 at 14:48

1 Answers1

0

Escaping >> (^>^>) should be enough:

@echo off
set /p UN=<name.txt
echo @echo off >sec.bat
echo echo set speech = wscript.CreateObject("SAPI.spVoice") ^>^>temp.vbs >>sec.bat
echo set text=%UN%! >>sec.bat
echo echo speech.speak "%text%" ^>^> temp.vbs >>sec.bat
echo start temp.vbs >>sec.bat
echo timeout /t 1 ^>^>nul >>sec.bat
echo del temp.vbs >>sec.bat
MichaelS
  • 5,941
  • 6
  • 31
  • 46
  • Thank you so much! I just have a different issue now, it's printing the redirects properly but it's not adding the variable %text%. The output in sec.bat looks like this: @echo off echo set speech = wscript.CreateObject("SAPI.spVoice") >>temp.vbs set text=Username! echo speech.speak "" >>temp.vbs start temp.vbs timeout /t 1 >nul del temp.vbs I'm guessing it's because the first bat that is creating this tries to print the variable %test% but since it hasn't been set it prints nothing. I tried using the ^ escape but it didn't seem to work. just made the line: ..."speak "^" " – Daniel S Mar 23 '17 at 17:51
  • Nevermind just found out I can do this by using double %. =D Ty for the help. – Daniel S Mar 23 '17 at 17:57