Login Register


Batch. The Ultimate Tutorial. filter_list
Author
Message
Batch. The Ultimate Tutorial. #1
Hi! 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.

[spoiler=1. Your First Script]

Code:
@echo off echo Hello World! pause

You 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.

[/spoiler]

[spoiler=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% pause

This 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.

[/spoiler]

[spoiler=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 Script

What 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.

Spoiler: 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.




Anyway, moving on!

[/spoiler]

[spoiler=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 question

Let'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.

[/spoiler]

[spoiler=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.txt

Remember, 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.

[/spoiler]

[spoiler=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.

[/spoiler]



Thanks for reading! That's it for THIS guide. Now you are well on your way to coding in batch. Remember, if you need help with anything batch related, PM me.


If you like this guide, thank me by adding some rep to my profile if not too vigorous the task.

PS: Click here for my batch "virus" generator to study the codes.[/color]
[Image: nNICR.jpg]
Creds to bluedog

For a list of my contributions (includes tutorials). Click here.

If you'd like to apply for Heat, please click here.

Reply

RE: Batch. The Ultimate Newbie Guide. #2
Nice threads mate.
Thanks for ur efforts.
[Image: arEUm.jpg]
Writing a TUT may take 1 or 2 days,
But adding a +rep or saying thanks takes 1 or 2 minutes.


Reply

RE: Batch. The Ultimate Newbie Guide. #3
(07-05-2011, 07:16 PM)∑√ıŁ Wrote: Nice threads mate.
Thanks for ur efforts.

Thanks bro! Glad you like it.
[Image: nNICR.jpg]
Creds to bluedog

For a list of my contributions (includes tutorials). Click here.

If you'd like to apply for Heat, please click here.

Reply

RE: Batch. The Ultimate Newbie Guide. #4
hmm nice game with batch file Biggrin
[Image: 262s21x.jpg]

Reply

RE: Batch. The Ultimate Tutorial. #5
Code:
for %%a in (%a%,%b%,%c%) do ( echo texttexttext>%%a )

so %%a is not refer to 'a' variable we've set up? and the contain of %%a is defined within 'do' block?

Quote:The "in" specifies the the string that we are setting the variable "%%a" too

what's that supposed to mean?
[Image: ap8g35.jpg]

Reply

RE: Batch. The Ultimate Tutorial. #6
nice tutorial, thanks Biggrin

Reply

RE: Batch. The Ultimate Tutorial. #7
(07-30-2011, 11:02 AM)chipp Wrote:
Code:
for %%a in (%a%,%b%,%c%) do ( echo texttexttext>%%a )

so %%a is not refer to 'a' variable we've set up? and the contain of %%a is defined within 'do' block?

Quote:The "in" specifies the the string that we are setting the variable "%%a" too

what's that supposed to mean?

I don't get your first question.

The second question, though, means that what is inside the parentheses after the "in" defines "%%a".
[Image: nNICR.jpg]
Creds to bluedog

For a list of my contributions (includes tutorials). Click here.

If you'd like to apply for Heat, please click here.

Reply

RE: Batch. The Ultimate Tutorial. #8
i mean what i've seen is:

texttexttext

is inserted into the three files we've included in bracket (a, b, c) so i would assume that "texttexttext" is defined into %%a, so that's mean that 'a' variable in this case is not the same 'a' as the first variable that we created (which contains "text.txt") ? and the value of %%a is defined within do block right?

Code:
do ( echo texttexttext>%%a )
[Image: ap8g35.jpg]

Reply

RE: Batch. The Ultimate Tutorial. #9
(08-02-2011, 09:20 AM)chipp Wrote: i mean what i've seen is:

texttexttext

is inserted into the three files we've included in bracket (a, b, c) so i would assume that "texttexttext" is defined into %%a, so that's mean that 'a' variable in this case is not the same 'a' as the first variable that we created (which contains "text.txt") ? and the value of %%a is defined within do block right?

Code:
do ( echo texttexttext>%%a )

No. Texttexttext is WRITTEN into the file that is defined by "%%a".

[Image: nNICR.jpg]
Creds to bluedog

For a list of my contributions (includes tutorials). Click here.

If you'd like to apply for Heat, please click here.

Reply

RE: Batch. The Ultimate Tutorial. #10
yes, but where is it defined? is it in the do? and so that's mean that %%a is not the same a we've declared before right?
[Image: ap8g35.jpg]

Reply







Users browsing this thread: