Home › Forums › Great Computer Tips And Tricks › What is??? How to??? › What is a batch file and how do you make a simple one?
- This topic has 5 replies, 2 voices, and was last updated 11 years, 10 months ago by
Mitz.
-
AuthorPosts
-
March 30, 2009 at 11:31 pm #28649
Mitz
KeymasterWhat is a batch file?A batch file is a list of commands that are written into a notepad text file, but saved as a .bat extension instead of a .txt file. Saving the file as a .bat extension will make the file executable when clicked on. This means that when this file is opened it will carry out the commands in the batch file.The commands used in a batch file are dos commands and can also be typed into the dos command window to gain the same results. What are the benfits of using a batch file?
- A batch file can have a series of commands in one file. If you wanted to perform these operations through the dos command window it would take forevever to type in.
- A batch file can store strings of long commands that you would not usually be able to remember.
- Having a batch file for a particular task can save time.
- A batch file can help you perform tasks on your computer with precission and ultimate customization..
March 30, 2009 at 11:53 pm #31649Mitz
KeymasterThe easiest way to understand more about a batch file is to make a simple one so you can see how it works. First thing we will do is open a Notepad file.Go to the start menu and open run.Then type notepad in and press ok.In Vista type notepad into the search bar on the start menu and press enter. Now I am going to make a batch file to open a program. In this case we will open Internet Explorer. So in the notepad file you need to type these words:start iexplorer.exeType your own filename in where it says filename: I saved the file as startIE.bat with no .txt on the end. See screenshot below.Make sure you know where you have saved it. Now when you double click on that file it will open Internet Explorer.
March 31, 2009 at 1:13 am #31650Merlin33069
ParticipantOk, the next tutorial is another very simple oneopen your text editor again (notepad)and type:@Echo Hello World!@pausesave it as "Hello World.bat"when clicked a black box should pop up that says:Hello World!press any key to continue...That is how you make a batch file display anything you likeYou can also use multiple lines, like follows:@Echo Hello World!@Echo My name is Ryan!@pausethis will give you:Hello World!My name is Ryan!press any key to continuewith us so far?OKThe @ symbol tells the computer not to display the command, only the outputthe echo command tells the computer to display the text that follows on the same linethe pause command tells the computer to pause and wait for the user to press any key to continueWhen the computer reaches the end of a bat file, and has nothing to do, it closes, thats why we add the pause command, if we didnt have that the computer would have exited the program because it had nothing else to do. you wouldnt have been able to read the text...Also for those of you who read the thread mitz posted about the DIR command:any command (@echo Hello World!) for example, followed by a '>' and a name (>lol.txt) for example, so that it looks like this:@echo Hello World!>lol.txtwould take the output of the command (in this case 'Hello World!') and write it to the text file, so that nothing would display on the screen, and if we opened the text file we would see a single line that said:Hello World!
March 31, 2009 at 10:28 pm #31651Mitz
KeymasterThis is my favorite batch file but the commands in all batch files vary, depending on what Operating system you are using. In this example I will show you how to backup files with Windows XP and Windows Vista.This is so simple and so much fun.How to make a batch file to backup files in Windows XPIf you typed the following into a notepad file and saved it as yourfilename.bat it would not work because you need to put your file paths in there. This is one of those cases where we will need to know how to find a file path. The paths you have to change are in italic writing.xcopy "C:Documents and SettingsYour usernameMy DocumentsMy Pictures" "H:backup of pictures" /e /yThis command will backup my pictures on C: drive and paste it to H: drive in a folder called backup of pictures. If you put the destination as just H: than all the files will be all over the place. You must specify a folder. I usually make a new folder first. Of course you can also get the batch file to make the new folder for you but this is another article altogether. /e means to copy all directories and sub directories/y means to copy with prompting to overwrite the already exsisting filesNow in this simple batch file you could do so many things:You could add more paths and backup more files. You can make it so it only backs up one folder only or one file.How to make a batch file to backup files in Windows VistaNow to do the same thing in Windows Vista we just need to change the xcopy command to robocopy. So this is what it will look like. (but remember to put your own paths in there)robocopy "C:Documents and SettingsYour usernameMy DocumentsMy Pictures" "H:backup of pictures" /e /yType this text, with your own details, into a notepad and save as a .bat file and thats it.
March 31, 2009 at 10:41 pm #31652Mitz
KeymasterHow do you find out what switches or parameters modifies your command in the batch file?What is a switch or parameter?Switches are codes that can be added with a command to manipulate how the command is carried out. As in the last post using the XCOPY command, we used /e /y./e means to copy all directories and sub directories/y means to copy with prompting to overwrite the already exsisting filesThe xcopy command has a number of switches that you can use manipulate what the xcopy command does. How to find out what switches you can use with xcopy?Press on the WIN KEY and the letter R to open a run boxThen type cmd and then press OKNow type xcopy/?Now all the details about the switches you can use with the XCOPY command will appear. If you type robocopy/?
April 17, 2009 at 9:24 am #31653Mitz
KeymasterIf you want to run your batch file automatically when you start your computer you can put the batch file in the startup folder. For example if you want to start a program when your computer starts, simply make your batch file with the right commands and place it in the startup folder.Now If you want to automatically start this batch file when you start your computer then follow these steps:Right click on the Start Menu in the bottom left corner. Choose openOpen the Programs folder, then The Startup Folder.Copy your batch file and paste it in the Startup folder.Now when you start your computer, your program will automatically start for you. You do not even have to press on the batch file to start your program.This works on Windows XP and Vista. But when you make your batch file, make sure you use a command that is compatible with your Operating system. Like xcopy is for Windows XP and robocopy is for Vista.
-
AuthorPosts
- You must be logged in to reply to this topic.