2016-09-14 107 views
2

我已經創建了批處理文件,其中包括批處理文件代碼以及VBScript代碼。現在我試圖將批處理文件中的變量值傳遞給VBScript,但它不起作用。如何將批處理文件的變量用於VB腳本文件

echo This is batch 
set /p Name="Enter Your Name: " 
:x=msgbox("You have Entered '" & name & "'" ,0, "Your Title Here") 
findstr "^:" "%~sf0">temp.vbs & cscript //nologo temp.vbs & del temp.vbs 
echo This is batch again 

以下輸出我得到:

c:\Users\vshah\Desktop>echo This is batch 
This is batch 

c:\Users\vshah\Desktop>set /p Name="Enter Your Name: " 
Enter Your Name: Vinkesh 

c:\Users\vshah\Desktop>findstr "^:" "c:\Users\vshah\Desktop\Print.bat" 1>temp.vbs & cscript //nologo temp.vbs & del temp.vbs 

c:\Users\vshah\Desktop>echo This is batch again 
This is batch again 

c:\Users\vshah\Desktop> 

In Message Box I am getting message only -- You have Entered " 
Not getting variable output 

請幫我從批號傳遞變量VBScript代碼,並使用它們

非常感謝您提前...

回答

1

簡單地改變這樣的:

:x=msgbox("You have Entered '" & name & "'" ,0, "Your Title Here") 
findstr "^:" "%~sf0">temp.vbs & cscript //nologo temp.vbs & del temp.vbs 

到這一點:

echo x=msgbox("You have Entered '%name%'" ,0, "Your Title Here")>temp.vbs 
cscript //nologo temp.vbs & del temp.vbs 

注意,你沒有真正傳遞變量這種方式,但創建一個包含該文本值一個臨時腳本變量。如果你想讓VBScript實際使用一個變量,你需要將代碼改爲如下所示:

echo name=WScript.Arguments.Unnamed(0)>temp.vbs 
echo x=msgbox("You have Entered '" ^& name ^& "'" ,0, "Your Title Here")>>temp.vbs 
cscript //nologo temp.vbs "%name%" 
del temp.vbs 
1

這會生成正確的msgbox:

@echo off 
setlocal enabledelayedexpansion 
echo This is batch 
set /p Name="Enter Your Name: " 
:x=msgbox("You have Entered '__name__'" ,0, "Your Title Here") 
findstr "^:" "%~sf0"> %TEMP%\str 
set /p vbl=<%TEMP%\str 
del %TEMP%\str >NUL 
set vbl=%vbl:__name__=!name!% 
rem remove the first colon 
echo %vbl:~1% 1>temp.vbs & cscript //nologo temp.vbs & del temp.vbs 

我已經使用了一個模板(__name__)以及enabledelayedexpansion,以便能夠用變量中的變量替換值。 我還必須創建另一個臨時文件,以後刪除。

相關問題