· 9 years ago · Jan 07, 2017, 06:08 PM
1@echo off
2:: Program made by Hugo7 for The ZT2 Round Table.
3:: Member of the French forum batch.xoo.it
4:: All lines that start with a "::" are explanations for the code,
5:: But lines that start with only one ":" are labels used by "goto" commands, like a call for a function.
6:: A function is a part of the code between ":something" and "goto:EOF"
7
8:: Technical details + variables
9set version=1.0
10setlocal enabledelayedexpansion
11mode con lines=40 cols=91
12set title_rad=RTZI zoo managing software version %version% - made by Hugo7 -
13
14:: If the list does not exist, we create it
15if not exist animals.db (
16#Syntax : species-males-females-youngMales-youngFemales>animals.db
17#"\" near species names are here to avoid confusions like "panda" and "pandared" because one contains the other. So now we have "\panda\" and "\pandared\" and it's all fine.>>animals.db
18)
19
20
21:: Main function begins
22:1
23title %title_rad% Type a command...
24
25:: Resets some variables and clears the screen - confirm mustn't be set to 0 or 1 for now.
26set cmd=0
27set count=0
28set confirm=2
29set cancel=0
30set species=0
31cls
32
33:: User input
34echo ========================================================================================
35echo = Available commands : list, +, -, search, backup =
36echo = Remember to use correct names for animals. Example : "Red panda" will be "pandared". =
37echo = Please use "help ^<command^>" to get the syntax of a command. =
38echo ========================================================================================
39echo Your command :
40set/p cmd=/
41
42:: Counts the number of arguments given by the user
43for %%C in (%cmd%) do set /a count=!count!+1
44set/a count=%count%-1
45
46:: Decomposes the input to take all arguments
47for /f "tokens=1-6 delims= " %%A in ("%cmd%") do (
48set main=%%A
49set arg1=%%B
50set arg2=%%C
51set arg3=%%D
52set arg4=%%E
53set arg5=%%F
54)
55
56:: Calls the corect function and send it the given arguments - please see the functions below to read explanations for the arguments.
57if "%main%"=="list" call :list
58if "%main%"=="search" call :search %arg1%
59if "%main%"=="+" call :add %arg1% %arg2% %arg3% %arg4% %arg5%
60if "%main%"=="-" call :remove %arg1% %arg2% %arg3% %arg4% %arg5%
61if "%main%"=="help" call :help %arg1%
62if "%main%"=="backup" call :backup
63
64:: if incorrect cmd, then returns to the menu - main function ends
65goto 1
66
67::----------------------
68
69:list
70:: Arguments : no need of any argument.
71title %title_rad% List of all your animals!
72echo.
73echo Lists all animals.
74echo Press any key to continue...
75pause >nul
76
77:: Sorts the list by alphabetic order
78sort animals.db /o animals.db_
79del/q animals.db
80ren animals.db_ animals.db
81
82:: Creates the temp file
83echo List of your animals : >animals.tmp
84
85:: Fills it
86for /f "tokens=1-5 delims=- eol=#" %%L in ('type animals.db') do (
87set species=%%L
88set species=!species:\=!
89echo !species! : %%M males, %%N females, %%O young males and %%P young females >>animals.tmp
90)
91
92:: Clears the screen and show the 'decoded' list.
93cls
94type animals.tmp | more
95
96echo.
97echo Press any key to return to the menu...
98pause >nul
99
100:: Deletes the temp file and returns to the menu
101del /q animals.tmp
102goto:EOF
103
104::----------------------
105
106:search
107:: Arguments : only one needed argument : just what the user searches for - only one word
108if %count%==0 (
109 echo ERROR : please provide an argument.
110 echo Press any key to return to the menu...
111 pause >nul
112 goto 1
113)
114title %title_rad% Search tool
115echo.
116for /f "tokens=1-5 delims=-" %%A in ('type animals.db ^| find /i "%1"') do (
117set species=%%A
118set species=!species:\=!
119echo !species! : %%B males, %%C females, %%D young males and %%E young females.
120)
121echo.
122echo Press any key to return to the menu...
123pause >nul
124goto:EOF
125
126::----------------------
127
128:add
129:: Arguments : (5) : species, number of males, nb of females, nb of young males, nb of young females
130if not %count%==5 (
131 echo ERROR : please provide 5 arguments
132 echo ^(species name, nb of males, nb of females, nb of young males and nb of young females^).
133 echo Press any key to return to the menu...
134 pause >nul
135 goto 1
136)
137title %title_rad% Adding animals to the list
138echo.
139echo Adding [%2 males, %3 females, %4 young males and %5 young females] %1 to the list...
140
141:: This is a function IN a function...
142:confirm1
143echo Please confirm by typing "1" or type "0" to cancel and return to the menu.
144set/p confirm=^>
145if "%confirm%"=="1" goto letsadd
146if "%confirm%"=="0" goto 1
147:: if it's not 1 or 0, then re-ask the question
148goto confirm1
149
150:: This is an other function IN a function...
151:letsadd
152:: Takes all the numbers in order to increment them
153for /f "tokens=1-5 delims=- eol=#" %%A in ('type animals.db ^| find /i "\%1\"') do (
154set nbmales=%%B
155set nbfemales=%%C
156set nbymales=%%D
157set nbyfemales=%%E
158)
159
160:: Increments (%2 = the second argument = nb of males to add, etc) some nb can be incremented by 0
161set /a nbmales=%nbmales%+%2
162set /a nbfemales=%nbfemales%+%3
163set /a nbymales=%nbmyales%+%4
164set /a nbyfemales=%nbyfemales%+%5
165
166:: Updating the list : all animals that are NOT the given animal are transfered from the list to
167:: a temp file and then the modified line is added. Then the list is sorted by alphabetic order.
168for /f "tokens=1,* delims=-" %%Y in ('type animals.db') do (
169if not %%Y==\%1\ (
170>>animals.db_ echo %%Y-%%Z
171)
172)
173>>animals.db_ echo \%1\-%nbmales%-%nbfemales%-%nbymales%-%nbyfemales%
174del /q animals.db
175sort animals.db_ /o animals.db
176del /q animals.db_
177color a
178echo.
179echo Operation completed!
180echo Press any key to return to the menu...
181pause >nul
182color 7
183goto:EOF
184
185::----------------------
186
187:remove
188:: Arguments : (5) : species, number of males, nb of females, nb of young males, nb of young females
189if not %count%==5 (
190 echo ERROR : please provide 5 arguments
191 echo ^(species name, nb of males, nb of females, nb of young males and nb of young females^).
192 echo Press any key to return to the menu...
193 pause >nul
194 goto 1
195)
196title %title_rad% Removing animals to the list
197echo.
198echo Removing [%2 males, %3 females, %4 young males and %5 young females] %1 to the list...
199
200:: This is a function IN a function...
201:confirm2
202echo Please confirm by typing "1" or type "0" to cancel and return to the menu.
203set/p confirm=^>
204if "%confirm%"=="1" goto letsrem
205if "%confirm%"=="0" goto 1
206:: if it's not 1 or 0, then re-ask the question
207goto confirm2
208
209:: This is an other function IN a function...
210:letsrem
211:: Takes all the numbers in order to decrement them
212for /f "tokens=1-5 delims=- eol=#" %%A in ('type animals.db ^| find /i "\%1\"') do (
213set nbmales=%%B
214set nbfemales=%%C
215set nbymales=%%D
216set nbyfemales=%%E
217)
218
219:: Decrements (%2 = the second argument = nb of males to remove, etc) some nb can be decremented by 0
220set /a nbmales=%nbmales%-%2
221set /a nbfemales=%nbfemales%-%3
222set /a nbymales=%nbmyales%-%4
223set /a nbyfemales=%nbyfemales%-%5
224
225:: Verifies if updated numbers are at least equal to 0 else cancels the operation + msg
226if not %nbmales% GEQ 0 goto err
227if not %nbfemales% GEQ 0 goto err
228if not %nbymales% GEQ 0 goto err
229if not %nbyfemales% GEQ 0 goto err
230
231:: Verifies if all updated numbers are equal to 0 : if all = 0, the specie is removed from the list
232if %nbmales%==0 if %nbfemales%==0 if %nbymales%==0 if %nbyfemales%==0 set cancel=1
233
234:: Updating the list : all animals that are NOT the given animal are transfered from the list to
235:: a temp file and then the modified line is added. Then the list is sorted by alphabetic order.
236for /f "tokens=1,* delims=-" %%Y in ('type animals.db') do (
237if not %%Y==\%1\ (
238>>animals.db_ echo %%Y-%%Z
239)
240)
241:: %cancel% is set when all categories (males, ...) are equal to 0, so there is no animal of the selected
242:: species so we remove the entry from the list
243if not %cancel%==1 (
244>>animals.db_ echo \%1\-%nbmales%-%nbfemales%-%nbymales%-%nbyfemales%
245)
246del /q animals.db
247sort animals.db_ /o animals.db
248del /q animals.db_
249
250color a
251echo.
252echo Operation completed!
253echo Press any key to return to the menu...
254pause >nul
255color 7
256goto:EOF
257
258:: Error function for the function above
259:err
260cls
261color c
262echo.
263echo ALERT : incorrect given number : you don't have enough animals.
264echo Your zoo will be like : %nbmales% males, %nbfemales% females, %nbymales% young males, %nbyfemales%, young females.
265echo A number of animals can't be a negative number!
266echo ^> The operation was cancelled. ^<
267echo.
268echo Press any key to return to the menu...
269pause >nul
270color 7
271goto 1
272
273
274::----------------------
275
276:help
277:: Arguments : just the command the user wants to learn the syntax
278title %title_rad% Wanna get some help?
279if %count%==0 (
280 echo ERROR : please provide an argument.
281 echo Press any key to return to the menu...
282 pause >nul
283 goto 1
284)
285echo.
286echo Syntax of the command "%1" :
287if %1==list (
288echo /list
289echo Lists all animals.
290)
291if /i %1==+ (
292echo /+ ^<species^> ^<nb of males^> ^<nb of females^> ^<nb of young males^> ^<nb of young females^>
293echo If the species already exists in the list, then the number^(s^) is/are
294echo incremented by the given number^(s^), else, the species is added to the
295echo list with the given number^(s^).
296echo Example :
297echo /+ pandared 1 0 0 0
298echo -^> adds one adult male red panda to the list
299)
300if /i %1==- (
301echo /- ^<species^> ^<nb of males^> ^<nb of females^> ^<nb of young males^> ^<nb of young females^>
302echo If the species already exists in the list, then the number^(s^) is/are
303echo decremented by the given number^(s^). An error message is shown if a
304echo number reaches a negative value ^(ex. if you want to remove 3 males
305echo while you have only 1 in the list^).
306echo Example :
307echo /- pandared 1 0 0 0
308echo -^> removes one adult male red panda to the list
309)
310if /i %1==search (
311echo /search ^<what^>
312echo Shows the line^(s^) in the list that contain^(s^) the provided word.
313echo It's coveniant to see what is the exact denimination for an animal.
314echo Example :
315echo /search pandared
316echo -^> Shows the detailled numbers of red pandas ^(males, females, etc.^)
317)
318if /i %1==backup (
319echo /backup
320echo Creates a backup of the list, with the date and the hour in the backup file's name.
321)
322echo.
323echo Press any key to return to the menu...
324pause >nul
325goto:EOF
326
327::----------------------
328
329:backup
330:: Arguments : none
331:: The variable %time% contains the hour,min,sec,milliseconds but also some ":" we need to remove because a file's name can't contain this. Replaces ":" by "-".
332for /f "tokens=1-3 delims=:" %%N in ("%time%") do set backuptime=%%N-%%O-%%P
333set backupname=backup_%backuptime%.bak
334
335:: Creates the backup using the correct name.
336copy animals.db %backupname% >nul
337echo.
338echo Backup created : %backupname%
339echo Press any key to return to the menu...
340pause >nul
341goto:EOF