问题
I am working on a database using a bat file an my variables are not expanding the right way.
Edit:
I have tried the above and have had no such luck.
This is all of the code necessary to show:
set n=0
:1
echo.
set /p fname=Enter First Name:
set /p lname=Enter Last Name:
set /p val%n%=Enter E-mail:
set /p num%n%=Enter Phone Number:
set /p var%n%=Enter Service Number:
set /p dvice%n%=Enter Device:
set /p dserv%n%=Enter Date Serviced:
set /p summ%n%=Enter Summary of Service:
@echo set var%n%=!var%n%! > "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
@echo set val%n%=!val%n%! >> "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
@echo set fname=%fname% >> "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
@echo set lname=%lname% >> "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
@echo set dvice%n%=!dvice%n%! >> "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
@echo set dserv%n%=!dserv%n%! >> "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
@echo set summ%n%=!summ%n%! >> "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
@echo set num%n%=!num%n%! >> "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
@echo set n=%n% >> "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
echo.
echo The data has been stored!
echo.
pause > nul
goto start
:2
echo.
echo LAST NAME IS CASE SENSITIVE!
set /p lname=Please enter last name:
echo.
if /i not exist "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat" (goto Error)
call "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
echo First Name: %fname%
echo Last Name: %lname%
echo Device: !%dvice%%n%!
echo Date of Service: !dserv%n%!
echo.
echo E-Mail: !val%n%!
echo Phone: !num%n%!
echo.
echo Summary:
echo.
echo !summ%n%!
echo.
pause > nul
goto start
This is all I have to work with. The %lname%.bat stores variables as var0="value of variable" The problem is that it is not reading var0. It reads !var 0!.
回答1:
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET /a n=0
:: call "C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat"
:start
call :plname
echo First Name: %fname%
echo Last Name: %lname%
echo Device: !%dvice%%n%!
echo Date of Service: !dserv%n%!
echo.
echo E-Mail: !val%n%!
echo Phone: !num%n%!
echo.
echo Summary:
echo.
echo !summ%n%!
echo.
SET /a n+=1
IF %n% lss 2 goto START
GOTO :EOF
:plname
SET "fname=First name read"
SET "lname=Last name read"
SET "dvice0=Device zero"
SET "dvice1=Device one"
SET "dserv0=Date zero"
SET "dserv1=Date one"
SET "val0=Email zero"
SET "val1=Email one"
SET "num0=Phone zero"
SET "num1=Phone one"
SET "summ0=Summary zero"
SET "summ1=Summary one"
GOTO :EOF
This batch proves that your code does work. The problem is with your %lname%.bat
which you haven't posted. Surely you don't set up a different %lname%.bat
for each customer?
No harm in posting some real sample data - just change the names to protect the guilty. Modify the real names to "Brown" or "Green" or "Robinson." No-one appears to use those names any more - they're obviously fake.
回答2:
When calling the script, a new instance of cmd is running. The variables set are only available to that instance.
set filepath=C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat
for /f "delims=" %%a in (%filepath%) do %%a
This will loop through each line of %lname%.bat
and run that line as a command - if all of the lines in %lname%.bat
are those set commands, this should work.
来源:https://stackoverflow.com/questions/21820233/why-are-my-variables-not-expanding-properly