Introduction: Very Basic Batch Tutorial

Picture of Very Basic Batch Tutorial

If you already know the essentials of Batch the you DO NOT need to read this instructable continue to Not So Basic Batch Tutorial.

NOTE!: Whenever something is in quotation marks ("example"), do not include the quotation marks (if you see that you must just type: example)

Step 1: So, What Is Batch

For those who don't know, a Batch file is a text file containing a series of commands to be executed by Command Prompt (CMD). When a batch file is run, CMD reads the file and executes its commands.

To open your CMD, click START > RUN > type "CMD" > OK
(some of you may need to type "CMD.EXE")
(or you can click START > ALL PROGRAMS > ACCESSORIES > COMMAND PROMPT).

Step 2: Now What?

These are some VERY basic commands you need to know:
(please try out some of these commands in your CMD).

@echo off - This gets rid of that "c:\documents...etc"

echo - This displays a message, (e.g "echo hello" = "hello") because if you type "hello" into CMD it will interpret "hello" as the command

cls - Clears the CMD of all text.

color - Changes the color (type "help color" for a list of colors).

goto - Goes to a particular word in your text (you will learn more later)

pause - Pauses the command prompt and displays the message: "Press any key to continue..."

and there are a few more we will learn about later.

Step 3: But What If I Want to Run a Lot of Commands.

Picture of But What If I Want to Run a Lot of Commands.

That is where BATCH comes in.

