为什么我的变量没有正确扩展

时间:2014-02-17 03:32:33

标签: variables batch-file expansion

我正在使用bat文件处理数据库,而我的变量没有以正确的方式扩展。

修改

我已经尝试过以上并且没有这样的运气。

这是显示所需的所有代码:

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

这就是我必须要做的一切。 %lname%.bat将变量存储为var0 =“变量值”问题是它不读取var0。它读取!var 0!。

2 个答案:

答案 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

此批次证明您的代码确实有效。问题在于您尚未发布的%lname%.bat。当然,您没有为每个客户设置不同的%lname%.bat

发布一些真实的样本数据没有坏处 - 只需更改名称以保护有罪。将真实姓名修改为“Brown”或“Green”或“Robinson”。似乎没有人再使用这些名字 - 他们显然是假的。

答案 1 :(得分:0)

调用脚本时,正在运行新的cmd实例。设置的变量仅适用于该实例。

set filepath=C:\Documents and Settings\Administrator\Desktop\My Personal Files\Computer Refresh\Database\%lname%.bat
for /f "delims=" %%a in (%filepath%) do %%a

这将循环遍历%lname%.bat的每一行并将该行作为命令运行 - 如果%lname%.bat中的所有行都是那些设置命令,这应该有效。