BATCH Scripting language commands and explanations, syntax, as well as examples and uses! 0.0 Introductions 0.1 Introducing: BATCH Scripting 0.2 Prerequisites for BATCH 0.3 How this document is annotated 1.0 Simple stuff 1.1 Displaying text 1.2 Reading a file 1.3 Writing to a file 1.4 Screen functions 1.5 Pausing execution 1.6 Slowing execution 1.7 Making random numbers 2.0 Advanced stuff 2.1 IF THEN statements 2.2 SET statements 2.3 Displaying variables 2.4 Using variables 2.5 Encoding and decoding files 2.6 Label statements 2.7 GOTO statements 2.8 Detecting key presses 2.9 Looping programs 3.0 Professional stuff 3.1 Selective randomization 3.2 CALL statements 3.3 Pinging internet locations 3.4 Opening applications 4.0 Examples 4.1 Simple examples 4.2 Advanced examples 4.3 Professional examples 5.0 Legal stuff 5.1 Copyright 5.2 Legal rights 5.3 Document information Section 0.0 - Introductions Part 0.1 - Introducing: BATCH Scripting So you want to learn how to BATCH like a pro, huh? Well, don't worry, that can be arranged easily! If you follow the instructions in this document, you will see just how easy it really is to learn and begin to use BATCH Scripts! First off, BATCH is what is called a Scripting language or a Shell language because of it's limitations and simplicity as compared to other languages such as BASIC, FORTRAN, or any of the C languages. Why should you learn BATCH? Because of a few simple reasons: -Small files compared to Java or any C language -Much easier to learn to program professionally -Simple commands that can do many things at once -Performance is not affected much by sloppy or non-linear programming -And to top it all off, a massive user base and no extra required installs! Part 0.2 - Prerequisites for BATCH Well, this IS batch we're talking about here, so there aren't really that many requirements, really just any Windows computer made in 2003 or later should be just fine, but some things require Windows 7 or later, others only require Windows 2000, and so forth. Sections that have special requirements will have an annotation explaining what you need and why. The only real suggestion I can give for requirements is probably something like the following, though there are certainly a lot of exceptions: -Intel Pentium 4 1.8GHz or faster -256MB or more RAM -2GB of free uncompressed hard disk space -Microsoft Windows XP Home Edition Service Pack 2 or later -Any speed internet connection You may also want to acquire the famous Notepad++, and set it to BATCH language mode, as it really does help out quite a lot when you need to find mistakes in your programs, as it helps by highlighting commands and stuff with colours and highlighters to allow you to see the words easier. Part 0.3 - How this document is annotated This document will (hopefully) be more organized than the TextEngine 0.1 to 1.4 README.TXT files were written, so I'll be sure to add plenty of annotation types in so you can tell exactly what you're doing when and where. Annotations are explained below: -When a section is annotated as +text+, it means that there are some extras you might want, and explains why you might want them. -When a section is annotated as (text), it means that you should insert some text here without the parentheses, obviously. -When a section is annotated as [text], it means that you should insert a variable here, without brackets. -When a section is annotated as {text}, it means that you should insert another command here, without brackets. -When a section is annotated as , it means that I have made a mistake in my typing, as per usual operation. Section 1.0 - Simple stuff IMPORTANT! You MUST put @ECHO OFF then ECHO OFF at the beginning of every BATCH script otherwise you will get a broken terminal! ECHO. May be used to invoke a line break, and MUST be used both BEFORE AND AFTER displaying the contents of a document on screen! EXIT must be used at the END OF A PROGRAM OR WHEN EXITING A PROGRAM ONLY! Part 1.1 - Displaying text Displaying text in BATCH can be done in one of two ways - direct or variable. When using variable, you display the value of a selected variable, and when using direct, you display the exact typed text. Here are some examples of each: -ECHO (write something to display here) -ECHO %[put your variable here]% -ECHO (write something to display here)%[put your variable here]* Part 1.2 - Reading a file Reading files in BATCH is done using the TYPE command, and it displays the ASCII contents of any file. Here is an example: -TYPE C:\Windows\WIN.INI Which displays the contents of a file on drive C in the folder Windows called WIN.INI. You can also use variables in these commands, as shown here: -TYPE C:\TE_RA\%GAME%\%TYPE%.MON This displays the contents of the file on disk C in a folder called TE_RA, then another folder with the same name as the %GAME% variable, and then a file with the same name as %TYPE% with .MON added to the end. Part 1.3 - Writing to a file Writing to a file in BATCH is extremely easy as compared to most other programming languages using complex commands to achieve what can be done simply in one line of code in BATCH. Here is an example: -ECHO (write something here) >> (put the file directory here) -ECHO Hello World! >> C:\HELLO.TXT This file can then be viewed and edited in much the same way as any other normal text document. Part 1.4 - Screen functions This section is mostly a combination of other commands dealing with the terminal window, and allows you to modify the data displayed in it easily. The first of these commands is COLOR, which changes the colours of the screen accordingly with a 16 colour palette described in hexadecimal to change the background and foreground colours of the terminal window. Here is the syntax of the COLOR command: -COLOR (background colour)(foreground, or text, colour) -COLOR %[background variable]%%[text variable]% As always, the operators for the command can be changed using variables. Here are some more examples: -COLOR 0A -COLOR 40 -COLOR %A%%B% -COLOR %BG_C%%TX_C% Here is a table of the colors available for use: 0 = Black 8 = Gray 1 = Blue 9 = Electric Blue 2 = Green A = Lime green 3 = Aqua B = Light Aqua 4 = Red C = Light Red 5 = Purple D = Light Purple 6 = Yellow E = Light Yellow 7 = White F = Bright White Colour tables such as this one are EXTREMELY useful when programming with colours, as they help to assure you that you get it right on the first try. To clear the terminal window's display area, you just type a simple 3-letter command like: -CLS -CLEAR Of course, this command has no operators so you can leave it as is. Part 1.5 - Pausing Execution Pausing program execution in BATCH is simple, here is an example: -PAUSE You can also type >NUL after PAUSE to allow for custom text instead of the standard "Press any key to continue...". Here is an example: -ECHO Press any key to return to the menu... -PAUSE>NUL These commands are very important when programming in BATCH, as it can help you debug your programs easily by stopping every command. Part 1.6 - Slowing execution +Windows 7 or later is REQUIRED for this to work, so bear that in mind!+ Slowing execution in a BATCH program is easy, here's the syntax for that command: -TIMEOUT (number of seconds to wait) > NUL -TIMEOUT %[number of seconds variable]% > NUL Here are some real world examples: -TIMEOUT 0.5 >NUL -TIMEOUT %s% >NUL This command only works in Windows 7 or later, so keep that in mind when programming with this command. Part 1.7 - Generating random numbers Generating random numbers in BATCH uses the SET command with the /A (math) switch to allow a variable to be set to a random value. Here's an example: -SET /A RANDOM=%RANDOM% Doing this will generate a random number from -32767 to 32768. Later on, I will show you how to dictate what numbers actually go through, as there is no way currently to change what numbers are generated in BATCH. Section 2.0 - Advanced stuff Part 2.1 - IF THEN statements I'll just give the syntax and a few examples for this command, as I would prefer not to explain for hours what it does and how you can use it, and I'm sure that you don't want to read that either. Here's the syntax: -IF %[variable to compare]%==(variable or number to compare to) THEN {commands go here} -IF %MENU%==0 THEN GOTO DEBUG If a number is greater than another: -IF /I %[variable to compare]% GTR (variable or number to compare to) THEN {commands to perform} -IF /I %CFA% GTR 16384 THEN GOTO MENU If a number is smaller than another: -IF /I %[variable to compare]% LSS (variable or number to compare to) THEN {commands to perform} -IF /I %CFA% LSS 2048 THEN GOTO MENU Part 2.2 - SET statements Setting variables is easy. Use /A to do math, and /P to take user input: -SET /P USER=?= -SET /P %[variable to change]%=(text to display to user) -SET /A PHP=%PHP%-%MATK% -SET /A %[variable to change]%=(math equations or including numbers or variables) Part 2.3 - Displaying variables This is done with the ECHO command: -ECHO %[variable to display]% -ECHO %PHP% -ECHO You're HP has dropped to %PHP%! Part 2.4 - Using variables I just explained this... Part 2.5 - Encoding and Decoding files This is surprisingly easy to do in BATCH! When a file is encoded, this is the syntax: -COPY (address of file to be encoded) (address to encode to) All you need to do in BATCH is change the file name and extension and you have encoded a file enough that normal users will not be able to open them! When decoding a file use this syntax: -COPY (encoded file address) (temporary folder) Just copy the file to a temporary folder with the original file extension (name does not matter) and delete it when you are done! Part 2.6 - Label statements Label statements are done as follows: -:(label name) Label names cannot contain spaces or the following characters: :, ;, &, %, #, $, @, /, \, >, <, | Part 2.7 - GOTO statements GOTO a label. Syntax: -GOTO (label name, no colon) Part 2.8 - Detecting key presses See Part 2.2 - SET statements. Part 2.9 - Looping programs To loop your program or a specific section of it, GOTO the same LABEL at the end of that LABEL section. Section 3.0 - Professional stuff Part 3.1 - Selective randomization Selective randomization in BATCH is actually one of the more difficult things to do, as you constantly have to generate numbers until one goes through. There are 2 ways to achieve this,and neither really works well in my experience, sometimes they can either be unreliable or in the case of one of these two methods just plain slow. The first method is to do something like this: -:RANDOMIZER -SET /A %[variable to randomize]%=%RANDOM% -IF /I %[variable again]% GTR (max number +1) GOTO RANDOMIZER -IF /I %[variable again]% LSS (min number -1) GOTO RANDOMIZER -GOTO (wherever you use this number) The other of these two methods looks something like this and is longer, but generally is much faster and unreliable in some cases. -:RANDOMIZER -SET /A %[variable]%=%RANDOM% -IF /I %[variable]% LSS 1024 GOTO (code) -IF /I %[variable]% LSS 2048 GOTO (more code) -IF /I %[variable]% LSS 4096 GOTO (even more code) -IF /I %[variable]% LSS 8192 GOTO (wow more code) -IF /I %[variable]% LSS 16384 GOTO (darn man how much code you got) -IF /I %[variable]% LSS 32768 GOTO (lots of code) -GOTO RANDOMIZER It is EXTREMELY IMPORTANT that you put the smallest number first, otherwise the sequence won't work properly and will skip all later code. Like I said, these are NOT fun to deal with, but they have to be done sometimes and I understand that (I'm FAMICOMASTER from the TextEngine team!) so I've tried to give you the best examples I could come up with here. Part 3.2 - CALL statements CALL statements are as follows and allow you to run another BATCH inside a the current BATCH window, but do not allow you to exit to the previous file. -CALL (location of BATCH file) Part 3.3 - Pinging internet locations To ping an internet location, do the following: -PING (address) (time) (ping size) Part 3.4 - Opening applications to open an application, do the following: -OPEN (location of application) (location to run from) Section 4.0 - Examples Part 4.1 - Simple example A basic example on using all of the SIMPLE commands we learned earlier: @ECHO OFF ECHO OFF CLS COLOR 0A ECHO Hello World! ECHO Hello World! >> C:\TXT.TXT ECHO. TYPE C:\TXT.TXT ECHO. ECHO PRESS ANY KEY TO GO ON... PAUSE>NUL SET /A RAND=%RANDOM% ECHO %RAND% ECHO PRESS ANYTHING TO LEAVE... PAUSE>NUL EXIT Part 4.2 - Advanced example A more advanced example using all of the ADVANCED commands we learned earlier: @ECHO OFF ECHO OFF CLS COLOR 40 :LOOP ECHO 1 2 OR 3? SET /P USER=?= IF %USER%==1 GOTO RIGHT IF %USER%==2 GOTO WRONG IF %USER%==3 GOTO WRONG IF %USER%==4 GOTO CHEAT GOTO LOOP :WRONG ECHO YOU GOT IT WRONG TRY AGAIN! GOTO LOOP :CHEAT ECHO YOU CHEATED! TRY AGAIN! GOTO LOOP :RIGHT CLS ECHO YOU GOT IT RIGHT! CLS ECHO YOU GOT IT RIGHt! CLS ECHO YOU GOT IT RIGhT! CLS ECHO YOU GOT IT RIgHT! CLS ECHO YOU GOT IT RiGHT! CLS ECHO YOU GOT IT rIGHT! CLS ECHO YOU GOT It RIGHT! CLS ECHO YOU GOT iT RIGHT! CLS ECHO YOU GOt IT RIGHT! CLS ECHO YOU GoT IT RIGHT! CLS ECHO YOU gOT IT RIGHT! CLS ECHO YOu GOT IT RIGHT! CLS ECHO YoU GOT IT RIGHT! CLS ECHO yOU GOT IT RIGHT! CLS GOTO RIGHT EXIT Part 4.3 - Professional example Yeah, I'm not typing this. If you REALLY WANT a professionally made BATCH example program, feel free to snoop around in TextEngine 1.4A when it's out I'm sure you can figure out what does what. Section 5.0 - Legal stuff Part 5.1 - Copyright BATCH belongs to Microsoft This document belongs to AiO Systems Design Example content in this document may be copied and reused whenever and wherever. Part 5.2 - Legal rights BATCH belongs to Microsoft, and AiO Systems design CEO FAMICOMASTER created this entire document. Part 5.3 - Document Information Document finished: 9/18/2015 4:20PM Document author: FAMICOMASTER Document computer: GATEWAY500GR Document title: BATCH explanations Document type: TXT ASCII text file Document program: NotePad++ v6.7.5 Document OS: Microsoft Windows 7 Home Premium Document subject: BATCH language command explanations for amateur programmers Document size: 14734 Bytes (14.4 KB)