2015-10-20 92 views
1

如標題所示,我的批處理腳本不工作,我知道這個錯誤發生在「REM BUCKS」之後,我也看到了文字「0在這個時候是意外的,」在退出之前,你們可以看看有什麼問題嗎?也許提供一個解決方案?我需要這個代碼用於我的遊戲。我的CMD批處理腳本不工作(此時0意外)。

這個腳本是一個得分腳本,我通過添加「SET的

根據模擬它「REM SCORE」將設置來獲得0,如果獲得變量是一個負數。

我不能給你完整的腳本怎麼一回事,因爲它如此巨大

這裏是代碼:

SETLOCAL 
set stage=add 
set r=10 
set dif=3 
set timer=30 
set w=2 
set cash=6 
REM BUCKS 
if %stage%==cha (
set st=10 
REM SCORE 
call :calculator %r%*10*%dif%*%st%-%w%*%timer% 
timeout 5 > NUL 
set earn=%val% 
if %earn% LSS 0 set earn=0 
set /a cash=%cash% + %val% 
goto :EOF 
) 
if %stage%==0 goto :EOF 
if %stage%==add set st=3 
if %stage%==sub set st=4 
if %stage%==mul set st=5 
call :calculator %r%*10*%dif%*%st%-(%timer%*%w%) 
set earn=%val% 
if %earn% LSS 0 (
set earn=0 
) 
set /a cash=%cash% + %val% 
pause 
exit 
goto :EOF 
:calculator 
>"%temp%\VBS.vbs" echo Set fso = CreateObject("Scripting.FileSystemObject") : Wscript.echo (%*) 
for /f "delims=" %%a in ('cscript /nologo "%temp%\VBS.vbs"') do set "val=%%a" 
del "%temp%\VBS.vbs" 
goto :EOF 

THX閱讀

+2

變量擴展。也許[這](http://stackoverflow.com/a/30177832/2861476)可以幫助 –

回答

4

您需要delayed expansion的int IF塊,我不當然,如果你的VBScript不會做你想要的。這是使用u ber-geeky batch/vbscript hybrid technique腳本的改進版本,它將減少您的IO操作並且腳本將更快:

@echo off 
SETLOCAL enableDelayedExpansion 
set stage=add 
set r=10 
set dif=3 
set timer=30 
set w=2 
set cash=6 
REM BUCKS 

if %stage%==cha (
    set st=10 
    REM SCORE 
    call :calculator "%r%*10*%dif%*%st%-%w%*%timer%" 
    timeout 5 > NUL 
    set earn=!val! 
    if !earn! LSS 0 set earn=0 
    set /a cash=!cash! + !val! 
    goto :EOF 
) 
if %stage%==0 goto :EOF 
if %stage%==add set st=3 
if %stage%==sub set st=4 
if %stage%==mul set st=5 
call :calculator "%r%*10*%dif%*%st%-(%timer%*%w%)" 
set earn=%val% 
if %earn% LSS 0 (
    set earn=0 
) 
set /a cash=%cash% + %val% 
echo %cash% 
for /f "tokens=2" %%# in ("%cmdcmdline%") do if /i "%%#" equ "/c" pause 
exit /b %errorlevel% 

:calculator 
for /f "delims=" %%a in ('cscript /noLogo "%~f0?.WSF" //job:calc "%~1"') do set "val=%%a" 

exit /b %errorlevel% 

    <job id="calc"> 
     <script language="VBScript"> 
     WScript.Echo(WScript.Arguments.Item(0)) 
     </script> 
    </job> 
+0

你好,我很困惑,我以爲你需要鍵入「** CMD/V:ON **」,才能使用「**! **「作爲變量分隔符 –

+0

@Daxter - 在批處理文件中,您可以使用'setlocal enableDelayedExpansion'。當您希望直接在命令提示符下使用延遲擴展時,需要「CMD/V:ON」。 – npocmaka

+0

「**直接**」是什麼意思? –