2014-02-17 93 views
0

我正在使用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 !.

+0

您尚未提供足夠的代碼來診斷問題。也許問題是價值從未設置,否則由於ENDLOCAL或其他類似原因導致值丟失。嘗試不帶參數的'set'來查看所有已定義的變量。 – dbenham

+0

我實際上刪除了代碼,因爲它使帖子冗長。變量設置爲!var%n%!並正確存儲。只是不能被閱讀。 –

+0

我強烈建議您按照我的建議,並在代碼的末尾放置'set'命令(不帶參數)以列出所有當前定義的變量。我相信你會看到你的變量不再被定義。正如Magoo在他的回答中所表明的那樣,問題出現在您未顯示的早期代碼中。 – dbenham

回答

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行的是那些組命令,這應該工作。

+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」。似乎沒有人再使用這些名字 - 他們顯然是假的。

+0

+1,但我個人知道一個布朗家族可能會對你的最後一句話不予理睬! Clark Kent,Lois Lane,John Doe,Jane Doe等可能是更好的賭注。 – dbenham

+0

我將在今晚晚些時候添加代碼。 %lname%.bat適用於每個單獨的客戶。數據將有多個服務號碼。關鍵在於讓一個人在路上有幾個服務號碼和摘要。這將是一個我可以快速編輯的迷你數據庫。 –

相關問題