问题
I want to have this kind of result in a DOS batch:
==> Please enter your filename, formatted as
[user][PC_id] :
allowing the user to enter the filename after the ":"
I tried a lot of combinations, but the best I can get it with
ECHO --^> Please enter your filename, formatted as
Set /P FILE=[user][PC_id] :
(blank spaces before "[" are not displayed)
回答1:
You have problems with a feature introduced in Vista/Win7.
It strips all whitespace characters in front of a string, when using set/p
.
But in your case it's easy, as you want to echo two lines.
@echo off
setlocal EnableDelayedExpansion
set LF=^
set /p "file=--> Please enter your filename, formatted as!LF! [user][PC_id]"
回答2:
jeb's solution works fine when you are printing 2 lines. But there is a generic solution that works when you only want to print a single line with leading space. All you need is a backspace character.
@echo off
setlocal
:: Define BS to contain a backspace
for /f %%A in ('"prompt $H&for %%B in (1) do rem"') do set "BS=%%A"
::Only need %BS% if you know prompt starts at line beginning
Set /P "FILE=%BS% [user][PC_id] : "
::If prompt is not at line beginning then need an extra char before %BS%
<nul set /p "=Part1"
<nul set /p "=.%BS% Part2"
回答3:
another variant:
exit | cmd /k prompt -$G Please enter your filename, formatted as$_$s$s$s[user][PC_id]$s
set /p FILE=
echo %FILE%
来源:https://stackoverflow.com/questions/13488592/in-dos-how-to-set-p-with-a-string-starting-with-spaces