Advertisement
Hugo7

RTZI zoo management tool

Jan 7th, 2017
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Batch 10.90 KB | None | 0 0
  1. @echo off
  2. :: Program made by Hugo7 for The ZT2 Round Table under CC-BY-NC 2.0 licence -> https://creativecommons.org/licenses/by/2.0/fr/ you must credit me id you copy any part of it.
  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
  9. set version=1.0
  10. setlocal enabledelayedexpansion
  11. mode con lines=40 cols=91
  12. set title_rad=RTZI zoo managing software version %version% - made by Hugo7 -
  13.  
  14. :: If the list does not exist, we create it
  15. if 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
  23. title %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.
  26. set cmd=0
  27. set count=0
  28. set confirm=2
  29. set cancel=0
  30. set species=0
  31. cls
  32.  
  33. :: User input
  34. echo  ========================================================================================
  35. echo  = Available commands : list, +, -, search, backup                                      =
  36. echo  = Remember to use correct names for animals. Example : "Red panda" will be "pandared". =
  37. echo  = Please use "help ^<command^>" to get the syntax of a command.                        =
  38. echo  ========================================================================================
  39. echo Your command :
  40. set/p cmd=/
  41.  
  42. :: Counts the number of arguments given by the user
  43. for %%C in (%cmd%) do set /a count=!count!+1
  44. set/a count=%count%-1
  45.  
  46. :: Decomposes the input to take all arguments
  47. for /f "tokens=1-6 delims= " %%A in ("%cmd%") do (
  48. set main=%%A
  49. set arg1=%%B
  50. set arg2=%%C
  51. set arg3=%%D
  52. set arg4=%%E
  53. set 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.
  57. if "%main%"=="list" call :list
  58. if "%main%"=="search" call :search %arg1%
  59. if "%main%"=="+" call :add %arg1% %arg2% %arg3% %arg4% %arg5%
  60. if "%main%"=="-" call :remove %arg1% %arg2% %arg3% %arg4% %arg5%
  61. if "%main%"=="help" call :help %arg1%
  62. if "%main%"=="backup" call :backup
  63.  
  64. :: if incorrect cmd, then returns to the menu - main function ends
  65. goto 1
  66.  
  67. ::----------------------
  68.  
  69. :list
  70. :: Arguments : no need of any argument.
  71. title %title_rad% List of all your animals!
  72. echo.
  73. echo Lists all animals.
  74. echo Press any key to continue...
  75. pause >nul
  76.  
  77. :: Sorts the list by alphabetic order
  78. sort animals.db /o animals.db_
  79. del/q animals.db
  80. ren animals.db_ animals.db
  81.  
  82. :: Creates the temp file
  83. echo List of your animals : >animals.tmp
  84.  
  85. :: Fills it
  86. for /f "tokens=1-5 delims=- eol=#" %%L in ('type animals.db') do (
  87. set species=%%L
  88. set species=!species:\=!
  89. echo  !species!   :   !REG3XP0!>%%M males,   %%N females,   %%O young males   and   %%P young females  >>animals.tmp
  90. )
  91.  
  92. :: Clears the screen and show the 'decoded' list.
  93. cls
  94. type animals.tmp | more
  95.  
  96. echo.
  97. echo Press any key to return to the menu...
  98. pause >nul
  99.  
  100. :: Deletes the temp file and returns to the menu
  101. del /q animals.tmp
  102. goto:EOF
  103.  
  104. ::----------------------
  105.  
  106. :search
  107. :: Arguments : only one needed argument : just what the user searches for - only one word
  108. if %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. )
  114. title %title_rad% Search tool
  115. echo.
  116. for /f "tokens=1-5 delims=-" %%A in ('type animals.db ^| find /i "%1"') do (
  117. set species=%%A
  118. set species=!species:\=!
  119. echo !species! : !REG3XP0!>%%B males, %%C females, %%D young males and %%E young females.
  120. )
  121. echo.
  122. echo Press any key to return to the menu...
  123. pause >nul
  124. goto:EOF
  125.  
  126. ::----------------------
  127.  
  128. :add
  129. :: Arguments : (5) : species, number of males, nb of females, nb of young males, nb of young females
  130. if 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. )
  137. title %title_rad% Adding animals to the list
  138. echo.
  139. echo 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
  143. echo Please confirm by typing "1" or type "0" to cancel and return to the menu.
  144. set/p confirm=^>
  145. if "%confirm%"=="1" goto letsadd
  146. if "%confirm%"=="0" goto 1
  147. :: if it's not 1 or 0, then re-ask the question
  148. goto confirm1
  149.  
  150. :: This is an other function IN a function...
  151. :letsadd
  152. :: Takes all the numbers in order to increment them
  153. for /f "tokens=1-5 delims=- eol=#" %%A in ('type animals.db ^| find /i "\%1\"') do (
  154. set nbmales=%%B
  155. set nbfemales=%%C
  156. set nbymales=%%D
  157. set nbyfemales=%%E
  158. )
  159.  
  160. :: Increments (%2 = the second argument = nb of males to add, etc) some nb can be incremented by 0
  161. set /a nbmales=%nbmales%+%2
  162. set /a nbfemales=%nbfemales%+%3
  163. set /a nbymales=%nbmyales%+%4
  164. set /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.
  168. for /f "tokens=1,* delims=-" %%Y in ('type animals.db') do (
  169. if not %%Y==\%1\ (
  170. >>animals.db_ echo %%Y-%%Z
  171. )
  172. )
  173. >>animals.db_ echo \%1\-%nbmales%-%nbfemales%-%nbymales%-%nbyfemales%
  174. del /q animals.db
  175. sort animals.db_ /o animals.db
  176. del /q animals.db_
  177. color a
  178. echo.
  179. echo Operation completed!
  180. echo Press any key to return to the menu...
  181. pause >nul
  182. color 7
  183. goto:EOF
  184.  
  185. ::----------------------
  186.  
  187. :remove
  188. :: Arguments : (5) : species, number of males, nb of females, nb of young males, nb of young females
  189. if 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. )
  196. title %title_rad% Removing animals to the list
  197. echo.
  198. echo 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
  202. echo Please confirm by typing "1" or type "0" to cancel and return to the menu.
  203. set/p confirm=^>
  204. if "%confirm%"=="1" goto letsrem
  205. if "%confirm%"=="0" goto 1
  206. :: if it's not 1 or 0, then re-ask the question
  207. goto confirm2
  208.  
  209. :: This is an other function IN a function...
  210. :letsrem
  211. :: Takes all the numbers in order to decrement them
  212. for /f "tokens=1-5 delims=- eol=#" %%A in ('type animals.db ^| find /i "\%1\"') do (
  213. set nbmales=%%B
  214. set nbfemales=%%C
  215. set nbymales=%%D
  216. set nbyfemales=%%E
  217. )
  218.  
  219. :: Decrements (%2 = the second argument = nb of males to remove, etc) some nb can be decremented by 0
  220. set /a nbmales=%nbmales%-%2
  221. set /a nbfemales=%nbfemales%-%3
  222. set /a nbymales=%nbmyales%-%4
  223. set /a nbyfemales=%nbyfemales%-%5
  224.  
  225. :: Verifies if updated numbers are at least equal to 0 else cancels the operation + msg
  226. if not %nbmales% GEQ 0 goto err
  227. if not %nbfemales% GEQ 0 goto err
  228. if not %nbymales% GEQ 0 goto err
  229. if 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
  232. if %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.
  236. for /f "tokens=1,* delims=-" %%Y in ('type animals.db') do (
  237. if 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
  243. if not %cancel%==1 (
  244. >>animals.db_ echo \%1\-%nbmales%-%nbfemales%-%nbymales%-%nbyfemales%
  245. )
  246. del /q animals.db
  247. sort animals.db_ /o animals.db
  248. del /q animals.db_
  249.  
  250. color a
  251. echo.
  252. echo Operation completed!
  253. echo Press any key to return to the menu...
  254. pause >nul
  255. color 7
  256. goto:EOF
  257.  
  258. :: Error function for the function above
  259. :err
  260. cls
  261. color c
  262. echo.
  263. echo ALERT : incorrect given number : you don't have enough animals.
  264. echo Your zoo will be like : %nbmales% males, %nbfemales% females, %nbymales% young males, %nbyfemales%, young females.
  265. echo A number of animals can't be a negative number!
  266. echo ^> The operation was cancelled. ^<
  267. echo.
  268. echo Press any key to return to the menu...
  269. pause >nul
  270. color 7
  271. goto 1
  272.  
  273.  
  274. ::----------------------
  275.  
  276. :help
  277. :: Arguments : just the command the user wants to learn the syntax
  278. title %title_rad% Wanna get some help?
  279. if %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. )
  285. echo.
  286. echo Syntax of the command "%1" :
  287. if %1==list (
  288. echo /list
  289. echo Lists all animals.
  290. )
  291. if /i %1==+ (
  292. echo /+ ^<species^> ^<nb of males^> ^<nb of females^> ^<nb of young males^> ^<nb of young females^>
  293. echo If the species already exists in the list, then the number^(s^) is/are
  294. echo incremented by the given number^(s^), else, the species is added to the
  295. echo list with the given number^(s^).
  296. echo Example :
  297. echo /+ pandared 1 0 0 0
  298. echo    -^> adds one adult male red panda to the list
  299. )
  300. if /i %1==- (
  301. echo /- ^<species^> ^<nb of males^> ^<nb of females^> ^<nb of young males^> ^<nb of young females^>
  302. echo If the species already exists in the list, then the number^(s^) is/are
  303. echo decremented by the given number^(s^). An error message is shown if a
  304. echo number reaches a negative value ^(ex. if you want to remove 3 males
  305. echo while you have only 1 in the list^).
  306. echo Example :
  307. echo /- pandared 1 0 0 0
  308. echo    -^> removes one adult male red panda to the list
  309. )
  310. if /i %1==search (
  311. echo /search ^<what^>
  312. echo Shows the line^(s^) in the list that contain^(s^) the provided word.
  313. echo It's coveniant to see what is the exact denimination for an animal.
  314. echo Example :
  315. echo /search pandared
  316. echo    -^> Shows the detailled numbers of red pandas ^(males, females, etc.^)
  317. )
  318. if /i %1==backup (
  319. echo /backup
  320. echo Creates a backup of the list, with the date and the hour in the backup file's name.
  321. )
  322. echo.
  323. echo Press any key to return to the menu...
  324. pause >nul
  325. goto: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 "-".
  332. for /f "tokens=1-3 delims=:" %%N in ("%time%") do set backuptime=%%N-%%O-%%P
  333. set backupname=backup_%backuptime%.bak
  334.  
  335. :: Creates the backup using the correct name.
  336. copy animals.db %backupname% >nul
  337. echo.
  338. echo Backup created : %backupname%
  339. echo Press any key to return to the menu...
  340. pause >nul
  341. goto:EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement