Create a database within a bat file

2019-08-05 04:42发布

问题:

I am trying to create a bat file that is in essence a database. I want to be able to enter information that is tied to a single record. When I enter a record, I want to be able to look up the record by the card number assigned to it. The code I have now doesn't really work due to the fact that the variables are not being stored properly.

This is my code:

Color 5F
@echo off
:start
cls
echo ==========================================
echo           Gift Card
echo ==========================================
echo.
echo What would you like to do?
echo.
echo    1 Add Card
echo    2 Check Information
echo    3 Edit Card Balance
echo    4 Delete Card
echo.
set /p choice=Please enter choice: 
if /I %choice%==1 goto 1
if /I %choice%==2 goto 2

:1
echo.
set /p var=Enter Card Number: 
set /p val=Enter Amount: 
set /p fname=Enter First Name: 
set /p lname=Enter Last Name: 
set /p cbal=Enter Current Balance: 
set /p diss=Enter Date issued: 
@echo set %var%=%val%=%fname%=%lname%=%cbal%=%diss% > %var%.bat
echo.
echo The data has been stored!
pause
goto start

:2
echo.
set /p var=Please enter card number: 
setlocal enabledelayedexpansion
call %var%.bat
echo !%fname%! !%lname%!'s !%var%! card has $!%cbal%! on it as of !%diss%!!
pause > nul
goto start

I have tried to send the variables separately and altogether and none have worked. I am thinking it is because I do not have the delayed expansion sytax correct.

Any help is very appreciated!

回答1:

Your problem seems to lie where you generate the batch file to fill the variables... You generate only one line, which does not actually assign anything to any of the variables you need.

Try changing

@echo set %var%=%val%=%fname%=%lname%=%cbal%=%diss% > %var%.bat

to be

@echo set var=%var% > %var%.bat
@echo set val=%val% >> %var%.bat
@echo set fname=%fname% >> %var%.bat
@echo set lname=%lname% >> %var%.bat
@echo set cbal=%cbal% >> %var%.bat
@echo set diss=%diss% >> %var%.bat

This should allow your variables to be loaded back properly.

Also, change

echo !%fname%! !%lname%!'s !%var%! card has $!%cbal%! on it as of !%diss%!!

to read

echo %fname% %lname%'s %var% card has $%cbal% on it as of %diss%!

You should never use both ! and % to surround variables in a batch file, only one or the other. % should be used in most cases; ! should be used when you need to read a variable inside a multi-line "code block" (for example, the result of an if statement or the body of a for loop) which is surrounded by parentheses.

Some more advice:

  • You can put setlocal delayedexpansion just once in the beginning of the file, right after @echo off. However, you are not doing anything in this program (yet, at least) to need delayed expansion. Delayed expansion is used to enable accessing variables with the ! symbol and is only useful inside multi-line statement bodies surrounded by parentheses. Because of that, you should get rid of it completely unless/until you actually need it, as it can cause other problems.
  • There is no need for the "@" symbol in any command after you call @echo off (but it won't break anything). The @ symbol simply suppresses echoing of the command which it precedes, but it is redundant because all commands are silenced by default after you call echo off. For this reason, it is important to only use it on the first line when calling @echo off, so that the user does not see that command echoed.