Home > front end >  Batch echo variable outside of the For loop then Call typewriter to write all lines in the variable
Batch echo variable outside of the For loop then Call typewriter to write all lines in the variable

Time:07-27

I need help on how to solve below problem :

Using a batch file, how do I get several lines of text from inside a text file to be typed inside a summoned cmd.exe window, using typewriter.

Let's say I have a text file named temp.txt with below sentences:

Successful crime is called virtue.
If you are successful, you can expect promotion.
We have made a successful sally.
He was successful in everything he did.
The operation was only partially successful.
I was still striving to be successful.

The problem is, only last line would be grabbed, and then written using the typewriter. Not all the texts inside the file.

I've tried to echo a variable that has been set inside a for loop, but that didnt work as it shows echo is off.

Below is the batch code:

@echo off
title %~nx0
color 0a
setlocal EnableDelayedExpansion
set color_white=%ESC%[37m
set color_black=%ESC%[30m

echo pasteclip= CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text") > temp.vbs
echo WScript.Echo pasteclip >> temp.vbs
cscript //nologo temp.vbs > temp.txt

for /f "Tokens=* Delims=" %%x in (temp.txt) do set SendPaste=%%x

set "text=%SendPaste%"%color_black% 
        
color 09
set Msg="  %text%" 
::Call :menu_[2]
Call :Typewriter %Msg% 

::*********************************************************************
:TypeWriter
Cls
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 150
echo For x = 1 to intTextLen
echo     strTempText = Mid(strText,x,1^)
echo     WScript.StdOut.Write strTempText
echo     WScript.Sleep intPause
echo Next
echo Set Voice=CreateObject("SAPI.SpVoice"^)
echo voice.speak strText
)>%tmp%\%~n0.vbs
@cScript.EXE /noLogo "%tmp%\%~n0.vbs" ^" ^
%empty line%
^
%empty line%
     %~1^"
     
exit /b
::********************************************************************* 

pause 

::most helpful references below has been used inside this batch file
::https://stackoverflow.com/questions/28369536/batch-file-typewriter-effect
::https://stackoverflow.com/a/36448240/9222942 batch-file-typewriter-effect ANSWER
::https://stackoverflow.com/questions/664957/can-i-mask-an-input-text-in-a-bat-file?lq=1
::https://stackoverflow.com/a/63847096/9222942 mask with black
::https://stackoverflow.com/a/24792070/9222942 mask-an-input-text-in-a-bat-file
::https://stackoverflow.com/a/62121081/9222942 how-can-you-get-the-clipboard-contents-with-a-windows-command
::https://stackoverflow.com/a/21205537/9222942 automatically-accept-user-input-windows-batch
::https://stackoverflow.com/a/16116676/9222942 how-to-read-file-contents-into-a-variable-in-a-batch-file
::https://stackoverflow.com/a/41926276/9222942 read-a-file-line-by-line-and-save-it-in-variable-batch-file

All the text should be displayed on the cmd screen using a guide from here, but when I tried to call the variable to start the typing, it would fail.

Let's say I use the modified below batch:

@echo off
title %~nx0
color 0a
setlocal EnableDelayedExpansion
set color_white=%ESC%[37m
set color_black=%ESC%[30m

echo pasteclip= CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text") > temp.vbs
echo WScript.Echo pasteclip >> temp.vbs
cscript //nologo temp.vbs > temp.txt

set "File2Read=temp.txt"
rem This will read a file into an array of variables and populate it 
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%File2Read%"') do (
    set /a count =1
    set "Line[!count!]=%%a"
)
rem Display array elements
For /L %%i in (1,1,%Count%) do (
    echo !Line[%%i]!
    set "text=!Line[%%i]!"%color_black% 
)
        
color 09
set Msg="  %text%" 
::Call :menu_[2]
Call :Typewriter %Msg% 

::*********************************************************************
:TypeWriter
Cls
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 150
echo For x = 1 to intTextLen
echo     strTempText = Mid(strText,x,1^)
echo     WScript.StdOut.Write strTempText
echo     WScript.Sleep intPause
echo Next
echo Set Voice=CreateObject("SAPI.SpVoice"^)
echo voice.speak strText
)>%tmp%\%~n0.vbs
@cScript.EXE /noLogo "%tmp%\%~n0.vbs" ^" ^
%empty line%
^
%empty line%
     %~1^"
     
exit /b
::********************************************************************* 

pause 

It will only flash the whole text, but then only type the last sentence using the typewriter.

How to get all the texts to be written?

CodePudding user response:

You should call this Call :Typewriter "!Line[%%i]!" inside the loop for /L


@echo off
title %~nx0
color 0a
echo pasteclip= CreateObject("htmlfile").ParentWindow.ClipboardData.GetData("text") > temp.vbs
echo WScript.Echo pasteclip >> temp.vbs
cscript //nologo temp.vbs > temp.txt

set "File2Read=temp.txt"
rem This will read a file into an array of variables and populate it 
setlocal EnableExtensions EnableDelayedExpansion
for /f "delims=" %%a in ('Type "%File2Read%"') do (
    set /a count =1
    set "Line[!count!]=%%a"
)
rem Display array elements
@For /L %%i in (1,1,%Count%) do (
    Call :Typewriter "!Line[%%i]!"
)
EndLocal & pause & Exit /B
::*********************************************************************
:TypeWriter
Cls
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo intPause = 150
echo For x = 1 to intTextLen
echo     strTempText = Mid(strText,x,1^)
echo     WScript.StdOut.Write strTempText
echo     WScript.Sleep intPause
echo Next
echo Set Voice=CreateObject("SAPI.SpVoice"^)
echo voice.speak strText
)>%tmp%\%~n0.vbs
@cScript.EXE /noLogo "%tmp%\%~n0.vbs" ^" ^
%empty line%
^
%empty line%
     %~1^"
     
exit /b
::*********************************************************************
  • Related