Jump to content
xisto Community
RGF

Batch Files Tutorial

Recommended Posts

I'm writing this tutorial for all that don't know what batch files are or how to make them.

 

So lets start at the beginning

 

What is a batch file?

A batch is a list command line instructions that have been batched together ( Kinda like the name says ) into one file. They're not really programs, but they are the backbone of windows... don't believe me see for yourself go into your C:\windows folder there are a lot of .bat \ .sys \ .cfg \ .INF and other types of files. But I wouldn't delete any of them because if you do you may disable your OS

 

So now lets get to some of the commands used in making a batch file:

 

echo - displays a text message in the DOS window like " HI ALL"

if you put @ before it means not to echo that line

 

cls - clears the screen ( DOS window not the monitor)

 

del - deletes a file

 

deltree - its kinda like del but deletes all of the files in a folder

 

copy - this copies the batch file to a directory ( folder )

 

end - ends the script

 

start - starts a program.exe [ example ]

say I wanted to start notepad it would look something like this

Start C:\windows\system32\notepad.exe or just

start notepad.exe ( cool eh? )

 

pause - it pauses the commands until you hit a button

 

call - calls up another batch file from in a folder or somewhere else on the computer

 

color - changes the color of the font and/or the background [ example ]

0 = Black 8 = Gray

1 = Blue 9 = Light Blue

2 = Green A = Light Green

3 = Aqua B = Light Aqua

4 = Red C = Light Red

5 = Purple D = Light Purple

6 = Yellow E = Light Yellow

7 = White F = Bright White

so something like this [ color 0C ]

 

ren - renames a file or folder

 

there are more commands, but I'm still learning them all myself. So anyway lets make something with what we just learned:

 

@echo on

color 2

echo "Hello, I hope you enjoy this tutorial"

copy C:\Program Files\new folder

del C:\Program Files\new folder

pause

cls

Start notepad.exe

End

 

OK save it as [ test.bat ] and look at that your very own batch file

 

what you think?

well that's all for me tonight I hope you enjoyed learning something new.

Share this post


Link to post
Share on other sites

there are programs used to change batch files into real executable files (exe). but nowadays, they didnt used frequently.anyway, now, batch files are used for complex command lines that cannot be completed using any GUI(graphical user interface).an excellence example is linux.

Share this post


Link to post
Share on other sites

ya like wanhafizi was saying if you want to turn your batch files into .exe (programs) there is a good Tool to help you with that.... thnx for the heads up man forgot about that part :)

Share this post


Link to post
Share on other sites

RGE,

nice tutorial. This is a subject that can be expanded a lot. Make it into a series, if you are good at batch files go the next step and show how to work with variables in a batch file.

I feel like a kid again :), first summerjob I had was loading punch cards from a box onto a sorter so we could run the invoicing (summer of 1970)

5 years later I was playing on a Tandy with 2: 5 1/2" drives and was trying to figure out how to setup batch files. I was never any good at it but I used it all the way through dos and windows... :)

Thanks. A +1 bonus point to you for stirring up some memories and for an interesting topic.

Nils

Share this post


Link to post
Share on other sites

Ok. I think this was a good topic to post a tutorial on actually. I was going to post a tutorial on batch files as well - seeing as how most have forgotten of them and the rest never heard of 'em. There actually are some good uses for batch files still believe it or not but I won't go into them now. I think we should make a series of these. I will post one soon - maybe somebody else can post another too - outlining thier uses for batch files now-a-days. I wanted to point out a couple things here though.Echo (command)Echo displays the text following it to the screen. The only way this really works though is if you first turn off the local echo of your batch commands. This is why most who have looked at a batch file are familiar with the Echo Off command. In the preceding example this command was left out - rendering the example a little confusing probably to some as each command was displayed instead of hidden and thus causing commands using echo to be displayed twice.Start (command)I believe this command is relatively useless because in a batch file you can just invoke a program using the command interpreter. So instead of using:Start notepad.exeYou could just type notepad.exeProvided you are in current path that notepad.exe is located or the path has been previously set using the command:Set path=Anyways time to go home more later in the week. Looking forward to another batch tutorial.

Share this post


Link to post
Share on other sites

BATCH files? Ha! Good times. Only one more thing is missing: the Errorlevel menu tutorial. Here is code I wrote like 6 years ago for my computer studies IGCSE class. The wierd letters with accents displayed as corners of the edge of the menu box on a Windows 98 machine (I could not run this on XP though).

