-1

I try to make text engine in cmd. So I use this code:

@echo off
cls
echo h
cls
echo he
cls 
echo hel
cls
echo hell
cls
echo hello

But when I run the file I can't see any text. So I used another code:

@echo off
cls
echo h
goto :2
:2
cls
echo he
goto :3
:3
cls 
echo hel
goto :4
:4
cls
echo hell
goto :5
:5
cls
echo hello

When I first open the file I only see the he. After that I see no text. What did I did wrong? I want to know why it has error and how to fix it to make text engine.

Compo
  • 36,585
  • 5
  • 27
  • 39
spidychoi
  • 1
  • 1
  • Well, you will get `hello` for both variants (though the second one makes absolutely no sense). For the first variant, place `timeout /T 1 > nul` between `echo` and `cls` for the text build-up not to occur too fast for your eyes… – aschipfl Nov 02 '20 at 12:38
  • [possible duplicate](https://stackoverflow.com/questions/28369536/batch-file-typewriter-effect)? – Stephan Nov 02 '20 at 15:53

1 Answers1

0

Here's an example of a which doesn't do what your example is demonstrating, but does something I think you may prefer instead:

@Set "TextString=This is my text string!"
@For /F %%G In ('"Prompt $H&For %%H In (1) Do Rem"') Do @Set "BS=%%G"
@(For /F Delims^=^ EOL^= %%G In ('(%SystemRoot%\System32\cmd.exe /D /S ^
 /U /V /C "Echo=!TextString!"^) ^| %SystemRoot%\System32\find.exe /V ""'
) Do @Set /P "=.%BS%%%G" 0< NUL & %SystemRoot%\System32\PATHPING.EXE ^
 127.0.0.1 -n -q 1 -p 150 1> NUL) & Echo=
@Pause

You simply place your required text to be printed, into the first line (between the = and closing "), and double-click it to run and view. If the intent is to run the script directly from , you do not need the last line, @Pause, and can safely remove it.

As a result of a comment, I have decided to present the code in a slightly different way, so that it can be reused for different strings.

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion
For /F %%G In ('"Prompt $H&For %%H In (1) Do Rem"') Do Set "BS=%%G"

Set "TextString=This is my text string!"
Call :GhostTypeIt

Set "TextString=This is "another" string."
Call :GhostTypeIt

Set "TextString=This & That are <words>."
Call :GhostTypeIt

Pause
Exit /B

:GhostTypeIt
(For /F Delims^=^ EOL^= %%G In ('(%SystemRoot%\System32\cmd.exe /D /S ^
 /U /V /C "Echo=!TextString!"^) ^| %SystemRoot%\System32\find.exe /V ""'
) Do Set /P "=.%BS%%%G" 0< NUL & %SystemRoot%\System32\PATHPING.EXE ^
 127.0.0.1 -n -q 1 -p 150 1> NUL) & Echo=
GoTo :EOF
Compo
  • 36,585
  • 5
  • 27
  • 39
  • You can also 'slow down', or 'speed up', each character's print speed, by adjusting the `-p` number, _(currently `150` milliseconds)_, on the penultimate line. – Compo Nov 02 '20 at 18:52
  • I don't think the OP is looking for a replacement solution. Plus... I'm guessing they're just staring out and I really doubt they are going to understand your far more advanced example. – David Makogon Nov 03 '20 at 12:21
  • Thanks for your feedback @DavidMakogon, although I'm not sure that you're any more capable of determining the OP's intent, than any other person. As for understanding, I don't think there's anything to understand, just changing the value content of `TextString` each time they wish to use the technique, is all that's required. Put the second `for` loop into a labelled section, then `Call`ing that label. I've put an example of that methodology into my answer above. – Compo Nov 03 '20 at 12:56
  • Thank you! This really helped me! – spidychoi Nov 11 '20 at 11:22