![]() |
|
Batch. The Ultimate Tutorial. - Printable Version +- Sinisterly (https://sinister.ly) +-- Forum: Coding (https://sinister.ly/Forum-Coding) +--- Forum: Coding (https://sinister.ly/Forum-Coding--71) +--- Thread: Batch. The Ultimate Tutorial. (/Thread-Batch-The-Ultimate-Tutorial) |
Batch. The Ultimate Tutorial. - Reverence - 07-05-2011 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!
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. [/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%
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. [/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 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. 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 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. [/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.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. [/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] RE: Batch. The Ultimate Newbie Guide. - changeusername123 - 07-05-2011 Nice threads mate. Thanks for ur efforts. RE: Batch. The Ultimate Newbie Guide. - Reverence - 07-05-2011 (07-05-2011, 07:16 PM)∑√ıŠWrote: Nice threads mate. Thanks bro! Glad you like it. RE: Batch. The Ultimate Newbie Guide. - jammy7777 - 07-05-2011 hmm nice game with batch file
RE: Batch. The Ultimate Tutorial. - chipp - 07-30-2011 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? RE: Batch. The Ultimate Tutorial. - JuiceKing - 07-30-2011 nice tutorial, thanks
RE: Batch. The Ultimate Tutorial. - Reverence - 07-30-2011 (07-30-2011, 11:02 AM)chipp Wrote: I don't get your first question. The second question, though, means that what is inside the parentheses after the "in" defines "%%a". RE: Batch. The Ultimate Tutorial. - chipp - 08-02-2011 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
)RE: Batch. The Ultimate Tutorial. - Reverence - 08-02-2011 (08-02-2011, 09:20 AM)chipp Wrote: i mean what i've seen is: No. Texttexttext is WRITTEN into the file that is defined by "%%a". RE: Batch. The Ultimate Tutorial. - chipp - 08-03-2011 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? |