Try copying this into your notepad (NOT Microsoft Word) and save as BatchFile.bat
Notepad is in accessories
NB: You must save it as .BAT or it won't work.
P.S (copy BETWEEN the stars, don't include them)
***********************************************************
echo If you type echo before a sentence it displays it as a message!.
echo Wow, thats pretty cool!
***********************************************************
copy that into notepad and save as .bat
Double-click on the .bat file...cool hey!
NOPE. It went too fast...try adding a pause at the end, like this:
***********************************************************
echo If you type echo before a sentence it displays it as a message!.
echo Wow, thats pretty cool!
PAUSE
**************************************************************
There it worked, but its quite hard to read with all those file paths in the way...
thats why we add an "@echo off" to the top;
like this:
***************************************************************
@echo off
echo If you type echo before a sentence it displays it as a message!.
echo Wow, thats pretty cool!
PAUSE
*******************************************************
There! that worked perfectly, you have now written your first batch file!

Step 4: The GOTO Command

Picture of The GOTO Command

Now we will learn how to use the GOTO command.

The GOTO command jumps to a certain part of your text.
Example:
if you type "goto fish"
then it will goto the place that you have typed in ":fish"
NB: you must always have a colon (:), before a word that is going to be GOTO'ed

Example:
you know the drill, copy into notepad and save as a .bat file.
(do not copy the stars)
**************************************************
@echo off
:fish
echo This is only one message being repeated over and over; Press Ctrl+C to stop
goto fish
**************************************************
*Note: Ctrl+C is the universal way of stopping a batch file

What happened was that every time it typed "Press Ctrl...etc"
the GOTO command made it go to the top.
Pretty nifty eh?

Step 5: The START Command

The START command starts something.
Not much to explain here.

For example:
open your CMD and type:
"start www.instructables.com"
This opens your default explorer and goes to the Instructables site.
OR
"start itunes"
OR
You can also open files or folders:
"start desktop\foldername

**NOTE: To access a file\folder which has spaces in the name, put quotation marks (" ") around it.
e.g start "C:\desktop\folder name"

Step 6: Done

Well done! Please continue to the Not So Basic Batch Tutorial for some cool Batch commands!

If you want to try something offline, I recommend getting Learn Batch File Programming! by John Albert, really simple, easy to follow and great if you want to get better!

Comments

admin (author)2008-06-17

Hello, and welcome to the Instructables community! It's great that you've decided to tell the world about something you've made by publishing an Instructable. We just wanted to let you know that your project still needs a little more work if you want it to be well received on Instructables. Projects that don't include certain basic elements tend not to get the attention that they deserve, and so we'd love for you to check out the list below of what makes a successful Instructable. Successful projects on Instructables include: - clearly written details of a finished project with instruction - as many steps as are necessary to explain your project - clear images that you took of your project for most, if not all of your steps - an intro image - proper spelling and grammar - appropriate cautions or safety considerations I'll give you another opportunity to make any final changes to your project before we publish it. Once you're all set to go, please republish your project and send me a quick comment letting me know that you've made some changes. I'll give it a quick final check to make sure you're on the right path, and then remove this note. Thanks for your submission and we hope to see your project published soon!

AJWSayden (author)2017-10-20

Hi guys, I'm trying to make a code similar yet different to a code that you may have already seen on the internet, but there's something wrong with it, can someone figure it out and fix it.

The problem lies within the part where the bot picks a number between 1 and 3

@echo off

color 0b

title MyCoins

goto start

:start

cls

echo Hi there, im a computer.. What is your name?

set /p p1name=

cls

echo Hi, %p1name%, shall we get started

pause

set /a coinsleft=%random%%%10+23

cls

goto regend

:regend

cls

echo Ok, %p1name%, lets start off with a basic tutorial.

pause

goto tut

:tut

cls

echo Coins Left- %coinsleft%

echo.

echo.

echo Those are the coins left

pause

cls

echo Coins Left- %coinsleft%

echo.

echo.

echo You can say a number 1-3, and then that number will be subtracted from the coins left.

pause

cls

echo Coins Left- %coinsleft%

echo.

echo.

echo If it is your turn, and there are 1 or less coins then you lose.

pause

cls

echo Coins Left- %coinsleft%

echo.

echo.

echo Go ahead and give it a number

pause

cls

:start2

set /a coinsleft=%random%%%10+23

cls

echo Coins Left- %coinsleft%

echo.

echo.

set /p input=

if %input%==1 goto succ

if %input%==2 goto succ

if %input%==3 goto succ

cls

echo Invalid selection!

echo.

echo Press any key to go back!

pause >nul

cls

goto start2

:succ

set /a coinsleft1= %coinsleft% - %input%

goto goon

:goon

cls

echo Coins Left- %coinsleft1%

echo.

echo Good, let's give it a real try, playing against me.

pause

goto realthing

:realthing

set /a coinsleft=%random%%%10+23

cls

echo Coins Left- %coinsleft%

echo.

echo.

set /p input=

if %input%==1 goto 1bot1

if %input%==2 goto 1bot1

if %input%==3 goto 1bot1

cls

echo Invalid selection!

echo.

echo Press any key to go back!

pause >nul

cls

goto realthing

:1bot1

set /a coinsleft= %coinsleft1% - %input%

cls

echo Coins Left- %coinsleft%

pause

goto 1bot

:1bot

:LOOP

set NUM=%random:~-1,1%

if %NUM% GTR 3b (

goto LOOP )

goto NEXT

:NEXT

cls

echo I choose, %inputbot%

pause

cls

set /a coinsleft1= %coinsleft% - %inputbot%

goto realthing2

:realthing2

cls

echo Coins Left- %coinsleft1%

echo.

echo.

set /p input=

if %input%==1 goto 1bot2

if %input%==2 goto 1bot2

if %input%==3 goto 1bot2

cls

echo Invalid selection!

echo.

echo Press any key to go back!

pause >nul

cls

goto realthing2

:1bot2

set /a coinsleft= %coinsleft1% - %input%

cls

echo Coins Left- %coinsleft%

pause

goto 1bot3

:1bot3

:LOOP

set NUM=%random:~-1,1%

if %NUM% GTR 3 (

goto LOOP )

goto NEXT

:NEXT

echo I choose, %inputbot%

pause

cls

set /a coinsleft1= %coinsleft% - %inputbot%

goto realthing

Please help.

email at ayden.stacy2@gmail.com

Piguru (author)2017-06-13

I tried running the first file from the book: Learn Batch File Programming!. The CMD gave an error message: 'first.bat' is not recognized as an internal or external command, operable program or batch file.

Kindly advise, please how to resolve this problem.

KaneC5 (author)2017-03-13

Why learn Batch for Windows/DoS? You could learn bigger things such as, HTML or CPP or Python or whatever, I do not mean to discourage any one if you want to learn Batch, But you wont use it as a developer if that's what you're trying to do, i only mean to help i do not mean to offend any one nor upset those trying to learn, Just expressing an opinion, G'day.

Sejma (author)KaneC52017-03-13

Not a bad point, but Batch is how I personally got into programming. It's super easy and understandable and there's no need to download and setup an environment as with Python, or worry about how to compile with C++. As for HTML, it is more of a structural language and doesn't really teach you flow and logic.

As a developer you won't work in it, but I certainly still use it to write smaller scripts on my PC.

Simply put, Batch is easy to just type into a text file and run. There's nothing more to it. That's what makes it so good for the people just starting out. No fuss no mess.

Thanks for your comment :)

Ivan Dennehy (author)2016-04-21

this is so confusing to learn my control prompt is not reconising the comands

maybe if you could spell:)

MatthewR119 (author)Ivan Dennehy2016-04-24

This is working for me and I am a total, total beginner. I would suggest 3 things:

1) Make sure you are running CMD as the Administrator (right click the icon and select 'run as administrator')

2) If you are making CMD files, you need to give them the suffix .cmd

3) Check out my little program for all windows systems; so basic but you can copy/paste it into a note file and give it a .cmd suffix (and run as administrator):

echo off

cls

color 1A

echo Welcome to the Magic System Scanner.

echo Magic will now scan your system.

echo This scan will use a considerable amount of system resources, but you can minimise this window and keep using other programs if you wish. Once the initial process is complete, you will have to type 'y' and 'enter' to restart the system and begin the disk repair. This process may take over an hour.

echo You can close this window at any time without harming your system. However, if you wish to run the Magic System Scanner again, you may need to restart your computer first.

sfc /scannow

chkdsk c: /r

Sejma (author)Ivan Dennehy2016-04-21

More information please:

What are you typing in, what is it saying in response?

Double check that you are typing the commands exactly as you see them.

phoenix701 (author)2016-10-29

