在 Windows 批处理文件中,我想向用户询问输入,我想向用户显示一个默认值,即 bat 文件所在的文件夹。因此,在运行批处理文件时,批处理检查当前文件夹并将默认变量设置为它,然后用户可以通过单击输入接受建议的值或输入不同的值。我尝试了此代码,但它不起作用,未设置 UserInputPath。
set default=ABCD
set /p UserInputPath=%default%
echo %UserInputPath%
在 Windows 批处理文件中,我想向用户询问输入,我想向用户显示一个默认值,即 bat 文件所在的文件夹。因此,在运行批处理文件时,批处理检查当前文件夹并将默认变量设置为它,然后用户可以通过单击输入接受建议的值或输入不同的值。我尝试了此代码,但它不起作用,未设置 UserInputPath。
set default=ABCD
set /p UserInputPath=%default%
echo %UserInputPath%
将第一行替换为set UserInputPath=ABCD
,因此当用户只是用 确认提示时ENTER,前一个变量值不会被覆盖,因此ABCD
会被回显:
set "UserInputPath=ABCD"
set /P UserInputPath="Prompt text: "
echo(%UserInputPath%
如果您想知道用户是否输入了任何内容,请ErrorLevel
稍后查询该值:
if ErrorLevel 1 echo The user just pressed {Enter}.
注意:
如果您希望提示预先填写ABCD
,那么您需要使用一些能够将击键发送到此提示的外部软件...
你要求user can accept the suggested value by clicking on enter or enter a different value
。
利用set /p
: 如果输入为空(只是ENTER
),则变量保持不变。所以你可以简单地设置一个默认值:
set "UserInputPath=ABCD"
set /p "UserInputPath=Enter path or just ENTER for default [%UserInputPath%] : "
echo %UserInputPath%
根据您对问题的编辑。您想用来%~dp0
检测批处理文件的驱动器和路径,然后将路径回显到提示中并将其设置为默认值,除非用户键入其他内容,否则它将始终使用运行批处理的默认路径。可以作为script value
或script
仅在提示用户的地方运行:
@echo off
set "UserInputPath=%1"
set "default=%~dp0"
if "%UserInputPath%"=="" set /p "UserInputPath=Enter Path (Default "%default%"): " || set "UserInputPath=%default%"
echo "%UserInputPath%"
pause