2

If you have ever seen a hacker movie, you saw code appear slowly character by character as if someone was typing them. I want to do this in batch. I found some code that did a similar thing, but with hole lines instead.

The code can be found at: How to have multiple colors in a Windows batch file? dbenham's answer.

Ive tried using disableDelayedExpansion since the command "sounded" like it was going to do what i wanted, but i didnt get anywere

Thx for any help in advance.

Also, kinda example of what i want to do- text appearing slowly gif

k9-primetime
  • 73
  • 1
  • 10

4 Answers4

4

Depending upon your string content, something as simple as this may suit you.

@Echo Off

Set "STRING=Try this, unfortunately it is untested!"

For /F %%A In ('"Prompt $H&For %%B In (1) Do Rem"') Do Set "BS=%%A"

For /F Delims^=^ EOL^= %%A In ('"(CMD/U/CEcho=%STRING%)|Find /V """'
) Do Set/P "=a%BS%%%A"<Nul & PathPing 127.0.0.1 -n -q 1 -p 100 1>Nul

You can change the speed of letter display by adjusting the number 100 as necessary. Perhaps try it a 200 for slower typing, and at 50 for faster!

Compo
  • 36,585
  • 5
  • 27
  • 39
  • why the second for loop is so compressed? it just makes it hard for reading. – npocmaka Jan 19 '19 at 18:12
  • it will print the `!` unlike my solution but will have issues with conditional execution and redirection operators like `&&` , `&` , `|` , `>` , `<` and `||` – npocmaka Jan 19 '19 at 18:17
  • 2
    @npocmaka, I compressed it because I wanted it to match the length of the line beneath it, _my OCD takes precedence over someone else's ease of reading_. I'm aware of the drawbacks of the methods used, hence the reason I was clear that it was string dependent. – Compo Jan 19 '19 at 19:06
  • Thank you for your answer :) – k9-primetime Jan 20 '19 at 18:33
0

try this:

@echo off
:typewriter
setlocal enableDelayedExpansion
set "string=%~1"
call ::strlen "%string%#" len
if "%~2" neq "" (
    set /a _timeout=%~2
) else (
    set _timeout=500
)

set "carret=^"
for /l %%a in (0,1,%len%) do (

    set "letter=!string:~%%a,1!"


    for /f %%# in (">" "<" "|" "&") do (
        if "!prev!" equ "%%#" do set "prev=^^%%#"
    )

    if "!letter!" equ " " (
        set "suffix=!suffix! "
    ) 


    ping 192.0.2.0 -n 1 -w %_timeout% 1>nul 2>&1
    if "!prev!" neq " " (
        break|set /p=!carret!!prev!!suffix!
        set "suffix="
    )
    set "prev=!letter!"
)

endlocal 
exit /b 0 %errorlevel%

:strlen
Setlocal EnableDelayedExpansion
:: strLen String [RtnVar]
::             -- String  The string to be measured, surround in quotes if it contains spaces.
::             -- RtnVar  An optional variable to be used to return the string length.
Set "s=#%~1"
Set "len=0"
For %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
  if "!s:~%%N,1!" neq "" (
    set /a "len+=%%N"
    set "s=!s:~%%N!"
  )
)
Endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
Exit /b

it accepts two arguments - the string you want to type slowly ,and the timeout in milliseconds between each letter. Example (if you call it typewriter.bat):

call typewriter.bat "hello world" 200

just mind that the exclamation marks wont be printed because of the delayed expansion. (but you can use special symbols like >,|,&,<)

For further reference check these: strlen , substring , sleep in milliseconds

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

Well for whatever reason the 'quick reply' field would not allow the use of the code block (inline code in backticks) so I am re-posting due to no longer being able to edit my previous message.

Set "STRING=Text you would like to be typed here..."
For /F %%A In ('"Prompt $H&For %%B In (1) Do Rem"') Do Set "BS=%%A"
For /F Delims^=^ EOL^= %%A In ('"(CMD/U/CEcho=%STRING%)|Find /V """'
) Do Set/P "=a%BS%%%A"<Nul & PathPing 127.0.0.1 -n -q 1 -p 50 1>Nul
Bri
  • 59
  • 6
-2

Maybe this is more to your liking:

REM This question was asked here : https://stackoverflow.com/questions/65489660/cmd-typewriter-effect-with-beep
REM Here is an update in order to make a beep on every letter typed !
Title Typewriter with speaking voice by Hackoo updated version Happy New Year 2021
Color 0A & Mode con cols=75 lines=3
set Msg="       Hello this is a Typewriter !"
Call :Typewriter %Msg%
set Msg="   I want to say Hello for everybody on StackOverflow !"
Call :Typewriter %Msg%
Set Msg="       Happy New Year 2021 !"
Call :Typewriter %Msg%
pause>nul
Exit /b
::---------------------------------------------------------------------
:TypeWriter
echo(
(
echo strText=wscript.arguments(0^)
echo intTextLen = Len(strText^)
echo Set WS = CreateObject("wscript.shell"^)
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     WScript.StdOut.Write Chr(7^)
echo Next
echo Set Voice=CreateObject("SAPI.SpVoice"^)
echo voice.speak strText
)>%tmp%\%~n0.vbs
@cscript //noLogo "%tmp%\%~n0.vbs" "%~1"
exit /b
::--------------------------------------------------------------------- 
Bri
  • 59
  • 6
  • If you have a new question, please ask it by clicking the [Ask Question](https://stackoverflow.com/questions/ask) button. Include a link to this question if it helps provide context. - [From Review](/review/late-answers/30247741) – Mahdi Zarei Nov 04 '21 at 10:00
  • Or another 'simpler' version: ` Set "STRING=Text you would like to be typed here..." For /F %%A In ('"Prompt $H&For %%B In (1) Do Rem"') Do Set "BS=%%A" For /F Delims^=^ EOL^= %%A In ('"(CMD/U/CEcho=%STRING%)|Find /V """' ) Do Set/P "=a%BS%%%A"Nul ` – Bri Nov 05 '21 at 18:33