我有一个bat脚本,要求用户输入下一行
SET /P returnString=Enter string: %=%
现在我希望在命令行上显示默认输入,例如:
Enter string: defaultstring
澄清一下:如果没有给出输入,我不想要默认值,我希望命令行上的默认值可见且可编辑,因此在上述情况下,defaultstring
可以替换为不同的文字。
批处理文件中的SET可以实现吗?如果没有,我怎么能做到这一点?
答案 0 :(得分:2)
@if (@CodeSection == @Batch) @then
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: The first line in the script is...
:: in Batch, a valid IF command that does nothing.
:: in JScript, a conditional compilation IF statement that is false.
:: So the following section is omitted until the next "[at]end".
:: Note: the "[at]then" is required for Batch to prevent a syntax error.
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: Batch Section
@echo off
setlocal
set "Input="
set "Default=Hello World"
title MyUniqueTitle
CScript //E:JScript //Nologo "%~f0" "MyUniqueTitle" "%Default%"
set /p "Input=> Prompt: "
if defined Input set "Input=%Input:"=%"
echo(%Input%
endlocal
exit /b 0
:: End of Batch
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
@end
////////////////////////////////////////////////////////////////////////////////
// JScript Section
try
{
var WshShell = WScript.CreateObject('WScript.Shell');
var Title = WScript.Arguments.Item(0);
var Message = WScript.Arguments.Item(1);
WshShell.AppActivate(Title);
WshShell.SendKeys(Message);
WScript.Quit(0);
}
catch(e)
{
WScript.Echo(e);
WScript.Quit(1);
}
WScript.Quit(2);
答案 1 :(得分:0)
嗯,这不是你要求的,但我觉得它更好。这将创建一个browseforfolder对话框,让您的用户选择要使用的文件夹。在这个例子中,r是函数的返回变量。如果选择了文件夹,它将返回错误级别0,如果按下取消按钮,则返回1。
@Echo off
setlocal
Call :BrowseFolder "Enter path to folder" "C:\scripts\" r
echo %r%
echo %errorlevel%
pause
Goto :EOF
:BrowseFolder <Title> <DefaultStartPath> <Return>
setlocal
set vbs="%temp%\_.vbs"
if exist %vbs% del /f /q %vbs%
>%vbs% echo set sh=wscript.CreateObject("Shell.Application")
>>%vbs% echo set f=sh.BrowseForFolder(0,%1,0,%2)
>>%vbs% echo if typename(f)="Nothing" Then
>>%vbs% echo wscript.echo "Dialog Cancelled"
>>%vbs% echo wscript.Quit(1)
>>%vbs% echo end if
>>%vbs% echo set fs=f.Items():set fi=fs.Item()
>>%vbs% echo p=fi.Path:wscript.echo p
for /f "tokens=*" %%a in ('cscript //nologo %vbs%') do set result=%%a
if exist %vbs% del /f /q %vbs%
if "%result%" EQU "Dialog Cancelled" (set a=1) else set a=0
endlocal & set %3=%result% & exit /b %a%