Batch Coding Tutorial II

by Batchcc in Circuits > Microsoft

3056 Views, 28 Favorites, 0 Comments

Batch Coding Tutorial II

8545.png
This is step two in batch more advanced functions for cooler codes see tutorial 1 below

https://www.instructables.com/id/Batch-Codeing-Tutorial-I/

In this instructable I will go over the attrib md and && commands as well as going more depth into if and set by viewing more codes.

Combine Lines With &&

ZXwGH.png
The && command is the equivalent of the word and in batch view the top of each code

@echo off
Cls

There is nothing wrong with that but to make it one line do this


@echo off && cls


Neither code is incorrect and I prefer the first one but if you want one line that's how to do it.

Del and Md

Del - deletes a specified file

Del "text file.txt"

Bath coding doesn't allow for spaces in some places like file names so if the name has quotes around it the space will be registered.

Del C:\Users\User\trash\*.*

The del command can delete a file path so if your user name is user this command will delete all the files in a folder you have named trash is your user folder.
*.* deletes all files *.bat specifes only batch files

Attrib - hide and unhide files
Attrib works the same way as del but before the file name add + - s h
+s makes the file a system file
+h make the file hidden
+s +h makes the file both
And -s or -h removes those attributes.

Set and Math

Set /a does math

@echo off
Cls
Echo Enter number 1
set /a x=
Echo Enter sign + - / *
set /p s=
Echo Enter second number
set /a y=
Echo %x% %s% %y%
Pause

This code should be able to preform basic math not decimals though rather that pause you can use the goto command so it doesn't close when it solves it and just remember that % signs are needed around vars.

If More Advanced

So first let's look at this code


@echo off
color 0e
title Guessing Game by seJma
set /a guessnum=0
set /a answer=%RANDOM%
set variable1=surf33
echo -------------------------------------------------
echo Welcome to the Guessing Game!
echo.
echo Try and Guess my Number!
echo -------------------------------------------------
echo.
:top
echo.
set /p guess=
echo.
if %guess% GTR %answer% ECHO Lower!
if %guess% LSS %answer% ECHO Higher!
if %guess%==%answer% GOTO EQUAL
set /a guessnum=%guessnum% +1
if %guess%==%variable1% ECHO Found the backdoor hey?, the answer is: %answer%
goto top
:equal
echo Congratulations, You guessed right!!!
echo.
echo It took you %guessnum% guesses.
echo.
pause

First a note to change batch colors type color /? Into cmd.exe to get the color code like color 1
Next this code is a guessing game if you guess GTR or LSS the code tells you using the if command.

Now look at another example
@echo off
title Folder Password v1.5
color 0a
set /a tries=3
set password=***ENTER YOUR PASSWORD HERE***
:top
echo.
echo ----------------------------------------------
echo.
echo Folder Password
echo.
echo ----------------------------------------------
echo You have %tries% attempts left.
echo Enter password
echo ----------------------------------------------
set /p pass=
if %pass%==%password% (
goto correct
)
set /a tries=%tries -1
if %tries%==0 (
goto penalty
)
cls
goto top
:penalty
echo Sorry, too many incorrect passwords, initiating shutdown.
start shutdown -s -f -t 35 -c "SHUTDOWN INITIATED"
pause
exit
:correct
cls
echo ----------------------------------------------
echo Password Accepted!
echo.
Pause

Using set /a and if the code will tell you how many times you have left to enter the correct password to change it modify

set /a tries=3

To the number you would like each time the password is incorrect the code will set %tries% to one less than before.

Start Advanced

bat-file.png
The start command opens files or web pages to open a file (same as del) type start filename.bat
Now to edit the batch file with notepad type this start notepad.exe filename.bat
Finally to open a web page look at this code

@echo off
color 2f
title Site Selector by seJma
:top
echo ***************************************************************
echo.
echo Site Selector
echo.
echo ***************************************************************
echo.
echo Key: [1] Google - Search Engine
echo [2] Hotmail - Mail Server
echo [3] Yahoo - Search Engine/Mail Server
echo [4] Facebook - Social Networking
echo [5] Myspace - Social Networking
echo [6] CNN - News
echo [7] Weather - Weather
echo [8] WikiHow - A How-To Website
echo [9] Instructables - A How-To Website
echo [10] YouTube - Online Videos
echo [11] Answers - Online Encyclopedia
echo [12] Wikipedia - Online Encyclopedia
echo.
echo [e] Exit
echo.
echo ***************************************************************
echo Enter the number of the website which you would like to go to:
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==1 start www.google.com
if %udefine%==2 start www.hotmail.com
if %udefine%==3 start www.yahoo.com
if %udefine%==4 start www.facebook.com
if %udefine%==5 start www.myspace.com
if %udefine%==6 start www.cnn.com
if %udefine%==7 start www.weather.com
if %udefine%==7 start www.wikihow.com
if %udefine%==9 start www.instructables.com
if %udefine%==10 start www.youtube.com
if %udefine%==11 start www.answers.com
if %udefine%==12 start www.wikipedia.com
if %udefine%==e goto exit

cls
echo ***************************************************************
echo.
echo Thank You for using Site Selector by seJma
echo.
echo ***************************************************************
echo Type [e] to exit or [b] to go back and select another site.
echo.
set /p udefine=
echo.
echo ***************************************************************
if %udefine%==b goto top
if %udefine%==e goto exit
:exit
cls
echo ***************************************************************
echo.
echo Thank You for using Site Selector by SEjMA
echo.
echo ***************************************************************
pause
exit

White sejama's batch code is OK you can use the set command watch

@echo off
Cls
Echo Enter the name of a website to open
Set/p site=
Start %site%