After reading this thread, you will know what fuels the creation of malicious or helpful batch scripts. Batch is VERY simple, it is one of the least complicated scripting languages out there. This is not the end, but a beginning, only scratching the surface of Batch.
Open notepad. Copy and paste the codes required, and save as ".bat" without the quotes.
1. Your First Script
Code:
@echo off
echo Hello World!
pauseYou have successfully created your first script! Now to analyze the code.
@echo off: Prevents the "echo" of commands. For example, if you remove the "@echo off", all the commands will be shown instead of just the "Hello World!" by itself. Try it! Take out the "@echo off".
echo: Writes text to the command interpreter.
pause: awaits user input before executing the next line.
2. Variables
You've heard that term before. A variable is a letter assigned to a number. But in programming, a variable is any set of letters and symbols assigned to any anything you like. To modify variables in batch, the "set" command is your go. Try this script!
Code:
@echo off
echo Enter your first name.
set /p input=
echo %input%
pause.Now to analyze the code.
set /p input: This tells the interpreter to wait for a user input. you can replace the "input" with anything you like, that is the variable.
Look at the "%input%" after the "echo". In batch, variables are defined by adding the "%" to the beginning and the end of the variable. The "%" only applies when the variable has been already defined.
We're not done with variables just yet. There's one little detail we should cover.
Code:
@echo off
set a=Hello World.
echo %a%
pauseThis sets the string "Hello world" to the variable "a". So every time "a" is enclosed with "%", the interpreter will find the defined variable as an output.
3. Dashing Through the Script
The title is more interesting than the actual content in this one. Batch has a command that utilizes "hopping" to one part of a script from the other: the "goto" command. Such scripts need some minor organization.
Try this code!
Code:
@echo off
goto b
:Script
echo Hello World!
pause
:a
goto c
:b
goto j
:k
goto a
:j
goto k
:c
goto ScriptWhat humble organization? Psych! This is really simple. Let's analyze the code!
goto: This tells the command interpreter to move to a labeled line.
Labeled lines: the ":a" and the ":Script" and the others that have a colon added before them. Note that the "goto" command only works if you have your lines labeled.
If you are still confused about the code above.
the first "goto" tells the interpreter to go to b, then the "goto" under the line labeled "b" tells the interpreter to go to j. Then j goes to k, k goes to a, a goes to c, and c goes to Script. And look what's under the ":Script" line, our original code!
Hope that cleared things up.
4. Conditional Statements
Conditional statements can be very complicated to a newbie in programming languages. But luckily, in Batch, conditional statements are only sand compared to the full beaches of C, C++, Python, and other various Programming Languages.
Try this code!
Code:
@echo off
:question
echo What's 1+1?
set /p input=
if '%input%' = '2' goto correct
if not '%input%' = '2' goto false
:correct
cls
echo Good job!
pause
:false
echo You are one dumbass.
echo Try again
echo.
goto questionLet's analyze the code!
if: Tells the interpreter to do a command if certain conditions are met. The main conditional statement. What's the variable being used? "Input"! That's right! The ' are optional but highly recommended.
if not: Tells the interpreter to do a command if certain conditions are NOT met.
echo.: Just makes a blank line.
cls: Clears the screen.
5. File Creation
Yes. File creation. Batch can do that. It is VERY easy. Try this code!
Code:
@echo off
echo texttexttext>File.txt
echo more texttexttext>>File.txtRemember, one ">" is to create the file, and two ">" is to append to the file. To save time, you may want to assign the file that you are creating or appending to to a variable.
6. Grouping
Sometimes, while coding in batch, you might want to have a set of files that you want to apply a command to simultaneously. Instead of going through all those files and making your changes manually, the "for" command does the trick. Try this code!
Code:
@echo off
set a=text.txt
set b=text2.txt
set c=text3.txt
for %%a in (%a%,%b%,%c%) do (
echo texttexttext>%%a
)Confused? Let's analyze the code!
set: Sets the variables a, b, and c, to the files text.txt, text2.txt, and text3.txt in that order.
for: This command can be very confusing, we are just covering the basics of this command. the "%%a" is the variable that we assign to the strings in the parentheses after the "in". The "in" specifies the the string that we are setting the variable "%%a" too. The "do" tells the interpreter to apply a certain set of commands within the parentheses.
There are much more detailed intricacies to the "for" command, but they won't be discussed here.
Thanks for reading! That's it for THIS guide. Now you are well on your way to coding in batch.
