Jump to content


Photo

Batch File Coding Tutorial 1


  • Please log in to reply
No replies to this topic

#1 Elric

Elric

    Designer

  • Hosted
  • 2,857 posts
  • Projects:Middle-Earth Expanded
  •  Coder
  • Donated

Posted 31 March 2012 - 09:12 PM

Batch file coding is not all that difficult. All it requires is the standard windows notepad application located under “Start/All Programs/Accessories/Notepad”. It is good to have some background knowledge of the notepad application, but, that is about it. So let’s begin with what batch files are.

A batch file is the name given to a type of script file, or a text file containing a series of commands to be executed by a command prompt that windows has built in.

You can use a command prompt to execute commands. Once a command is entered the prompt does what it’s supposed to do. It can make navigating files easier and harder at the same time because if you don’t know what you are doing it can make things a whole lot harder.
To start are first tutorial we will do something that most tutorials have these days called a “Hello World!” test. To do this open up notepad and type this code:

@echo off
echo Hello World!
@pause

Then go to “Save as…” and save it to your desktop as “Hello World!.bat”. Then close notepad and click the file on your desktop. It should bring up a black window saying Hello World! And then it says press any key to continue. Press a key and it should close. To edit it right click and press edit and it should open in notepad.

Now let’s go over the code you typed in to the .bat file.

@echo off tells the command prompt to not have anything before it. Take out @echo off and save it and run it and it should have more than just Hello World! Now if you add it again it should be regular.

Echo Hello World tells the prompt to echo out Hello World in the prompt itself.

@pause pauses it so that you have to press a key to continue. If you don’t have this in then it will open quickly and then close, before you can even read the message.

Now if you want to have the windows show multiple lines of code then add this to it:

@echo off
echo Hello World!
echo This is line 2!
@pause

See I added “This is line 2!” Now when you save it and click it, it will say:

Hello World!
This is line 2!
Press any key to continue…

If you want it to show only when you press any key then have the code arranged like this:

@echo off
echo Hello World!
@pause
echo This is line 2!
@pause

I added another pause in between Hello World! and This is line 2!
Save it and run it and it should say:

Hello World!
Press any key.
Once pressed it should be
Hello World!
This is line 2!
Press any key…

As you can see batch file coding can be VERY easy. Tutorial 2 should be out in a few says, please be patient. Also, thanks for reading!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users