-------------------------------------------------------------------- | _ _______ __ _ _ __ __ _ __ _ _ _______ __ _ _ __ | | | '_ _ |/ _' | '_ \ / _` |/ _' | | '_ _ |/ _' | '_ \ | | | | | | | | (_| | | | | (_| | (_| | | | | | | | (_| | | | | | | |_| |_| |_|\__._|_| |_|\__. |\__._|_____|_| |_| |_|\__._|_| |_| | | __/ | |_____| | | |___/ | |Contact info at bottom -2002 | -------------------------------------------------------------------- Batch File Programming Tutorial -------------------------------------- I basically decided to make this tutorial because I got bored. Batch files can be very useful and also very pointless, but who cares? I'm going to teach you the basics to slightly advanced. Batch file programming is not really programming. It's just a series of DOS commands that your computer can execute very quickly. If you have any comments to make to me just e-mail or AIM me. Contents ----------- 1 - The Starting Points 1.1 - @echo off 1.2 - echo 1.3 - Example 2 - Some Simple Commands 2.1 - pause, pause >nul 2.2 - cls 2.3 - Goto, :Label 2.4 - Example 3 - File Related Commands 3.1 - Copy 3.2 - Ren 3.3 - Del, Erase 3.4 - md, mkdir, rd 3.5 - Example 4 - Slightly More Advanced Commands 4.1 - Parameters 4.2 - if exist, if not exist 4.3 - New files 4.4 - Choice 4.5 - Example 5 - Misc Commands 5.1 - cd.., cd\, cd 5.2 - Type 5.2 - @Time, @Date 5.3 - Call 5.4 - dir, dir/p/w, dir * 5.5 - Example 6 - Notes to Remember 6.1 - Important Things 6.2 - Cool Things 1 - The Starting Points --------------------------- 1.1 - @echo off The very first thing you must do at the start of a batch file is type '@echo off' (without the quotation marks, durrr). If you don't type this, you will get the batch files directory come up all the time when you run the batch file. 1.2 - echo The simpliest thing in a batch file to do is display a line of text. All you have to do is type 'echo Text here'. If you don't understand, then, well, uhhh..., go home. If you want to leave a blank line in your batch file you have to type 'echo.' 1.3 - example Now let me show you a basic batch file using these three points: @echo off echo This is my first batch file echo. echo Bye! 2 - Some Simple Commands --------------------------------- 2.1 - pause, pause >nul If you want to stop your batch file from running and continue when the user presses a key then just type 'pause'. Yes it's that easy. But, when you do that 'Press any key to coninue. . .' appears, and that can be frustraiting. There is a small bug that gets rid of that. Instead of typing 'pause', type 'pause >nul'. 2.2 - cls Eventually your batch files may become flooded with horrible text. To clear the screen and get rid of everything (when your running the batch file) type 'cls' (when your coding). That's it. 2.3 - Goto, :Label Later on you may want your batch files to go back/forward to another point in your batch file. First you must label the points you want to goto (if you don't understand then check the example). To do this just type ':LabelName'. Then, to make your batch file goto the point you must type 'goto LabelName'. 2.4 - Example Here's an example using these points: @echo off echo This is my first batch file echo. pause >nul cls :Label1 echo Time to repeat for ever pause >nul goto Label1 3 - File Related Commands -------------------------------- 3.1 - Copy To make your batch file copy a different file then type 'copy FileName NewFileName'. Check the example for full understanding. 3.2 - Ren It works the same to rename files. Type 'ren FileName NewFileName'. Kinda easy. 3.3 - Del, Erase If you want to delete a file just type 'del FileName' or 'erase FileName'. Note:When your entering file names you can include the path 3.4 - md, mkdir, rd To create new directories then type 'md NewDirectoryName' or 'mkdir NewDirectoryName'. To remove directories type 'rd DirectoryName' 3.5 - example A more complex example using all of the commands: @echo off echo This is my first batch file echo. echo Smash your keyboard to continue. . . pause >nul cls md C:\Pleb copy C:\autoexec.bat C:\Pleb\autoexec.bat echo Check C:\Pleb\autoexec.bat (don't delete it). Press a key. . . pause >nul ren C:\Pleb\autoexec.bat C:\Pleb\Readme.txt echo Now check it. Press a key. . . pause >nul del C:\Pleb\Readme.txt rd C:\Pleb echo Now it's gone (and the directory). Press a key. . . pause >nul :start cls echo You can close me now pause >nul goto start 4 - Slightly More Advanced Commands ---------------------------------------------- 4.1 - Parameters You can make your batch file check for specific parameters. To do this just type 'If "%1"=="Parameter here" Command here'. For example, if your batch file was called temp.bat and you had 'If "%1"=="Hello" echo Hello to you too', when somone types 'temp.bat Hello' (in DOS), 'Hello to you too' would appear on screen. Check the example if you don't understand. 4.2 - if exist, if not exist To make your batch file check if a file exists/doesn't exist is actually quite easy. Type 'if exist FileName Command' or to check if a file doesn't exist 'if not exist FileName Command' You can include paths with the file names. 4.3 - New files You can get your batch file to make files (such as txt and even another batch!). It's relatively quite easy once you get the hang of it. Type 'echo FileContents > FileName.FileExtension'. If you want to get onto the next line in the file type 'echo FileContents2 >> FileName.FileExtension'. Notice how now there are 2 '>'s which means your on the 2nd line. For the 3rd line type 3 '>'s and so on. Check the example if your still not sure. 4.4 - Choice This next bit is quite hard to figure out at first, but it eventually does become come easy. You can make the user have a choice. For example, you could say press 1 to exit or press 2 to continue. Type 'choice /c:Choices'. Your choices can only be one digit and no spaces between them. This is where it gets complicated. Count up how many choices you have. Then type 'If errorlevel Number Command' as many times as you counted up on new lines. Where Number is you have to type what number line your on but going downwards. The command for number 1 would be the command for your first choice. So far you probably haven't got the slightest clue what I'm talking about. It's best that you check the example and read it carefully. 4.5 - Example The example on the advanced commands: @echo off If "%1"=="Cheat" goto cheat If not exist C:\autoexec.bat echo Hey! Did you know that autoexec.bat don't exist!? If exist C:\autoexec.bat echo autoexec.bat exists! echo. echo What would you like to do today? echo 1 - Get stuck in a neverending loop echo 2 - Have all the money in the world echo 3 - Make readme.txt echo 4 - Quit choice /c:1234 if errorlevel 4 goto end if errorlevel 3 goto mkfile if errorlevel 2 goto money if errorlevel 1 goto loop :loop cls :startloop echo Looping for ever. . . pause >nul goto startloop :money echo Tough, it's my money! goto end :mkfile echo Making readme.txt . . . echo You are reading this file > readme.txt echo Did you know that? >> readme.txt echo I bet you didn't >>> readme.txt echo You smell >>>> readme.txt cheat: echo You shouldn't cheat, you cheater! :end 5 - Misc Commands ----------------------- Here are some important and not so important commands that I missed 5.1 - cd.., cd\, cd If you want to change the current directory that your batch file is working in there are several ways of doing this. (1) To go back a directory type 'cd..' (2) To go into a directory type 'cd Directory' (3) To go to the current hard drives first directory type 'cd\'. Check the example if your not to sure of how this works. 5.2 - Type You can make your batch file display the contents of another file by typing 'type FileName' 5.2 - @Time, @Date You can make the user edit the computers current time by typing '@time' and you can change the date by typing '@date'. 5.3 - Call To load another file within your batch file (once the file you chose to open has ended it will continue loading your batch file) just by typing 'call FileName'. 5.4 - dir, dir/p/w, dir * To display a list of all the files in the current directory, type 'dir', but if the list is longer than the screen type 'dir/p/w' to view the files in a better, easier way. Also if you wish to only view a certain type of file type 'dir *.FileExtension'. For example 'dir *.exe'. 5.5 - Example Yet another example (but only on changing your current directory and 'dir'): @echo off echo All the files in this directory: dir/p/w pause echo All the files in this drives directory cd\ dir/p/w pause echo All the .exe in the windows directory cd windows dir *.exe 6 - Notes to Remember ---------------------------- 6.1 - Important Things Try searching your hard drive for batch files and reading them, you may learn something new. But make sure you don't edit any of them, some of them are very important and may damage your computer. If you ever make something potentially dangerous, it's best not to try it out. Anything you do will not be none of my responsibility. Oh yeah, if you don't know (and you should) batch files extension are .bat. So when you make a batch file save it as 'FileName.bat'. Or if you search for batch files, search for *.bat 6.2 - Cool Things You can get software which turns your batch files into exe's! You can download one at my site. You can use wierd symbols in your batch file, and when you run it, they change. This is not important but there are some cool lines which you can draw things with. ---End--- This concludes my tutorial on batch file programming. There may have been some more important commands I missed, but I shall try to add them in future updates. manga_man - 2002 AIM : manga1man e-mail : manga_man300@hotmail.com homepage : http://www.manga-man.cjb.net