awesome and thnks

it is the greatet

vvvvvvv123 (author)2016-09-05

awf

AdityaP81 (author)2016-05-31

Too much clicks and very confusing ... I found some good tutorial on

http://www.tinysoftwares.in/BatchFileTutorial/Index

XavierF9 (author)AdityaP812016-05-31

Thanks

Pratik Sonigra (author)2016-03-26

For useful Batch files visit

http://batch798.blogspot.in/

AawejM (author)2015-10-16

I question is my file name is 1mp3
And my bat file open in pc then open file 1mp3 which extention is give to my bat fil

Noobinha made it! (author)2015-10-13

gg izi *-*

bmlbytes (author)2009-05-13

Ahh! The evil GOTO command!!!!!!!!!!!!!!!!!!!!! Make it go away! Try using this in school for a real programming language. The professor will tell you to just leave and never come back. GOTO is evil in all cases.

Mr_ MikaS (author)bmlbytes2015-09-22

Why.

Well, the trouble is, MS script does not have the command structures that allow programmers to avoid the GOTO statement, such as the WHILE statement. Which I think is unconscionable, since the GOTO statement is indeed evil, and destroys the structure of your programs.

kroq-gar78 (author)bmlbytes2009-08-13

FORK BOMB FTW!!!!

Indeed

knex_mepalm (author)bmlbytes2010-02-13

 It doesn't seem to work for m, I made a batch subsystem which is incomplete and it does not seem to work wah...

tinkerC (author)bmlbytes2009-07-12

Then your professor should not be teaching programming. The goto command must not be over-used, but it stops your programs from being linear. That is very useful, as your professor doesn't realize.

Mr_ MikaS (author)2015-09-18

Cool, I made something like this.
If you want to check it out....
https://www.instructables.com/id/Basic-Batch-command-list/

BILLYREEDY (author)Mr_ MikaS2015-09-22

Ok I am checking yours

Mr_ MikaS (author)BILLYREEDY2015-09-22

Thanks.

MatthewF7 (author)2015-04-05

yes thank you fellow data traveller.ressspek.

NightRaven97 (author)2015-02-13

It says:

^CTerminate batch job? <Y/N>

...and it doesn't go to the top! :(

tabascow (author)2011-11-27

@ echo off
echo :press a key to go to ???
pause
start www.instructables.com

RomanB1 (author)tabascow2014-11-05

Remove the second line as pause will already say press any key to go.

himanshu16 (author)2014-05-24

that's give big help

Regulators (author)2014-04-21

this is cool, but i made my own batch file, that allow you to download file from internet, just like download manager. please visit this instructables

https://www.instructables.com/id/How-To-download-File-on-internet-using-batch-file/

thanks dude!

will421 (author)2010-07-22

I learn my first batch stuff here and and looky me! I understand every command from TAT`s batch... file... mega... subsystem I think? Or is it just system? OH WELL!

sinxxx (author)2010-06-12

ee

Mr. Qwack (author)2009-01-06

u dont need the @ in front of the echo if u type in echo off or echo on

Guard13007 (author)Mr. Qwack2010-04-10

 You do if you want a clean file. What I mean is that the command turning echo off will show up at the top. You don't want one code to show up and then have it be all clean, you either put the @ and make it all clean or forget that command and deal with the extra stuff.

redmaker (author)2009-05-24

coooooool i put color 4d

will421 (author)redmaker2009-06-21

You like pink when your username is redmaker!??!?!??!?!??!??!

will421 (author)will4212009-06-21

Okay that is kinda cool....

Doubleblades85 (author)will4212010-03-24

haha

redmaker (author)will4212009-06-26

no i hate pink but i didnt knolw wat to put so i radomly pressed 2 keys

erikals (author)2009-07-16

EDIT: I found the file, downloaded it, and it works.

ForgetMyDiySorry (author)2009-06-10

Haha, yes indeed. I had ICT class last week(to orientate for next year's electives)
and i wrote a simple program including multiple choice questions and a simple clock ( commands: echo %TIME% %DATE%) and the teacher was like: uhm, if you're gonna use GOTO in my class you can just leave and i'll give you an A because, it IS pretty advanced but still it's not good. haha GOTO = evil!

tinkerC (author)ForgetMyDiySorry2009-07-12

I'm sorry, but if the commands clear the line, type in the time, and wait half a second each time, use the goto command, as it is the way to do it. Loops take too much time.

comno (author)2009-06-01

Can you do this with programs or documents

comno (author)2009-06-01

Thank you much

Beverly5876 (author)2009-05-04

Batch is a new language for me. I'm still not sure what all it is or how far I'll get with it but, I do love a challenge and learning something new. In fact...I always did want to learn a new language. LOL Thanks.

Eirinn (author)2009-01-12

Thank you, just what i needed for some basic shortcut stuff!

HardCoreHacker (author)2008-07-09

Great!!

Sejma (author)HardCoreHacker2008-07-10

ty man

About This Instructable

351,410views

46favorites

License:

More by Sejma:Very Basic Batch TutorialNot So Basic Batch TutorialSome Cool Batch Applications
Add instructable to: