in DOS how to SET /P with a string starting with spaces

安稳与你 提交于 2019-12-12 16:25:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!