@echo off@echo offclsh:\goto startREM  simple menu system:startCLSecho.echo ===============================================================================echo Âş                         Ă==============================Âť                    Âşecho Âş                         Âş Wyvern-Griffin Backup UtilityÂş                    Âşecho.Âş                         Ă==============================Âź                    Âş echo.Âş ============================================================================Âşecho.Âş                                                                             Âş echo Âş                         [1]    Backup Hospital Panacea                      Âşecho Âş                         [2]    Backup Patients' Database                    Âş echo Âş                         [3]    Exit menu system                             Âş                                                                      echo ===============================================================================choice /c:123                           Enter optionecho ===============================================================================echo.rem -- menu choice handled by choice.exe signifies the option selected by the userrem -- This message take on the values 3 or 2 or 1rem -- The errorlevel cascade (from highest expected to lowest) makes a goto jumprem -- to the appropriate GOT destination line (all GOTO destinations are preceded rem -- with colons)rem -- GOTO destination names cannot be too long, I think beyond 15, DOS gets rem -- confuses.if errorlevel 3 goto Exitif errorlevel 2 goto BackupDatabaseif errorlevel 1 goto BackupHospitalgoto ChoiceError:ChoiceErrorbeepecho Please make a valid optiongoto start:BackupHospitalcopy /y c:\progra~1\panacea\*.* h:\backup\panacea\echo  Your Files have been backed-up.echo.pausegoto  start:BackupDatabasecopy h:\patients\patients.dat h:\backup\patients\echo Your Database has been backed-up.echo.pausegoto  start:exitcd\echo            All Rights Reserved.¸1999/2000 Wyvern-GriffinŠ Software Inc.exitrem 

The idea is that the program choice.exe returns the value entered by the user for a menu item, this value is then treated as an errorlevel (sort of a DOS message system) which depending on the integer value lets you jump to different parts of the batch file to complete the desired operation. This menu system really did little except copy files here and there but back then I was impressed to do a menu system in DOS so much that I added it to my project.

Share this post


Link to post
Share on other sites

I remember I did a batch file to clean all the temporary crap files the computers made when students didn't use the correct procedure to log into their student accounts... Saved the teacher in charge of the computers hours of time instead of having her do it manually. Ahhh, those were the good old days :)

Share this post


Link to post
Share on other sites

But why turn a batch file into an executable ?ill call them scripts, cos, well... thats what there called on my OS.the advantage of scripts, is they can be easily edited.compiling them as a binary removes this advantage.secondly, scripts are used to chain other programs together,the other programs will probably be binary, so unless your script hasa very long computational loop (if it doesm your using the wrong language)i cant see any speed advantages to compiling a script.if you reeealy wantto get into scripting on win32, i would recoment installig "cygwin".it will give you a bash envoronment to code in.

Share this post


Link to post
Share on other sites

if you reeealy wantto get into scripting on win32, i would recoment installig "cygwin".

it will give you a bash envoronment to code in.

<{POST_SNAPBACK}>


Exactly. Bash or (t)csh if you prefer more C-styled scripting like I do.

 

Scripting is not just good way to make some routine tasks easier and less time taking, bt also a great way to test things before you program them into a bigger "real" program.

 

And I agree Qwijibow on the compiling scripts matter. "Compiled script" is kind of a oxymoron isn't it. The whole point of sctipts or batch files, if you will, is to have a quickly and easily editable code that does simple thing. If performance is an issue, you should be using something else than script...

Share this post


Link to post
Share on other sites

nice tutorial, I really needed it for launching Java applications on Windows (:unsure:). However, I prefer the BASH Shell Scripts.

For example, I can do system service startup and shutdowns with shell scripts without typing the long command lines over and over again.

echo "Please enter the root password to restart the network"su -c "/etc/init.d/network restart"echo "Done!"

*This code only works on SuSE Linux.

xboxrulz

Share this post


Link to post
Share on other sites

Wow! Another programming language! :unsure: Really, batch files could act as a DOS programming language. Good simple tutorial as well. I might write my own batch file. I think I know how it works now. But what does the command 'rem' do?~souradipm

Share this post


Link to post
Share on other sites

I remember making a batch file to start a service and a few programs when windows started because one program would only run when the other was started and it made it easier, it's also a great way to learn windows shell commands and practise windows scripting. In my experiences the dos prompt will actually hold more power than the simple explorer.exe which makes it a very handy tool if you know how to use it-HellFire

Share this post


Link to post
Share on other sites

I like batch since it can save my time for start/shutdown my server like Apache, MySQL and hMailServer. I use this batch because I have limited RAM so if all of these service run when Windows start-up it can slow down the start-up time.

Here's the batch that I've user, I name it myserver.bat

@echo offset me=myserver.bat - (c) copyright 2006 - Rio Astamalif "%1"=="start" goto startif "%1"=="stop" goto stopif "%1"=="" goto usage:startecho Starting server application server...net start apache2net start mysqlnet start hmailservergoto quit:stopecho sutting down server application...net stop apache2net stop hmailservernet stop mysqlgoto quit:usageecho usage:echo   myserver start/stopecho -------------------------echo note: start to activate the server and stop to shutdown the server.echo .echo .echo %me%goto quit:quitif "%2"=="yes" (  exit)

myserver.bat have 2 argument, the first one is for start/stop the server and the second argument (optional) wheter user want to quit the command prompt or not.
Edited by masterio (see edit history)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.