Engenharia informática
Preface This book ‘Batch File Programming’ is written after experimenting and testing all the snippets covered in this book. Batch File Programming is a pretty old one, but i have found lot of books that haven’t covered the dark-side of the batch, which still remains untold.
The ultimate goal of this book is to make the readers understand how it works, What are the limitations of the batch, what else is possible with a batch, constructing useful programs with various views, Creating a batch virus by mis-using the commands, creating or ThlS book is aimedat _ if you are new to pro this would be the rig eal time examples along with ble and Iot more. grammer, No matter e this book contains screenshots that really helps in a better understanding ofthe concept. First Edition May 2009 Acknowledgements First and foremost I would like to thank my Mum and Dad for their constant care and blessings.
My Special thanks to Mr. C. Robinson (CEO, W3cert), for his kind encouragement in authoring this book more over cannot forget to express my gratitude for my relatives and comrades. contents in this E-gook ‘gatch File Programming’ Will really help the students of W3Cert for their exploration in batch file programming nd interfenng WIth the windows kernel by using the commands given in this book. This page is intentionally Ieft blank Batch File Programming Batch File Programming Introduction Batch file programming is the native programming offered by the Microsoft Windows Operating System. atch file is created using any text editors like notepad, WordPad, WinWord or so on, which comprises of a sequence of built-in commands used to perform some often done tasks like deleting a series of files of same type or of different type, creating logs, clearing unwanted craps from your computer and even for creating a batch VIRUS. Whenever a gatch program is executed, it was interpreted line-by- line by the CLI (Command Line Interpreter) command. com or the cmd. exe. Batch file is really helpful in automating tedious tasks and for maintaining system logs.
The commands used while creating a batch file are case insensitive, in the sense that it may accept both small and upper case letters. Modes: There are two diferent m upported bv DOS (Disk user, the further processes are carried out. For example, let’s take the ‘del’ command. The ‘del’ command is used for deleting files that reside inside a directory. Now am going to elete all the files inside a folder named ‘a’, and when I executed the following command, it is interacting with me prompting “Are you sure (Y/N)? , confirming the deletion operation, and depending upon my input, it decides What to do. If I hit Y then it Will delete the files specified, else if I hit ‘N’ then it won’t delete. 2 Are you sure (WN)? y Batch Mode: Batch mode can also be referred as ‘Silent mode’ or ‘Quiet Mode’, and this is mere opposite to the interactive mode. The command that operates at batch mode Will never interact with the user at any instance, instead it Will take care of every operation by itself. For example, am going to explain this by using the same ‘del’ command.
There is a switch available for the ‘del’ command, which makes the command to operate at silent mode, and that switch is ‘/Q’ In this case, the command is not at all interacting with me, whether to delete those file or nota In the above example, have tried to delete the same files in the same folder by using the same command but with a different switch. Anyhow both the commands Will perform the same operation but the mode it operates differs. written using any of the text editors such as notepad, wordpad and so on, but notepad is the most often used text ditor in such cases.
Like any Ether programing languages, lets start our first program with the ‘Hello World’ program. 1. Open up a notepad and type the following. @echo off Echo Hello World pause 2. the file with any name you wish, but make sure that you save the file extension with . bat, in this case am saving this file as ‘first. bar. 3. When you save the batch file, then the icon becomes like the below icon, In Windows XP, the gatch file icon looks like above, where as in Windows Vista the Icon looks like the below image, 4. Just double click to execute the batch file that you have created now. And the output looks like, .
You are done! Let me explain What does the above given program does, ‘echo’ is the command used to print text on the screen, so whatever that follows the echo command Will be displayed on the output screen. This command is Just like the ‘printf statement in the C language. When you type the echo command alone, then it Will tell you whether the ‘echo is ON’ or ‘echo is OFR. It’s always recommended to turn the echo off, else it Will display the prompts like an screen, and the pause command is used to wait for the user interaction, whether to proceed further or not. If the pause is not used, then the batch
WIII terminate immediately after displaying the “Hello World”. 4 Internal and External Commands There are two types of commands that we can run from a command prompt, and they were, 1. Internal commands 2. External commands. Internal Commands Internal commands are nothing but the built-in commands that are shipped along with the operating system, for example, echo, cls, del, dir were few ofthe well known internal commands. External Commands External commands are the commands that are often created while installing a new application and these commands mostly have no use except calling that application and support files.
Few external commands can only be executed in the ‘Run’ dialog box (start C] Run), but not on the command prompt, and those commands include ‘firefox’. The ‘firefox’ command can be executed only from the run line, that too if the firefox application is installed on that machine and it won’t work on the command prompt Likewise the ‘firefox’ there are various other external commands such as the “PsTools” which includes commands like, psExec, PSFiIe, PSGetSid, psinfo, PSKill, PSLiSt, PsLoggedOn and so on. for constructing a good batch program. Here am going to list out the useful run line commands With a brief description.
Commands Descriptions access. cpl Accessibility Controls accwlz Accessibility Wizard appwiz. cpl Add/Remove Programs ciadv. msc Indexing Service control admintools Administrative Tools cleanmgr Disk Cleanup Utility control color Display Properties compmemt. msc Internet Explorer Properties ipconfig Windows IP Configuration intl. cpl Regional Settings joy. cpl Game Controllers lusrmgr. msc Local Users and Groups logoff Logs out current user magnify Open Magnifier 7 makecab Cabinet Maker, file compressor. msconfig Open System Configuration Utility mshearts relog Used for Logging. replace Replaces files rexe c
Runs commands on remote hosts running the REXEC service route Manipulates network routing tables rsh Runs commands on remote hosts running the RSH service Manages media resources using Removable Storage runas Allows a user to run specific tools and programs with diferent permissions than the users current logon provides. rege dit Opens Registrv Editor Administration. Used to start, stop, and send msg to a terminal session connected to via telnet. tscon Attaches a user session to a terminal session. tsdiscon Disconnects a session from a terminal server. tskill Ends a process. Even can terminate a process 10 unning on a remote session. ourstart Windows XP Tour Wizard tsshutdn shutdown in 60 sec typeperf Very useful in login events. Used to monitor Processor threads and writes into a specified log use rinit My Documents verifier P-aGFg and re directional operators Bitwise and Bitwise exclusive or BltV/ise or Assignment operators separator For using Multiple commands For executing one from many commands The above given were the operators available in Batch file programming for performing arithmetic and logical operations. Let me briefyou the operators with a small example, Note : For performing ari ns, the ‘SET’ command PAGF