Introduction: Advanced Batch Coding III
This is super advanced batch coding; I will discuss echo and type for ascii graphics, I will discuss more if statements and I will discuss how to add delays in batch files
Step 1: Echo and Type
We have already gone over the echo command but this is more advanced.
Echo. "This texted will be typed in a textfile"> hello.txt
Echo. "This will be line two">> hello.text
Echo. Three four five and so on">> hello.text
Add one ">" to make it the first line and 2 ">>" to make it on the next blank line.
Type types text into the cmd window and can be used to display text graphics
Will display the contents of hello.text this can be used to display pictures of text.
Step 2: Saved Variables
set /p "name=Name: "
set /p name=echo %name%
This code asks the user to enter their name and sets it as %name%
The code then as seen in step one sends %name% to name.text
The code then calls name.text and searches for the variable name% and displays it. This can be used to store variable without making the user type them in every time
Step 3: If Statemnts III
We have gone over the if command twice but there is more to learn about the first is if /I.
The if /I command makes the letters A and a be interpreted the same way for example
If %word% == yes echo yes
If %word% == Yes echo yes
Can be changed to
If /I %word% == yes echo yes
This spares you a second line for the capital yes.
Step 4: Ping - Delay a Command
Pause waits for you to hit enter but ping allows for you to just wait view this line below
Ping 22.214.171.124 -n 1 -w 100>nul
You must ping an invalid in address ( 126.96.36.199) then the 100 will delay the batch file 1 second ( delay time is in miliseconds) and nul hides it the user also has no way to bypass ping they must wait the specified time.
Sleep - while I prefer ping sleep should still do the same thing ( sleep 1) measured in seconds.
Step 5: Call Vs Start
Start - start a file
Call - call a file ( but here's the difference )
Echo good bye
The first batch file will open hello.bat in the same window the problem is because hello.bat ends the first batch file will not continue ( you won't see the word hello ) to fix this problem use call
The call command will run hello.bat in the same windows but when hello.bat ends the first batch file will resume ( replace start with call ). So is start useless? No, start can be used to open a web page ( start google.com ) you can open a file such as a word document and you can edit a batch file using start notepad hello.bat ( open with notepad ). What else can call do? This
( call can call a section in a batch file )
Step 6: More Batch
I have a collection of batch codes here
And I will be publishing tutorial 4 soon with super advanced batch coding!
We have a be nice policy.
Please be positive and constructive.
You can also use "ping localhost -n 1 >nul". This is not exactly 1 second, but is close to it and can save you time.