Jul 4, 2009
Pages: 1, 2, 3

Batch Files - Tutorial

free web hosting

Read Latest Entries..: (Post #25) by iGuest on Apr 22 2009, 06:19 AM.
Opening Multiple Files simultaneously from batch file Batch Files This was mentioned earlier, but not explained. If you execute another application from a batch file it will halt since it then waits for input from this application. To just start it and move on use the prefix "start". -reply by iGuests little helper  ...
read more.
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Free Web Hosting > Computers & Tech > How-To's and Tutorials > Programming > MISC (not applicable to above Programming Categories)

Batch Files - Tutorial

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

 

 

 


Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

TailGrab
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:

Comment/Reply (w/o sign-up)

RGF
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

Comment/Reply (w/o sign-up)

NilsC
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

Comment/Reply (w/o sign-up)

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

 

 

 


Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

Herbert
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

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

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

Comment/Reply (w/o sign-up)

Latest Entries

iGuest
Opening Multiple Files simultaneously from batch file
Batch Files

This was mentioned earlier, but not explained. If you execute another application from a batch file it will halt since it then waits for input from this application. To just start it and move on use the prefix "start".

-reply by iGuests little helper

 


Comment/Reply (w/o sign-up)

iGuest
Creating User Defined Folders using DOS
Batch Files

Hi there,

 

I was wondering if anyone had any code for a user to run a bat file and then have to enter a folder name to be created.

 I have seen several peices of code but nothing really stood out to me, I am a novice in DOS.

Any Help would be fantastic

Thank you

andrew


 


Comment/Reply (w/o sign-up)

Darasen
The & is telling the command line parser that a new command is beginning. Thus your sytax is not going to work.

The easiest solution would be to rename rhw offending folder to something friendlier to the batch file syntax.

Comment/Reply (w/o sign-up)

iGuest
I want to create a batch file the creates a Folder called= Themes & MODS
Batch Files

I am trying to create a batch file to copy loads of files to a specific Folder that will be named

Themes & MODS 

When I run the batch file it does not seem to recognize the Syntax> Themes & MODS and says an error trying to create the file?

The structure I'm using is very simple PLEASE SEE BELOW...

:BEGIN
@ECHO OFF
ECHO. **************************************
ECHO. **    PSPWillys PSP Custom waves 2009             *   
ECHO. ** For XMBeditor v2 Running CFW 3.90m33-2      *   
ECHO. **************************************
ECHO.
ECHO.
ECHO.
ECHO.
ECHO.
Md  F:\Themes&Mods\Mods\system_plugin_bg
Md  F:\Themes&Mods\Mods\system_plugin_bg\Allsort
Copy Allsort\Preview.Jpg   F:\Themes&mods\mods\system_plugin_bg\Allsort

This is just a sample of the batch code I'm writing...Is the problem with the "&" syntax?

If it is can someone point me in the right direction

ChrisUK


Comment/Reply (w/o sign-up)

iGuest
Opening Multiple Files simultaneously from batch file
Batch Files

I would like to open multiple files from a batch file.My code is

Call file x
Call file y
And so on

But the files open one by one and not until I close the first file the second does not open. Kindly advise

-question by saif

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Pages: 1, 2, 3

See Also,

*SIMILAR VIDEOS*
Searching Video's for batch, files, tutorial
advertisement



Batch Files - Tutorial

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com