Welcome Guest ( Log In | Register )



3 Pages V   1 2 3 >  
Reply to this topicStart new topic
> Batch Files, Tutorial
RGF
post Jan 11 2005, 09:04 AM
Post #1


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 16
Joined: 2-December 04
Member No.: 1,593



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.
Go to the top of the page
 
+Quote Post
wanhafizi
post Jan 11 2005, 01:56 PM
Post #2


Advanced Member
Group Icon

Group: Members
Posts: 145
Joined: 13-December 04
Member No.: 1,734



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.
Go to the top of the page
 
+Quote Post
TailGrab
post Jan 11 2005, 03:36 PM
Post #3


Newbie [ Level 1 ]
Group Icon

Group: Members
Posts: 6
Joined: 29-December 04
Member No.: 1,903



There are also some fancy things you can do with paramater parsing etc. If you want more information on this topic you can find more info here:
Go to the top of the page
 
+Quote Post
RGF
post Jan 11 2005, 05:09 PM
Post #4


Newbie [ Level 2 ]
Group Icon

Group: Members
Posts: 16
Joined: 2-December 04
Member No.: 1,593



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 huh.gif
Go to the top of the page
 
+Quote Post
NilsC
post Jan 11 2005, 05:44 PM
Post #5


To Err Is Human, To Forgive Divine
Group Icon

Group: Members
Posts: 558
Joined: 24-December 04
From: http://www.ultimatekayakfishing.com/
Member No.: 1,871



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 smile.gif, 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... smile.gif

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

Nils

Go to the top of the page
 
+Quote Post
Ploforia
post Jan 11 2005, 11:49 PM
Post #6


Member - Active Contributor
Group Icon

Group: Members
Posts: 87
Joined: 3-January 05
Member No.: 1,987



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.exe

You could just type
notepad.exe

Provided 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.
Go to the top of the page
 
+Quote Post
Dizasta
post Jan 12 2005, 05:51 AM
Post #7


Member [ Level 1 ]
Group Icon

Group: Members
Posts: 34
Joined: 12-December 04
Member No.: 1,718



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).

CODE
@echo off
@echo off
cls
h:\
goto start
REM  simple menu system

:start
CLS
echo.
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 option
echo ===============================================================================
echo.
rem -- menu choice handled by choice.exe signifies the option selected by the user
rem -- This message take on the values 3 or 2 or 1
rem -- The errorlevel cascade (from highest expected to lowest) makes a goto jump
rem -- 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 Exit
if errorlevel 2 goto BackupDatabase
if errorlevel 1 goto BackupHospital
goto ChoiceError
:ChoiceError
beep
echo Please make a valid option
goto start
:BackupHospital

copy /y c:\progra~1\panacea\*.* h:\backup\panacea\

echo  Your Files have been backed-up.
echo.
pause
goto  start
:BackupDatabase
copy h:\patients\patients.dat h:\backup\patients\
echo Your Database has been backed-up.
echo.
pause
goto  start
:exit
cd\
echo            All Rights Reserved.¸1999/2000 Wyvern-Griffin© Software Inc.
exit
rem 



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.
Go to the top of the page
 
+Quote Post
Herbert
post Jan 14 2005, 05:46 PM
Post #8


Premium Member
Group Icon

Group: [HOSTED]
Posts: 432
Joined: 4-September 04
Member No.: 234



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 cool.gif
Go to the top of the page
 
+Quote Post
qwijibow
post Jan 23 2005, 03:34 AM
Post #9


Way Out Of Control - You need a life :)
Group Icon

Group: Members
Posts: 1,366
Joined: 14-September 04
From: Nottingham England
Member No.: 570



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 has
a 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.
Go to the top of the page
 
+Quote Post
Hercco
post Jan 25 2005, 11:22 AM
Post #10


Super Member
Group Icon

Group: Members
Posts: 595
Joined: 4-September 04
Member No.: 228



QUOTE(qwijibow @ Jan 23 2005, 05:34 AM)

if you reeealy wantto get into scripting on win32, i would recoment installig "cygwin".
it will give you a bash envoronment to code in.
*



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...
Go to the top of the page
 
+Quote Post

3 Pages V   1 2 3 >
Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics


 



- Lo-Fi Version Time is now: 12th October 2008 - 04:52 PM