Sinisterly
A Batch Tutorial - Printable Version

+- Sinisterly (https://sinister.li)
+-- Forum: Coding (https://sinister.li/Forum-Coding)
+--- Forum: Coding (https://sinister.li/Forum-Coding--71)
+--- Thread: A Batch Tutorial (/Thread-A-Batch-Tutorial)



A Batch Tutorial - killerOfCode - 03-24-2012

- Getting Started

In order to begin the scripting of a batch file, you must create a file. For this, you don't need a

special program or IDE, all you need is a basic text editor such as Notepad, which should come with

your Windows OS. For this tutorial, we are going to use Notepad to create our files. Firstly, you

must save a file as [anything].bat. For some versions, you may need to change the 'Text Documents

(*.txt)' to 'All Files'.

- Writing Scripts

After you have created your file, you can start writing code. Here are a few basic commands that will

help with your scripting...

Adding a comment to your code:
PHP Code:
::not counted as part of the code




Print a line on the screen:
PHP Code:
echo Hello World! ::the below will print a blank line. echo. ::another useful command for echo, is to disable the display of command that are being used. @echo off




Setting a variable:
PHP Code:
::user input for the line below set /P variable= ::also, setting one without user input set variable=Hello World! ::you can print them too echo %variable%




Functions and calling them:
PHP Code:
:function1 goto function2 :function2




Pausing the script:
PHP Code:
pause ::do it without prompting? pause > nul




Although not as refined, you can make a conditional statement. Here is an example of a login code:
PHP Code:
@echo off echo Password: set /p pass= if '%pass%' == 'world' goto correct goto fail :correct echo yay! pause exit :fail echo boo! pause exit




You can actually create a file too:
PHP Code:
echo hello world > new.txt ::append to a file echo hello world >> new.txt




Finding text in a file:
PHP Code:
find "hello" world.bat




And lastly, quitting the console window:
PHP Code:
exit


- Conclusion
I hope you enjoyed reading this article, and if you are going to copy this, please give me some credit. I'd also like to mention that batch files are not sophisticated enough to create a virus, so any threads indicating the opposite are actually programs to annoy users, but not gain information of any sort.


P.S. - This is my first tutorial, so don't go around hatin'. Biggrin



RE: A Batch Tutorial - killerOfCode - 05-19-2012

No comments for my nicely written tutorial? Sad


RE: A Batch Tutorial - #Unkn0wn - 05-19-2012

Because there is another tutorial about that o.O


RE: A Batch Tutorial - killerOfCode - 05-21-2012

Yay! Somebody posted... what I probably wouldn't want to hear. lol xD


RE: A Batch Tutorial - ArkPhaze - 05-21-2012

Code:
if '%pass%' == 'world' goto correct goto fail

Here I would check if it's not 'world' then goto :fail if it's not, otherwise it will avoid that check and goto :corrrect anyways, so there's no need for goto :correct in there.

Rest of the tutorial is okay... Other than here:
Code:
echo hello world > new.txt ::append to a file echo hello world >> new.txt

Where it should be mentioned that paths with spaces should be wrapped in quotes.


RE: A Batch Tutorial - killerOfCode - 05-21-2012

Yeah, well I'm really suggesting that people are smart enough to realize the second problem. Wink