![]() |
|
Your definitive guide to batch scripting - Printable Version +- Sinisterly (https://sinister.li) +-- Forum: Coding (https://sinister.li/Forum-Coding) +--- Forum: Coding (https://sinister.li/Forum-Coding--71) +--- Thread: Your definitive guide to batch scripting (/Thread-Your-definitive-guide-to-batch-scripting) Pages:
1
2
|
Your definitive guide to batch scripting - Ambiguous - 10-28-2011 Your definitive guide to batch scripting Simple programming commands in a batch environment. Things you need to know about batch : Quote:Batch is easy to learn and is fun to use. Things you need : Quote:Computer :epic: Lets us start learning this awesome language! How to write a batch file : Type in the DOS commands you want to execute, save it as a .bat file, and you are good to go! However, there are also more sophisticated batch file structures, using simple programming commands built into the batch structure. Your first batch file: Most programming tutorials start with “Hello worldâ€Â, so I shall do the same Type this code in notepad and save it as Hello world.bat. Quote:Code: The MS-DOS window then prints out : Quote:Hello world! So, how does this program work? Quote:@echo off : When echo is turned off, the command prompt does not appear in the Command Prompt window. Used in most batch files. If you take away the echo off, all commands will be shown instead. So, you may not want to have the “Press any key to continue...†at the end of your batch file, so now, we will learn the “nul†command. Nul can be used in many different ways, like "pause > nul", or echo abc > nul This command basically hides “Press any key to continue . . .â€Â, and can be used to hide any command. Let us make our first program with the nul command. Quote:Code: Type this code into Notepad and save it as [Your file name here].bat. Do you understand what this batch script does? The batch script will print “And this line too†2 lines below the first line because I used the command “echo.†When you want to enter an extra line when printing text on screen, you will type "echo.". It will show as a blank line in the ms - dos window. Well,what about the “::â€Â? This is used to type comments, like using // in C++. You can also use the REM command for comments. It is used by programmers to insert comments into their code to make it more readable and understandable. It is normally not used in small easy to understand batch programs but is very useful in huge snippets of code with geek stuff loaded into it, which is really hard to understand. Usage: Quote:@echo off Launching programs and websites Next, I will teach you how to launch programs from batch files. For most programs, you have to add the file path to run the file, but for windows built – in programs, you don’t! For example, you can type in : Quote:start notepad.exe and notepad will open Some of the built – in programs ( you can find more online) Quote:start mspaint Now, you will learn how to launch websites with batch files. Type in : Quote:start www.hackcommunity.com This will make the website will open in your default browser. If you want to launch it in Internet Explorer (I hate IE, so I don't use it), type : Quote:start iexplore www.hackcommunity.com Let’s say you wanted to open a certain file in [My Documents] folder with a batch file. You can just type in : Quote:start C:\Users\[your login name]\Documents\[filename].[filetype]. It's that simple! Changing the text & background colour You will now learn how to change the colour (I am not going to use American English here ) of a batch file.Scenario : You’re bored with the default colour of the window and text, but you don’t know how to change it. Well, let me teach you how! The [color] ( american english ) command specifies colour attribute of console output.Colour attributes are specified by TWO hex digits -- the first corresponds to the background; the second the foreground. Each digit can be any of the following values: Quote:0 = Black Example: "color fc" produces light red on bright white Note : You cannot have the same colour background and foreground. Type "color" and not "colour" :dance: Changing the title of your DOS window : using the command titled “title†Quote:Code : Title [your title] For example : Quote:Code: Copying files : The “copy†command copies files from one location to another. The destination defaults to the current directory. So, your first input will be Usage : copy “[destination of file + filename] †“[destination where you want to copy the files to] For example, you want to copy a file from Your C: drive to your desktop. Here is how you will write the batch file. Quote:@echo off Creating text files: Quote:@echo off This batch file would add "Your text goes here" to the file qwerty.txt. If there is already text in the files, it will be erased and replaced by “Your text goes here†unless it is an empty file or you use†Your text goes here >> qwerty.txt†. This will keep the text in the file and add a new line. Loops Now, we will learn about loops and how to execute them. Loops save A LOT of time. For example, I can create a batch file that pings "www.hackcommunity.com" over and over. Quote:@echo off The goto command tells the command interpreter to goto a line with : (labelled line). Loops are definitely useful, but they can also be dangerous. For example, we can create a batch file that opens internet explorer infinitely until the computer crashes due to overload of RAM. I recommend you not to try this on your computer. :epic: Quote:@echo off Tasklist & taskkill: Next, we will learn the tasklist and taskkill commands. The tasklist shows the lists of running processes and taskkill removes them. Quote: As you can see all the process running are shown. Now for the taskkill command. We can use taskkill in many different ways like: Quote:@echo off This kills the process with the process ID 1234. You can also kill processes like this : Quote:@echo off This kills notepad. This is useful in many ways, and can be used to kill lagging processes. Assigning variables: Now, we will move on to assigning variables. In programming, a variable is a set of number, letters or symbols used to assign values to anything . This is useful because most programs wait for user input before doing what the user inputted. To modify variables in batch, you will use the set command. And here’s the really cool part, You can make them up as you go along, and assign them as you wish (as long as you don’t use one that has a legitimate assigned value, such as, say, %appdata%, or like %windir%. Quote:@echo off The batch file will print out “Which country are you from?†and them sets the variable “userInput†Set/p tells the interpreter to wait for a user input. You can replace “userInput†with anything you want because that is the variable. Another batch file with variables : Quote:@echo off This sets the variable abc to “Good day Hackcommunity!†and then prints the variable out. The del command: Used to deleted files and folders. Quote:@echo off This batch file goes to your C: drive and deletes the text file “abcâ€Â. I don't think I have to explain this. ![]() Date and time in batch: Quote:@echo off The %DATE% and %TIME% variables are already built – in commands, so they do not have to be assigned. This is all for now, I will be updating this thread regularly. Please Rate, comment and + rep if you liked this tutorial! Have fun with batch scripting! :bye: RE: Your definitive guide to batch scripting [part 1] - FunKx - 10-28-2011 Nice guide, but a bit too...dry It could be longer.
RE: Your definitive guide to batch scripting [part 1] - FunKx - 10-28-2011 Nice guide, but a bit too...dry It could be longer.
RE: Your definitive guide to batch scripting [part 1] - SPG.WoLF_TSC - 10-28-2011 Nice dude, good for beginners. @FunKx This is the first part (written in the description)
RE: Your definitive guide to batch scripting [part 1] - SPG.WoLF_TSC - 10-28-2011 Nice dude, good for beginners. @FunKx This is the first part (written in the description)
RE: Your definitive guide to batch scripting [part 1] - JuiceKing - 10-28-2011 I can't wait part 2 Very nice tutorial bro! ^^
RE: Your definitive guide to batch scripting [part 1] - JuiceKing - 10-28-2011 I can't wait part 2 Very nice tutorial bro! ^^
RE: Your definitive guide to batch scripting [part 1] - FunKx - 10-28-2011 I know it's part 1, it's just too short ^^ how many parts will be there this short? 256? LOL xD RE: Your definitive guide to batch scripting [part 1] - FunKx - 10-28-2011 I know it's part 1, it's just too short ^^ how many parts will be there this short? 256? LOL xD RE: Your definitive guide to batch scripting [part 1] - ReaperX - 10-28-2011 Nice Tut. There are a lot of these. Beginners Shouldnt Complain. They can get Info from Just about Everyones Perspective.
|