Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @echo off
- :: 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.
- :: Member of the French forum batch.xoo.it
- :: All lines that start with a "::" are explanations for the code,
- :: But lines that start with only one ":" are labels used by "goto" commands, like a call for a function.
- :: A function is a part of the code between ":something" and "goto:EOF"
- :: Technical details + variables
- set version=1.0
- setlocal enabledelayedexpansion
- mode con lines=40 cols=91
- set title_rad=RTZI zoo managing software version %version% - made by Hugo7 -
- :: If the list does not exist, we create it
- if not exist animals.db (
- #Syntax : species-males-females-youngMales-youngFemales>animals.db
- #"\" 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
- )
- :: Main function begins
- :1
- title %title_rad% Type a command...
- :: Resets some variables and clears the screen - confirm mustn't be set to 0 or 1 for now.
- set cmd=0
- set count=0
- set confirm=2
- set cancel=0
- set species=0
- cls
- :: User input
- echo ========================================================================================
- echo = Available commands : list, +, -, search, backup =
- echo = Remember to use correct names for animals. Example : "Red panda" will be "pandared". =
- echo = Please use "help ^<command^>" to get the syntax of a command. =
- echo ========================================================================================
- echo Your command :
- set/p cmd=/
- :: Counts the number of arguments given by the user
- for %%C in (%cmd%) do set /a count=!count!+1
- set/a count=%count%-1
- :: Decomposes the input to take all arguments
- for /f "tokens=1-6 delims= " %%A in ("%cmd%") do (
- set main=%%A
- set arg1=%%B
- set arg2=%%C
- set arg3=%%D
- set arg4=%%E
- set arg5=%%F
- )
- :: Calls the corect function and send it the given arguments - please see the functions below to read explanations for the arguments.
- if "%main%"=="list" call :list
- if "%main%"=="search" call :search %arg1%
- if "%main%"=="+" call :add %arg1% %arg2% %arg3% %arg4% %arg5%
- if "%main%"=="-" call :remove %arg1% %arg2% %arg3% %arg4% %arg5%
- if "%main%"=="help" call :help %arg1%
- if "%main%"=="backup" call :backup
- :: if incorrect cmd, then returns to the menu - main function ends
- goto 1
- ::----------------------
- :list
- :: Arguments : no need of any argument.
- title %title_rad% List of all your animals!
- echo.
- echo Lists all animals.
- echo Press any key to continue...
- pause >nul
- :: Sorts the list by alphabetic order
- sort animals.db /o animals.db_
- del/q animals.db
- ren animals.db_ animals.db
- :: Creates the temp file
- echo List of your animals : >animals.tmp
- :: Fills it
- for /f "tokens=1-5 delims=- eol=#" %%L in ('type animals.db') do (
- set species=%%L
- set species=!species:\=!
- echo !species! : !REG3XP0!>%%M males, %%N females, %%O young males and %%P young females >>animals.tmp
- )
- :: Clears the screen and show the 'decoded' list.
- cls
- type animals.tmp | more
- echo.
- echo Press any key to return to the menu...
- pause >nul
- :: Deletes the temp file and returns to the menu
- del /q animals.tmp
- goto:EOF
- ::----------------------
- :search
- :: Arguments : only one needed argument : just what the user searches for - only one word
- if %count%==0 (
- echo ERROR : please provide an argument.
- echo Press any key to return to the menu...
- pause >nul
- goto 1
- )
- title %title_rad% Search tool
- echo.
- for /f "tokens=1-5 delims=-" %%A in ('type animals.db ^| find /i "%1"') do (
- set species=%%A
- set species=!species:\=!
- echo !species! : !REG3XP0!>%%B males, %%C females, %%D young males and %%E young females.
- )
- echo.
- echo Press any key to return to the menu...
- pause >nul
- goto:EOF
- ::----------------------
- :add
- :: Arguments : (5) : species, number of males, nb of females, nb of young males, nb of young females
- if not %count%==5 (
- echo ERROR : please provide 5 arguments
- echo ^(species name, nb of males, nb of females, nb of young males and nb of young females^).
- echo Press any key to return to the menu...
- pause >nul
- goto 1
- )
- title %title_rad% Adding animals to the list
- echo.
- echo Adding [%2 males, %3 females, %4 young males and %5 young females] %1 to the list...
- :: This is a function IN a function...
- :confirm1
- echo Please confirm by typing "1" or type "0" to cancel and return to the menu.
- set/p confirm=^>
- if "%confirm%"=="1" goto letsadd
- if "%confirm%"=="0" goto 1
- :: if it's not 1 or 0, then re-ask the question
- goto confirm1
- :: This is an other function IN a function...
- :letsadd
- :: Takes all the numbers in order to increment them
- for /f "tokens=1-5 delims=- eol=#" %%A in ('type animals.db ^| find /i "\%1\"') do (
- set nbmales=%%B
- set nbfemales=%%C
- set nbymales=%%D
- set nbyfemales=%%E
- )
- :: Increments (%2 = the second argument = nb of males to add, etc) some nb can be incremented by 0
- set /a nbmales=%nbmales%+%2
- set /a nbfemales=%nbfemales%+%3
- set /a nbymales=%nbmyales%+%4
- set /a nbyfemales=%nbyfemales%+%5
- :: Updating the list : all animals that are NOT the given animal are transfered from the list to
- :: a temp file and then the modified line is added. Then the list is sorted by alphabetic order.
- for /f "tokens=1,* delims=-" %%Y in ('type animals.db') do (
- if not %%Y==\%1\ (
- >>animals.db_ echo %%Y-%%Z
- )
- )
- >>animals.db_ echo \%1\-%nbmales%-%nbfemales%-%nbymales%-%nbyfemales%
- del /q animals.db
- sort animals.db_ /o animals.db
- del /q animals.db_
- color a
- echo.
- echo Operation completed!
- echo Press any key to return to the menu...
- pause >nul
- color 7
- goto:EOF
- ::----------------------
- :remove
- :: Arguments : (5) : species, number of males, nb of females, nb of young males, nb of young females
- if not %count%==5 (
- echo ERROR : please provide 5 arguments
- echo ^(species name, nb of males, nb of females, nb of young males and nb of young females^).
- echo Press any key to return to the menu...
- pause >nul
- goto 1
- )
- title %title_rad% Removing animals to the list
- echo.
- echo Removing [%2 males, %3 females, %4 young males and %5 young females] %1 to the list...
- :: This is a function IN a function...
- :confirm2
- echo Please confirm by typing "1" or type "0" to cancel and return to the menu.
- set/p confirm=^>
- if "%confirm%"=="1" goto letsrem
- if "%confirm%"=="0" goto 1
- :: if it's not 1 or 0, then re-ask the question
- goto confirm2
- :: This is an other function IN a function...
- :letsrem
- :: Takes all the numbers in order to decrement them
- for /f "tokens=1-5 delims=- eol=#" %%A in ('type animals.db ^| find /i "\%1\"') do (
- set nbmales=%%B
- set nbfemales=%%C
- set nbymales=%%D
- set nbyfemales=%%E
- )
- :: Decrements (%2 = the second argument = nb of males to remove, etc) some nb can be decremented by 0
- set /a nbmales=%nbmales%-%2
- set /a nbfemales=%nbfemales%-%3
- set /a nbymales=%nbmyales%-%4
- set /a nbyfemales=%nbyfemales%-%5
- :: Verifies if updated numbers are at least equal to 0 else cancels the operation + msg
- if not %nbmales% GEQ 0 goto err
- if not %nbfemales% GEQ 0 goto err
- if not %nbymales% GEQ 0 goto err
- if not %nbyfemales% GEQ 0 goto err
- :: Verifies if all updated numbers are equal to 0 : if all = 0, the specie is removed from the list
- if %nbmales%==0 if %nbfemales%==0 if %nbymales%==0 if %nbyfemales%==0 set cancel=1
- :: Updating the list : all animals that are NOT the given animal are transfered from the list to
- :: a temp file and then the modified line is added. Then the list is sorted by alphabetic order.
- for /f "tokens=1,* delims=-" %%Y in ('type animals.db') do (
- if not %%Y==\%1\ (
- >>animals.db_ echo %%Y-%%Z
- )
- )
- :: %cancel% is set when all categories (males, ...) are equal to 0, so there is no animal of the selected
- :: species so we remove the entry from the list
- if not %cancel%==1 (
- >>animals.db_ echo \%1\-%nbmales%-%nbfemales%-%nbymales%-%nbyfemales%
- )
- del /q animals.db
- sort animals.db_ /o animals.db
- del /q animals.db_
- color a
- echo.
- echo Operation completed!
- echo Press any key to return to the menu...
- pause >nul
- color 7
- goto:EOF
- :: Error function for the function above
- :err
- cls
- color c
- echo.
- echo ALERT : incorrect given number : you don't have enough animals.
- echo Your zoo will be like : %nbmales% males, %nbfemales% females, %nbymales% young males, %nbyfemales%, young females.
- echo A number of animals can't be a negative number!
- echo ^> The operation was cancelled. ^<
- echo.
- echo Press any key to return to the menu...
- pause >nul
- color 7
- goto 1
- ::----------------------
- :help
- :: Arguments : just the command the user wants to learn the syntax
- title %title_rad% Wanna get some help?
- if %count%==0 (
- echo ERROR : please provide an argument.
- echo Press any key to return to the menu...
- pause >nul
- goto 1
- )
- echo.
- echo Syntax of the command "%1" :
- if %1==list (
- echo /list
- echo Lists all animals.
- )
- if /i %1==+ (
- echo /+ ^<species^> ^<nb of males^> ^<nb of females^> ^<nb of young males^> ^<nb of young females^>
- echo If the species already exists in the list, then the number^(s^) is/are
- echo incremented by the given number^(s^), else, the species is added to the
- echo list with the given number^(s^).
- echo Example :
- echo /+ pandared 1 0 0 0
- echo -^> adds one adult male red panda to the list
- )
- if /i %1==- (
- echo /- ^<species^> ^<nb of males^> ^<nb of females^> ^<nb of young males^> ^<nb of young females^>
- echo If the species already exists in the list, then the number^(s^) is/are
- echo decremented by the given number^(s^). An error message is shown if a
- echo number reaches a negative value ^(ex. if you want to remove 3 males
- echo while you have only 1 in the list^).
- echo Example :
- echo /- pandared 1 0 0 0
- echo -^> removes one adult male red panda to the list
- )
- if /i %1==search (
- echo /search ^<what^>
- echo Shows the line^(s^) in the list that contain^(s^) the provided word.
- echo It's coveniant to see what is the exact denimination for an animal.
- echo Example :
- echo /search pandared
- echo -^> Shows the detailled numbers of red pandas ^(males, females, etc.^)
- )
- if /i %1==backup (
- echo /backup
- echo Creates a backup of the list, with the date and the hour in the backup file's name.
- )
- echo.
- echo Press any key to return to the menu...
- pause >nul
- goto:EOF
- ::----------------------
- :backup
- :: Arguments : none
- :: 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 "-".
- for /f "tokens=1-3 delims=:" %%N in ("%time%") do set backuptime=%%N-%%O-%%P
- set backupname=backup_%backuptime%.bak
- :: Creates the backup using the correct name.
- copy animals.db %backupname% >nul
- echo.
- echo Backup created : %backupname%
- echo Press any key to return to the menu...
- pause >nul
- goto:EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement