TechTalkz.com Logo Ask the Experts!

Go Back   TechTalkz.com Technology & Computer Troubleshooting Forums > TechTalkz Member Reviews & Guides > Guides

First Steps to learning Batch Programming!

Guides

 
Comment
Tutorial Tools Display Modes
<!-- google_ad_section_start -->First Steps to learning Batch Programming!<!-- google_ad_section_end -->
First Steps to learning Batch Programming!
Published by 1mAn3rd
30-09-2008
First Steps to learning Batch Programming!

Nowadays, people don't think that much about batch files when it comes to programming. In my opinion, they can be very helpful, and they are extremely easy to make! There are a few basic rules to them though, and this is a guide for people who are taking their first steps into batch file learning.

I will be going over the basic commands in this tutorial. Next to the command will be - text and that text will describe what the command does, so it is not part of the command:
Code:
 echo - Anything that you put after this command will be repeated by the command line. EX: echo Hello world!
pause - This command will sort of pause the actions and wait for you to press a key to continue
help - lists all of the simple commands and what they do
del - deletes the specified file, but you must be in the same directory. EX: del test.txt
cd - Changes the directory. EX: cd "C:\Test batch files\"
cd.. - goes back one directory
cd\ - goes to the root directory, in most cases C:\
echo off - Turns off the option to show the current directory next to where you type...
echo on - Turns on the option to show the current directory next to where you type...
color - Lists all of the color codes. For example, if you wanted a black background with bright green text, you would type this: color a
cls - Stands for clear screen. It wipes the command line so all previous commands that have been executed can't be seen.
dir - Lists all of the files and subdirectories in the current directory
Those are all of the simple commands that I can think of at the moment. Now, lets go on to making your first batch file now that you know a few commands!

First open NotePad. It is a very good text editor when it comes to making batch files. Copy and paste this into notepad:
Code:
echo off
cls
echo Hello World!
echo This is my first batch file!
pause
cls
echo Goodbye...
pause
exit
Can you guess what that would do? If you can, then that's great! That means that you are very good at programming already if you can visualize the program before running it! If you don't know, that's ok, because this may be your first program and no one expects you to know it all!

Lets see what it does for ourselves! In NotePad, go to File > Save As... and in the Name input box, type this: "anyname.bat"
and save it. It MUST have the quotes, or it will not save correctly. You can change the anyname to anything that you want. Now, go to where you saved it and open it! You will see a black box and you can read the on screen text and do what it says! Great! Now, try to remember some of simple commands and you will be able to make easy programs like this.

You're probably wondering; "Well, what can I do with this? It seems useless..."
To answer your question, many things! You can use this as an instructional program for people. However, there is a LOT more to batch programming than just this... but we will go over more advanced stuff later on.

Now that you've got all of that down and remembered, lets go over some variables.

Variables are preset commands that hold information based on their names. These can come in useful when specifying directories or other things. Variables are always wrapped with %. Here is an example of a variable: %username%

Now, when testing commands, you want to open a command line, not make batch files. To open a command line, go to Start > Run > cmd.exe and click Ok. Now, to test that %username% variable and see what it does, type:
echo %username%

You should see that it printed your username on the screen. If not, check your spelling. That variable can come in useful when specifying their desktop directory. Lets say that you wanted to make a batch file show someones desktop directory. We would type this:
echo C:\Documents and Settings\%usermame%

Well, before you dive into using variables, go over the simple commands. Make flash cards and practice They are very useful when it comes to making helpful and useful batch files. In the 2nd edition of this tutorial, I will go over more variables and you will start making useful batch files!

Have fun!
1mAn3rd
Published by
1mAn3rd's Avatar
Advanced Member (250+)
Join Date: Nov 2007
Location: Guilford C
Posts: 396
Rep Power: 154
1mAn3rd will become famous soon enough
Send a message via AIM to 1mAn3rd

Tutorial Tools

Latest 5 tutorials

Featured Tutorials
References
My brain

The Following 4 Users Say Thank You to 1mAn3rd For This Useful Post:
AYAN (17-11-2009), Fithay (19-07-2010), Strider (01-10-2008), Wolfie276 (02-10-2008)
  #1  
By Strider on 01-10-2008, 12:42 PM
Re: First Steps to learning Batch Programming!

Great.. Another interesting Guide from our 'Guider' 1mAn3rd. Good work bro. A few more code examples detailing file operations would be a good bonus
Reply With Quote
  #2  
By 1mAn3rd on 01-10-2008, 07:57 PM
Re: First Steps to learning Batch Programming!

Oh yeah! forgot about file operations Well, I'll go over them in the part 2
Reply With Quote
  #3  
By Wolfie276 on 02-10-2008, 12:42 PM
Re: First Steps to learning Batch Programming!

can i take the number 2? i just wanna write a guide now for some reason xD


EDIT: nah...now i dont want anymore
Last edited by Wolfie276; 02-10-2008 at 01:15 PM..
Reply With Quote
  #4  
By 1mAn3rd on 02-10-2008, 08:20 PM
Re: First Steps to learning Batch Programming!

lol If you feel like doing it again, be my guest. I'm very busy, so I might not get it finished for a while.
Reply With Quote
  #5  
By Wolfie276 on 03-10-2008, 01:10 AM
Re: First Steps to learning Batch Programming!

nah...i just looked at the IF commands and thats a hell of a job writing down
Reply With Quote
  #6  
By 1mAn3rd on 03-10-2008, 08:24 PM
Re: First Steps to learning Batch Programming!

Not exactly...

if "%blank%"=="something" yourcommand
or
if exists filename.extension yourcommand

and so on. It's not that hard
Reply With Quote
  #7  
By Wolfie276 on 03-10-2008, 11:11 PM
Re: First Steps to learning Batch Programming!

i didnt get the errorlevels thats all
Reply With Quote
  #8  
By Fithay on 19-07-2010, 12:18 PM
Re: First Steps to learning Batch Programming!

thank you, this was very helpful
Reply With Quote
  #9  
By rick0909 on 19-08-2010, 12:38 AM
Re: First Steps to learning Batch Programming!

I just ran across this. I know some batch programming, but would like to expand my knowledge. Too bad the poster didn't continue with this thread. This is good info.

At some point a few years ago, I wanted to back up some files each day. Creating a batch program to back them up was not a problem. What I wanted to do was have the batch program also create a folder with a name of the current date, then back up these files to that folder.

I read a lot of books and online information regarding creating a folder within the batch program and never could do it. Maybe batch programming is not capable of creating folders with a new name each time it is run.

Still would be interested, though the need is not as important as it was then.
Rick
Reply With Quote
Comment

Tutorial Tools
Display Modes



< Home - Windows Help - MS Office Help - Hardware Support >


New To Site? Need Help?

All times are GMT. The time now is 04:25 AM.


vBulletin, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO
Copyright © 2005-2013, TechTalkz.com. All Rights Reserved - Privacy Policy
Valid XHTML 1.0 Transitional