Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
xhai
- Posts: 39
- Joined: 13 Jun 2013 09:33
#1
Post
by xhai » 24 Feb 2014 19:45
Code: Select all
@echo off
:: Ghost typer
setlocal enableextensions enabledelayedexpansion
set lines=6
set "line1=Twinkle twinkle little star"
set "line2=How I wonder what you are"
set "line3=Up above the world so high"
set "line4=Like a diamond in the sky"
set "line5=Twinkle twinkle little star"
set "line6=How I wonder what you are"
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
for /L %%a in (1,1,%lines%) do set num=0&set "line=!line%%a!"&call :type
pause>nul
goto :EOF
:type
set "letter=!line:~%num%,1!"
set "delay=%random%%random%%random%%random%%random%%random%%random%"
set "delay=%delay:~-6%"
if not "%letter%"=="" set /p "=a%bs%%letter%" <nul
:: adjust the 3 in the line below: higher is faster typing speed
for /L %%b in (1,3,%delay%) do rem
if "%letter%"=="" echo.&goto :EOF
set /a num+=1
goto :type
i found this code and i want to use it that look like a credit section like watching a movie at the end of the show it display the director, staff and so on..
to do
- i wan to extend the line to 20 or more "don't know where to edit"
- make it more faster "don't know where to edit"
- improve the code
thank you
-
Squashman
- Expert
- Posts: 4486
- Joined: 23 Dec 2011 13:59
#2
Post
by Squashman » 24 Feb 2014 19:55
The code explains how to make it go slower or faster. Read the code.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#3
Post
by foxidrive » 24 Feb 2014 20:41
I recognise that!
Add lines to the text block and change
lines=6 to
lines=11 for 11 lines.
set "line7=more text"
set "line8=more text"
set "line9=more text"
set "line10=more text"
set "line11=more text"
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#4
Post
by foxidrive » 24 Feb 2014 21:26
Here is another version - the text is at the top here. Edited to be able to use ! characters.
Code: Select all
@:A Sunday school teacher was discussing the Ten Commandments with her
@:five and six year olds. After explaining the commandment
@:'Honour thy father and thy mother,' she asked
@:'Is there a commandment that teaches us how to treat our brothers
@:and sisters?'
@:
@:Without missing a beat one little boy answered, 'Thou shall not kill!'
@:
@:
@:
@:
@:
@:A father came in the bedroom to find his 14-year-old daughter
@:smoking a cigarette. 'My God! How long have you been smoking?'
@:screams the father.
@:
@:'Since I lost my virginity,' replies the girl.
@:
@:'You lost your VIRGINITY!!! When the hell did this happen?'
@:shrieks the father.
@:
@:'I don't remember,' says the girl. 'I was drunk.'
:: Add text above and start each line with @:
:: Do not use " in the text, replace them with '
@echo off
:: Ghost typer II
:: Adjust speed variable - higher numbers increase typing speed - default=3
set speed=10
for /f "tokens=1,* delims=:@" %%a in ('findstr /n "^@:" "%~f0" ') do (
set "line-%%a=%%b"
if "%%b"=="" set "line-%%a= "
set "numlines=%%a"
)
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
for /L %%a in (1,1,%numlines%) do set num=0&call set "line=%%line-%%a%%"&call :type
pause>nul
goto :EOF
:type
call set "letter=%%line:~%num%,1%%"
set "delay=%random%%random%%random%%random%%random%%random%%random%"
set "delay=%delay:~-6%"
if not "%letter%"=="" set /p "=a%bs%%letter%" <nul
for /L %%b in (1,%speed%,%delay%) do rem
if "%letter%"=="" echo.&goto :EOF
set /a num+=1
goto :type
-
penpen
- Expert
- Posts: 2009
- Joined: 23 Jun 2013 06:15
- Location: Germany
#5
Post
by penpen » 25 Feb 2014 01:42
The original code contains a bug, where you could create "wrong octals" (for example 099999) as a random variable.
So you should replace this line
by such a line
Code: Select all
set /A "delay=1%delay:~-6%-1000000"
to avoid trailing zeroes.
penpen
Edit: I've added a "/A", thx to einstein1969.
Last edited by
penpen on 25 Feb 2014 09:02, edited 1 time in total.
-
einstein1969
- Expert
- Posts: 961
- Joined: 15 Jun 2012 13:16
- Location: Italy, Rome
#6
Post
by einstein1969 » 25 Feb 2014 05:24
penpen wrote:The original code contains a bug, where you could create "wrong octals" (for example 099999) as a random variable.
So you should replace this line
by such a line
to avoid trailing zeroes.
penpen
Hi penpen, do you mean Set /A ? I don't probed the code.
einstein1969
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#7
Post
by foxidrive » 25 Feb 2014 06:03
penpen wrote:The original code contains a bug, where you could create "wrong octals" (for example 099999) as a random variable.
So you should replace this line
by such a line
to avoid trailing zeroes.
penpen
I didn't know the /L for command counted in octal.
In this case it's not a problem as the effect here will merely give a zero delay for that character, but thanks for the tip, penpen.
-
xhai
- Posts: 39
- Joined: 13 Jun 2013 09:33
#8
Post
by xhai » 26 Feb 2014 20:55
thanks you to all of you...
but i have error like this.. 'echo.' is not recognized as an internal or external commands, operable program or batch file.
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#9
Post
by foxidrive » 27 Feb 2014 01:24
Look in the same folder for a file that is called echo
-
jeb
- Expert
- Posts: 1055
- Joined: 30 Aug 2007 08:05
- Location: Germany, Bochum
#10
Post
by jeb » 27 Feb 2014 01:48
xhai wrote:but i have error like this.. 'echo.' is not recognized as an internal or external commands, operable program or batch file.
And change all
to
This will avoid this error and is ~15times faster
jeb
-
justrelaxasc
- Posts: 1
- Joined: 14 Jan 2016 12:45
#11
Post
by justrelaxasc » 14 Jan 2016 12:49
Code: Select all
@echo off
:: Ghost typer
setlocal enableextensions enabledelayedexpansion
set lines=6
set "line1=Twinkle twinkle little star"
set "line2=How I wonder what you are"
set "line3=Up above the world so high"
set "line4=Like a diamond in the sky"
set "line5=Twinkle twinkle little star"
set "line6=How I wonder what you are"
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
for /L %%a in (1,1,%lines%) do set num=0&set "line=!line%%a!"&call :type
pause>nul
goto :EOF
:type
set "letter=!line:~%num%,1!"
set "delay=%random%%random%%random%%random%%random%%random%%random%"
set "delay=%delay:~-6%"
if not "%letter%"=="" set /p "=a%bs%%letter%" <nul
:: adjust the 3 in the line below: higher is faster typing speed
for /L %%b in (1,3,%delay%) do rem
if "%letter%"=="" echo.&goto :EOF
set /a num+=1
goto :type
I'm quiet new to batch coding and I'm quiet interested in this specific code if someone could explain me what each of this lines do and mean, it'd be much appreachiated, thanks in advance.
-
ShadowThief
- Expert
- Posts: 1166
- Joined: 06 Sep 2013 21:28
- Location: Virginia, United States
#12
Post
by ShadowThief » 14 Jan 2016 17:29
I love doing stuff like this.
Code: Select all
:: Two colons is a comment. If any of the for loops had spanned multiple lines,
:: I would be using REM instead because the two colons breaks for loops.
::
:: Comments explaining what a line does will go directly above that line.
:: Prevents the commands from being displayed to the command prompt before they are performed
@echo off
:: setlocal tells the command prompt that the variables we're using don't need to
:: exist outside of this script.
:: enableextensions activates command extensions, although this is already on by default
:: unless you turned it off for some reason - but you probably didn't.
:: Ordinarily, variables get replaced with their value when the script is parsed.
:: enabledelayedexpansion allows the variables that are called using the !variable!
:: notation to not be replaced until the script runs. We also use it to build variables out
:: of other variables, like we will with line%%a later.
setlocal enableextensions enabledelayedexpansion
:: Sets the variable "lines" to the value of 6
:: We're going to use this to tell the program to expect six lines to be printed
set lines=6
:: Sets the contents of the lines that we will print out
set "line1=Twinkle twinkle little star"
set "line2=How I wonder what you are"
set "line3=Up above the world so high"
set "line4=Like a diamond in the sky"
set "line5=Twinkle twinkle little star"
set "line6=How I wonder what you are"
:: Creates a backspace character and stores it in a variable called BS
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
:: Counts from 1 to the value of %lines% (six in this case), each time
:: setting num to 0, setting the %line% variable to %line1% through
:: %line6% depending on what the value of %%a is, and then calling the
:: :type subroutine
for /L %%a in (1,1,%lines%) do set num=0&set "line=!line%%a!"&call :type
:: Waits for the user to press any key to continue, but doesn't print a message
:: telling the user to press any key to continue
pause>nul
:: Goes to the end of the script, ending processing
goto :EOF
:: The :type subroutine - personally, I would have called it something else since
:: type is a batch command, but the command prompt is smart enough to know that
:: it's a subroutine since there's a colon in front of it.
:type
:: Sets the variable %letter% to the %num%th character in the value of %line%
:: for example, %num% being 4 and %line% being Twinkle twinkle little star
:: will result in %letter% being k, since you start counting string characters at 0
set "letter=!line:~%num%,1!"
:: set %delay% to a string of seven random numbers
:: %random% returns a number between 1 and 32768
:: We're using %random% seven times to ensure that there are at least six digits in %delay%
set "delay=%random%%random%%random%%random%%random%%random%%random%"
:: Remove all but the last six digits of %delay%
set "delay=%delay:~-6%"
:: set /p "=something" <nul is used to display text on the screen without
:: adding a newline character afterwards.
:: if %letter% has a value, display an a, back up a space, and display %letter%
:: If you don't do it like this, spaces won't be printed.
if not "%letter%"=="" set /p "=a%bs%%letter%" <nul
:: adjust the 3 in the line below: higher is faster typing speed
:: Counts from 1 to %delay% by 3s, doing nothing but spending processor cycles
:: This way, you can delay for less than a second, unlike if you use ping or timeout
for /L %%b in (1,3,%delay%) do rem
:: if %letter% is empty, print a newline character and leave the subroutine
:: (goto :EOF in subroutines leaves the subroutine instead of exiting the script)
if "%letter%"=="" echo.&goto :EOF
:: Increment %num% by 1 to get the next character in %line%
set /a num+=1
:: Go back up to the :type label
goto :type
Last edited by
ShadowThief on 17 Jan 2016 03:07, edited 1 time in total.
-
Aacini
- Expert
- Posts: 1914
- Joined: 06 Dec 2011 22:15
- Location: México City, México
-
Contact:
#13
Post
by Aacini » 14 Jan 2016 19:02
Well, what I love is doing stuff like THIS:
Code: Select all
@if (@CodeSection == @Batch) @then
@echo off
for %%a in ("Twinkle twinkle little star"
"HowÿI wonder whatÿyou are"
"Upÿa boveÿthe worldÿso high"
"Likeÿa diamond inÿthe sky"
"Twinkle twinkle little star"
"HowÿI wonder whatÿyou are") do (
CScript //nologo //E:JScript "%~F0" "%%~a"
)
goto :EOF
@end
var sapi = WScript.CreateObject("SAPI.SpVoice"),
word = WScript.Arguments(0).split(" ");
for ( var i = 0; i < word.length; i++ ) {
WScript.Stdout.Write(word[i]+" ");
sapi.Speak(word[i]);
}
WScript.Stdout.WriteLine();
Antonio
-
foxidrive
- Expert
- Posts: 6031
- Joined: 10 Feb 2012 02:20
#15
Post
by foxidrive » 14 Jan 2016 21:13
Nice work ShadowThief.
I would rephrase this passage though, as it seems a little odd and inaccurate.
Code: Select all
:: Ordinarily, variables get replaced with their value before the batch script is run.
:: enabledelayedexpansion allows the variables that are called using the !variable!
:: notation to remain treated like variables.
That's good fun too Antonio! It sounds a bit like a retarded Dr Seuss
penpen wrote:The original code contains a bug, where you could create "wrong octals" (for example 099999) as a random variable.
Edit: I've added a "/A", thx to einstein1969.
I didn't see your edit penpen because I'd already read your post, and I'm not sure if I removed a
/A or what? I don't see one there now, and that issue isn't a problem for it atm - as I mentioned in reply to your post.