Batch script guide

Batch Script - Quick Guide

Batch Script is incorporated to automate command sequences which are repetitive in nature. Scripting is a way by which one can alleviate this necessity by automating these command sequences in order to make one’s life at the shell easier and more productive. In most organizations, Batch Script is incorporated in some way or the other to automate stuff.

Some of the features of Batch Script are −

  • Can read inputs from users so that it can be processed further.

  • Has control structures such as for, if, while, switch for better automating and scripting.

  • Supports advanced features such as Functions and Arrays.

  • Supports regular expressions.

  • Can include other programming codes such as Perl.

Some of the common uses of Batch Script are −

  • Setting up servers for different purposes.

  • Automating housekeeping activities such as deleting unwanted files or log files.

  • Automating the deployment of applications from one environment to another.

  • Installing programs on various machines at once.

Batch scripts are stored in simple text files containing lines with commands that get executed in sequence, one after the other. These files have the special extension BAT or CMD. Files of this type are recognized and executed through an interface (sometimes called a shell) provided by a system file called the command interpreter. On Windows systems, this interpreter is known as cmd.exe.

Running a batch file is a simple matter of just clicking on it. Batch files can also be run in a command prompt or the Start-Run line. In such case, the full path name must be used unless the file's path is in the path environment. Following is a simple example of a Batch Script. This Batch Script when run deletes all files in the current directory.

:: Deletes All files in the Current Directory With Prompts and Warnings
::(Hidden, System, and Read-Only Files are Not Affected)
:: @ECHO OFF
DEL . DR

Batch Script – Environment

This chapter explains the environment related to Batch Script.

Writing and Executing

Typically, to create a batch file, notepad is used. This is the simplest tool for creation of batch files. Next is the execution environment for the batch scripts. On Windows systems, this is done via the command prompt or cmd.exe. All batch files are run in this environment.

Following are the different ways to launch cmd.exe −

Method 1 − Go to C:\Windows\System32 and double click on the cmd file.

Writing Executing Method1

Method 2 − Via the run command – The following snapshot shows to find the command prompt(cmd.exe) on Windows server 2012.

Writing Executing Method2

Once the cmd.exe is launched, you will be presented with the following screen. This will be your environment for executing your batch scripts.

Batch Scripting Environment

Environment Variables

In order to run batch files from the command prompt, you either need to go to the location to where the batch file is stored or alternatively you can enter the file location in the path environment variable. Thus assuming that the batch file is stored in the location C:\Application\bin, you would need to follow these instructions for the PATH variable inclusion.

OSOutput
WindowsAppend the String; C:\Application\bin to the end of the system variable PATH.

Batch Script – Commands

In this chapter, we will look at some of the frequently used batch commands.

S.NoCommands & Description
1VER

This batch command shows the version of MS-DOS you are using.

2ASSOC

This is a batch command that associates an extension with a file type (FTYPE), displays existing associations, or deletes an association.

3CD

This batch command helps in making changes to a different directory, or displays the current directory.

4CLS

This batch command clears the screen.

5COPY

This batch command is used for copying files from one location to the other.

6DEL

This batch command deletes files and not directories.

7DIR

This batch command lists the contents of a directory.

8DATE

This batch command help to find the system date.

9ECHO

This batch command displays messages, or turns command echoing on or off.

10EXIT

This batch command exits the DOS console.

11MD

This batch command creates a new directory in the current location.

12MOVE

This batch command moves files or directories between directories.

13PATH

This batch command displays or sets the path variable.

14PAUSE

This batch command prompts the user and waits for a line of input to be entered.

15PROMPT

This batch command can be used to change or reset the cmd.exe prompt.

16RD

This batch command removes directories, but the directories need to be empty before they can be removed.

17REN

Renames files and directories

18REM

This batch command is used for remarks in batch files, preventing the content of the remark from being executed.

19START

This batch command starts a program in new window, or opens a document.

20TIME

This batch command sets or displays the time.

21TYPE

This batch command prints the content of a file or files to the output.

22VOL

This batch command displays the volume labels.

23ATTRIB

Displays or sets the attributes of the files in the curret directory

24CHKDSK

This batch command checks the disk for any problems.

25CHOICE

This batch command provides a list of options to the user.

26CMD

This batch command invokes another instance of command prompt.

27COMP

This batch command compares 2 files based on the file size.

28CONVERT

This batch command converts a volume from FAT16 or FAT32 file system to NTFS file system.

29DRIVERQUERY

This batch command shows all installed device drivers and their properties.

30EXPAND

This batch command extracts files from compressed .cab cabinet files.

31FIND

This batch command searches for a string in files or input, outputting matching lines.

32FORMAT

This batch command formats a disk to use Windows-supported file system such as FAT, FAT32 or NTFS, thereby overwriting the previous content of the disk.

33HELP

This batch command shows the list of Windows-supplied commands.

34IPCONFIG

This batch command displays Windows IP Configuration. Shows configuration by connection and the name of that connection.

35LABEL

This batch command adds, sets or removes a disk label.

36MORE

This batch command displays the contents of a file or files, one screen at a time.

37NET

Provides various network services, depending on the command used.

38PING

This batch command sends ICMP/IP "echo" packets over the network to the designated address.

39SHUTDOWN

This batch command shuts down a computer, or logs off the current user.

40SORT

This batch command takes the input from a source file and sorts its contents alphabetically, from A to Z or Z to A. It prints the output on the console.

41SUBST

This batch command assigns a drive letter to a local folder, displays current assignments, or removes an assignment.

42SYSTEMINFO

This batch command shows configuration of a computer and its operating system.

43TASKKILL

This batch command ends one or more tasks.

44TASKLIST

This batch command lists tasks, including task name and process id (PID).

45XCOPY

This batch command copies files and directories in a more advanced way.

46TREE

This batch command displays a tree of all subdirectories of the current directory to any level of recursion or depth.

47FC

This batch command lists the actual differences between two files.

48DISKPART

This batch command shows and configures the properties of disk partitions.

49TITLE

This batch command sets the title displayed in the console window.

50

Comments

Popular posts from this blog

Paytm carding trick

HOW TO MAKE UNDETECTABLE SCAMPAGE TUTORIAL

Computer short keys