Author Topic: Batch Files in a Nutshell by iTpHo3NiX  (Read 8226 times)

0 Members and 1 Guest are viewing this topic.

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Batch Files in a Nutshell by iTpHo3NiX
« on: October 30, 2011, 09:44:06 am »

Welcome to my first real tutorial for EvilZone. I will go over just about everything you can do with a batch file from simple commands to utilizing those in every day use, and very complex batch files to automate any process. This tutorial is geared towards batch files in a Windows XP environment. So lets get started.

Chapter 1 - What is a Batch File?

In DOS, OS/2, and Microsoft Windows, a batch file is a text file containing a series of commands intended to be executed by the command interpreter. When a batch file is run, the shell program (usually COMMAND.COM or cmd.exe, or Wscript.exe or cscript.exe) reads the file and executes its commands, normally line-by-line. Batch files are useful for running a sequence of executables automatically and are often used by system administrators to automate tedious processes. Unix-like operating systems (such as Linux) have a similar type of file called a shell script.

(Source Batch File Wikipedia )

What this means is you create a text document (using notepad or any simple text editor in ANSI format) with an extention of "bat" so it would be myfile.bat. Now why not just delve into the programming?

Chapter 2 - Hello World

Ok as will ANY programming language, we will create your very first batch file. Of course we will do the "Hello World" Application. So what we need to do is create a new text document and open the file in notepad. Now I will go line by line explaining what each of those lines do.

Line One:
Code: [Select]
@echo off
What that command does is will hide the command echo, so when you want to display text, it wont display the "echo" command or what is happening, some simply when you write the command, the command wont be visable only the output you designate.

Line Two:
Code: [Select]
title Hello World
This command sets the title of the command prompt to whatever you choose, I chose "Hello World" because that is the name of the file we are making. This command should be pretty straight foreward, your setting a title for the application

Line Three:
Code: [Select]
echo Hello World
This command is actually what we are displaying in the command prompt screen which is simply displays "Hello World" the echo command is one of the most common you will come across in batch scripting. I will go in further detail in the next chapter.

Line Four:
Code: [Select]
echo.
This command will create a blank line (like a line brake in HTML <br /> )

Line Five:
Code: [Select]
pause
This command is another common one that does exactly what it says, it will pause any further commands until a user pastes any key to continue. This is similar to the WAITKEY command in various programming languages.

So the final batch file code will look like this:

Code: [Select]
@echo off
title Hello World
echo Hello World
echo.
pause

And the output like this:


Ok that's it, your very first Hello World Batch File. Now to the next Chapter.

Chapter 3 - Common Commands

Ok as said before I will now be going in depth of the most commonly used commands in a batch file. Chances are in each of your batch files they will contain these commands. So lets learn the basics.

@

This commands is used in the begginning (usually) in front of the ECHO command as in "@ECHO OFF" and what it does is not echo back the text after the "at" symbol. And the purpose of the "@ECHO OFF" to prevent any of the commands in the batch fie from being displayed, just the information that is needed.

ECHO

This command has 3 functions, "ON", "OFF", and "MESSAGE". For example at the beggining of the batch file if it has "@ECHO ON" then it will disply each line of the batch on screen, however that command is not needed because if you just remove it, it is default so it will do that anyways, a real life application for that is when your debugging a batch file and just change OFF to ON in the first line just for simplicities sake. Now the OFF function will only display the output on the screen. And finally "MESSAGE" is a string of characters to display (remember the Hello World file). Wait, you think I'm done with echo? Hahaha, no way I'm just getting started. You can even use echo to insert text into a file. For example:

Code: [Select]
ECHO Word word word word>Filename.txt
That will create a text document with "Word word word word" in Filename.txt Now you can also use ECHO to display a variable, for example:

Code: [Select]
echo %temp% will display the location of your Temperary files that the %temp% variable points to. You can even have echo make a sound. To do this is the following command:

Code: [Select]
ECHO •
You use the 'Alt' key, followed by a 7 on the numeric keypad to get the 'ascii 7' character. Lastly I would like to touch on the blank line. As stated in the second chapter you do the following command to produce and empty line:

Code: [Select]
ECHO.
CLS

This command can be used a lot or very little, myself I use it A LOT, like the echo command its in every single one of my batch files. its use is simple, there are no parameters, its simply "CLS" you add it on a line to "CLear the Screen"

CD

Ok this command is to change the current directory to another. Another commonly used command in batch files (or navigating through a CLI or Command Line Interface, as opposed to GUI or Graphical user Interface). The Syntax is as follows:

Code: [Select]
CD [/D] [drive:][path]
CD [..]

The /D parameter changes the current DRIVE in addition to changing folders. Lets say your batch file is in the root of the drive, which we will call "C:\" however you want to create a batch file that deletes temperary files, so we need to be in the temp file for the computer, now here you can do one of two things, the first more complicated way is to use the "CD" command and do the following:

Code: [Select]
CD /D "C:\Documents and Settings\User\LocalData\temp"
Or more simply, and also universal for all computers:

Code: [Select]
CD %TEMP%
Then there is also the 'CD ..' which will continue to the parent directory, for example

Code: [Select]
C:\Working\Directory>cd .. [press enter]
C:\Working\>cd .. [press enter]
C:\>

So it goes up a directory. Now another function that I use with CD is the following:

Code: [Select]
CD\
This command with bring the current directory to the root directory. I include this usually right after '@ECHO OFF' in my batch files.

Offline iTpHo3NiX

  • EZ's Pirate Captain
  • Administrator
  • Titan
  • *
  • Posts: 2920
  • Cookies: 328
    • View Profile
    • EvilZone
Re: Batch Files in a Nutshell by iTpHo3NiX
« Reply #1 on: November 18, 2011, 10:47:29 am »
Man I still had like 20 unpublished chapters of this somewhere... I never did get the time to do it all. I might have a backup somewhere, I just don't know where. But I believe I left off on Chapter 25 or something lol
[09:27] (+lenoch) iTpHo3NiX can even manipulate me to suck dick
[09:27] (+lenoch) oh no that's voluntary
[09:27] (+lenoch) sorry

Offline ande

  • Owner
  • Titan
  • *
  • Posts: 2664
  • Cookies: 256
    • View Profile
Re: Batch Files in a Nutshell by iTpHo3NiX
« Reply #2 on: November 26, 2011, 09:12:35 pm »
Man I still had like 20 unpublished chapters of this somewhere... I never did get the time to do it all. I might have a backup somewhere, I just don't know where. But I believe I left off on Chapter 25 or something lol

Hope you find it :D
if($statement) { unless(!$statement) { // Very sure } }
https://evilzone.org/?hack=true

Offline iTpHo3NiX

  • EZ's Pirate Captain
  • Administrator
  • Titan
  • *
  • Posts: 2920
  • Cookies: 328
    • View Profile
    • EvilZone
Re: Batch Files in a Nutshell by iTpHo3NiX
« Reply #3 on: November 27, 2011, 02:18:34 pm »
Hope you find it :D

just searched throough the majority of my files, the only place it could be is on my external, if not then lost forever
[09:27] (+lenoch) iTpHo3NiX can even manipulate me to suck dick
[09:27] (+lenoch) oh no that's voluntary
[09:27] (+lenoch) sorry