Batch File: Typewriter Effect

The ping solution in the comments above is a worthwhile solution for computers with installed network interfaces (as almost all have).

@echo off
setlocal

for %%i in (h e l l o o o o o o o o o o o o o o) do (
   set /p "=%%i"<nul
   ping 169.254.0.0 -n 1 -w 500 >nul
)
echo;
goto :EOF

However, it appears that the minimum wait time recognized with this method is 500ms. If you change the 500 to a lower value, you still pause a half second between letters. If you want finer control, or if your computer has no network interface, you’ll have to borrow from another runtime environment — JScript, for example.

@if (@CodeSection == @Batch) @then

@echo off
setlocal

for %%i in (h e l l o o o o o o o o o o o o o o) do (
   set /p "=%%i"<nul
   cscript /nologo /e:JScript "%~f0"
)
echo;

goto :EOF
rem // end batch portion

@end
// begin JScript chimera
WSH.Sleep(Math.random() * 250 + 100);

Note on the choice of IP to ping: For the -w switch to work as intended, the IP you ping must result in “Request timed out”. You can use a non-existent LAN IP such as a 10.x.x.x or 192.168.x.x. But for widespread deployment, if you can’t be certain that those ranges are unused, a link local IP in the range of 169.254 should work just fine for this purpose. Please do not use an IP in historical bogon space like 1.1.1.1 or 1.2.3.4. Just because such addresses don’t reply doesn’t mean your packets aren’t adding to network congestion somewhere.

Eventually as IPv4 addresses draw ever nearer to complete exhaustion, people need to be more conscientious of polluting the Internet with bogus traffic. It could be that 1.1.1.1 and 1.2.3.4 will never be useful to anyone because they are so often abused by casual scripters. But that’s no reason to add to the mistreatment of those addresses. See this page for further reading, and please, save the bogons.


Jack.bat

Just to see how far I could take the typewriter effect, I wrote a script that outputs text similar to the X screensaver “Jack”. It outputs the same line over and over, and randomly introduces typographical errors. Run it and you’ll be mesmerized, rooting for the script to complete a line without any typos.

@if (@CodeSection == @Batch) @then

@echo off
setlocal

cls
color 70

call :split chars "All work and no play makes Jack a dull boy."

:begin
for %%i in (%chars%) do call :type "%%~i"
echo;
goto begin

:split <var_to_set> <str>
setlocal enabledelayedexpansion
set "line="
set "str=%~2"
for /L %%I in (0,1,43) do set line=!line! "!str:~%%I,1!"
endlocal & set %~1=%line%
goto :EOF

:type <char>
cscript /nologo /e:JScript "%~f0" "%~1"
goto :EOF

@end
// end batch / begin JScript chimera
function pause() { WSH.Sleep(Math.random() * 250 + 100); }
function odds(num) { return !(Math.round(Math.random() * num) % num) }
function backspace() { WSH.StdOut.Write(String.fromCharCode(8)); }

pause();

if (odds(15)) {
    WSH.StdOut.Write(String.fromCharCode(Math.round(Math.random() * 95 + 32)));
    pause();
    if (!odds(20)) {
        backspace();
        pause();
    }
}

if (odds(300)) WSH.Echo('');
if (!odds(400)) WSH.StdOut.Write(WSH.Arguments(0));

Leave a Comment