Advanced Batch Coding III

by Batchcc in Circuits > Microsoft

5563 Views, 24 Favorites, 0 Comments

Advanced Batch Coding III

8546.png
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
Tutorial I
Tuttorial II

Echo and Type

hqdefault.jpg
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

Type hello.text


Will display the contents of hello.text this can be used to display pictures of text.

Saved Variables

@echo off
cls
set /p "name=Name: "
echo %name%>name.txt
set /p name=echo %name%
pause

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

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.

------------------------------------------------------------------------------------------------------

Ping - Delay a Command

ping-1.png
Pause waits for you to hit enter but ping allows for you to just wait view this line below

Ping 1.1.1.1 -n 1 -w 100>nul

You must ping an invalid in address ( 1.1.1.1) 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.

Call Vs Start

Start - start a file
Call - call a file ( but here's the difference )

@echo off
Cls
Start hello.bat
Echo hello
Pause

::Hello.bat

@echo off
Cls
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

@echo off
Cls
Call hello
Goto exit
:hello
Echo hello
:exit

( call can call a section in a batch file )

More Batch

I have a collection of batch codes here

https://www.instructables.com/id/Batch-codes/

And I will be publishing tutorial 4 soon with super advanced batch coding!