This is not designed for reading once. Print it or save on your disk. But it is added days by days.
If you have any experience, please send it to me and we will have a library of experience.
Batch files
Pascal for DOS
C for DOS
Assembly
Visual Basic
VB script & Java script
Batch files
o you remember it? The .BAT files.
I knew it 2 months after I had touched the computer.
It is not a real programming language, but I used it
to write a game "one two three" (and of course, computer always
won because it doesn't support random). I thinks there're
reasons to used batch files to do simple works (as do a list of
commands, plus a bit favour of programming language),
but it undoubtedly can be replaced by script files.
Here're some explain why I like batch files. The first is basic knowledge,
then some experiences
- Filter symbols: are
>
, >>
, <
, |
The larger symbol is used to direct the output device
EX 1: create a file LIST.TXT contains directory structure of A:\
EX 2: show "Press a key" and wait for a key
ECHO Press a key
PAUSE > NUL
this not show the default message of pause
command,
because the output is directed as nul
EX 3: create file ALL.TXT includes contents of FILE1.TXT and FILE2.TXT
TYPE FILE1.TXT > ALL.TXT
TYPE FILE2.TXT >> ALL.TXT
(the >>
appends new content after old file)
With this situation, you can do in a different way: COPY FILE1.TXT ALL.TXT
and then: COPY ALL.TXT + FILE2.TXT
The smaller symbol is used to direct the input device
EX 4: automatically format a floppy diskette
- First, you edit a file C:\FORMAT.INP with lines below (line numbers for reference only):
(note that this file has 4 line, and the first and last line are blank)
- Then, type this command:
FORMAT A: < C:\FORMAT.INP
- The disk in drive will be automatically formatted and name hai nam
without any question.
- Of course, you can change to format your HDD, but don't try now!
The last symbol is |
EX 5: delete all file in directory C:\REMOVE
The difficulty is the command DEL C:\REMOVE
will confirm you.
There're at least three ways to do. I mention only two here, the last one
is for the structure FOR IN DO
The first method is like the EX4: you make a file DELALL.INP contains a letter N
and a blank line, then you used DEL C:\REMOVE < DELALL.INP
The second, much faster, is the command:
(The symbol |
has ASCII code #124)
- The percentage symbol: can be called as variable
You use %0 to %9 to get commandline parameters.
EX 6: look at this batch file
1: @ECHO OFF
2: ECHO The file name is %0
3: ECHO The first parameter is %1
4: ECHO The second parameter is %2
If you want the command interpreter treats symbol %
in batch file
like in command line, write %%
. Look at this:
1: ECHO %%
2: FOR %%f IN (*.*) DO ECHO %%f
In command line, to do the same, you enter:
ECHO %
FOR %f IN (*.*) DO ECHO %f
- Specified commands for Batchs
IF
check a condition
There're three syntax for IF (IF do the command if condition is true; with NOT,
the command will be done if condition if false)
IF [NOT] ERRORLEVEL number command
true if errorlevel >= number
IF [NOT] string1==string2 command
true if string1 and string2 are the same
IF [NOT] EXIST filename command
true if file filename exists
Here're three examples:
EX 7: most of programs return error other than zero if there are abnormal
events. This will check it:
IF ERRORLEVEL 1 ECHO There are some errors
EX 8: this is a very simple check password method:
IF "%1"=="okk" ECHO Password correct!
(notice the case-sensitive: OKK or OKk are different from okk)
EX 9: verify a file exists or not
IF EXIST C:\CONFIG.SYS ECHO File c:\config.sys exists
IF EXIST C:\BATCH\NUL ECHO Folder c:\batch exists
(notice that file NUL (and CON, PRN...) are dummy devices, they exist in all folder)
GOTO
go to a label in batch file
EX 10: a batch file
1: @ECHO OFF
2: XCOPY C:\GAMES\WAR2\*.SAV C:\BACKUP
3: IF ERRORLEVEL 1 GOTO error
4: ECHO All saved game were copied successfully
5: GOTO end
6: :error
7: ECHO There are some error(s)
8: :end
CALL
call a new batch file then continue the old
EX 11: two ways to call a batch (assume that there's a file DO-IT.BAT)
1: @ECHO OFF
2: ECHO This will run DO-IT.BAT
3: CALL DO-IT
4: ECHO Now continue...
5: ECHO And now, run DO-IT.BAT again
6: DO-IT
7: ECHO You won't see this line
SHIFT
shift all parameters into left
EX 12: a practical example, this deliver files to many places.
Assuming this batch file named DELIVER.BAT, the syntax is
DELIVER files place1 [place2 [place3 [...]]]
Without SHIFT command, you can use only 8 places (%2 to %9)
1: @ECHO OFF
2: SET filesrc=%1
3: :copyfile
4: IF "%2"=="" GOTO end
5: COPY %filesrc% %2 > NUL
6: SHIFT
7: GOTO copyfile
8: :end
9: SET filesrc=
FOR IN DO
loop command
Syntax:
FOR %variable IN (set) DO command parameters
EX 13: this one-line batch equivlent to file DELIVER.BAT, but 8-place limited
1: FOR %%place IN (%2 %3 %4 %5 %6 %7 %8 %9) DO IF NOT "%%place"=="" COPY %1 %%place
If you don't want to do this again, you needn't a batch. And you command is
FOR %place IN (C:\TARGET1 C:\TARGET2) DO COPY C:\AUTOEXEC.BAT %place
(Moreover, this can use more than 8 places, but you get 127-character-per-commandline limitation)
CHOICE
an external command, to get answer or comfirmation
Syntax:
CHOICE [/C[:]choices] [/N] [/S] [/T[:]c,nn] [text]
- If
choices
omitted, it is YN as default
/N
won't display choices
/S
case-sensitive choices
/T[:]c,nn
default choice to c
after nn
seconds
text
prompt text
Type CHOICE /?
to know the detail.
EX 14:
01: @echo off
02: ECHO 1. WarCraft II 2. RoadRash 3. FIFA 2000
03: choice /c:123 Which game do you like best
04: IF ERRORLEVEL 3 GOTO 3
05: IF ERRORLEVEL 2 GOTO 2
06: ECHO WarCraft II
07: GOTO end
08: :2
09: ECHO RoadRash
10: GOTO end
11: :3
12: ECHO FIFA 2000
13: :end
- Some experiences
Read carefully the basic knowledge before (there're also my experiences
in 14 examples).
EXP 1: ignore case-sensitive
1: @ECHO OFF
2: SET temp=%path%
3: SET path=%1
4: SET input=%path%
5: SET path=%temp%
6: SET temp=
7: ECHO Your %%1 input is %input%
EXP 2: get current date and time
Go to the top
Pascal for DOS
t's my favourite!
I knew Turbo Pascal one month after I had known batch file.
I've got version 6.0 in a 1.2MB disk. The later (and the last, too) version
is 7.0, get error 200: Division by zero
with fast Pentium
(such as Celeron 233 MHz and faster) (this is only overflow error).
If you get error with it, get the patchs right now: one for compiled .EXE
and one for TURBO.TPL. If you use graph, you
may need some .BGI and .CHR (some of them are from one other than TP)
- My experiences
- My programs (with source)
Many of them (which I had been written for 5 years) lost
when my 1.2GB HDD died in 8/1998 (back up your data, please). Then
I started at the beginning for a few months and stopped. I rarely use Pascal now.
My library text, graph, maths, XMS... integrated. Download
Karaoke player it an 'instant noodle' program
to play karaoke with computer speaker. Download
Welcomer a small example of Pascal resident program. Download
FontEdit 2.0 edit 8x16 text font. One of top-7 programs in national non-professional
IT contest in 1997. I lost the source now! But I give a copy of my source to Phung Khoi Duy Minh (11 Le Hong Phong high school, HCM City).
Download file EXE
Go to the top
C for DOS
Assembly
Visual Basic
VB script & Java script