1

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

2 Answers2

2

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
  • 30,301
  • 4
  • 20
  • 32
  • 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 `&&` , `&` , `|` , `>` , ` – 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
  • 51,748
  • 17
  • 123
  • 166