找回密码
 注册

QQ登录

只需一步,快速开始

查看: 243|回复: 0

Windows batch: echo without new line

[复制链接]
发表于 2013-5-9 06:53:35 | 显示全部楼层 |阅读模式
Two ways to use set with /p to 'echo' without a new line:
  1. echo|set /p=.


  2. <nul set /p=.
复制代码
Both examples rely on the set command's behaviour when given the '/p' switch. The '/p' switch turns 'set' into a prompt for input. Usually, this would be employed to solicit interactive input from the user. The value entered at the prompt can be stored in a variable, e.g. 'set /p armageddon=Push Big Red Button?', where the variable 'armageddon' is set to the user's response to the prompt question.

The first example works by having 'set /p=.' output the '.', then get a response from the output of  an empty 'echo' statement piped into the command. The response from echo is discarded, because there is no variable defined for the '=' in the 'set' statement. The second example works by redirecting the 'nul' device into the 'set' statement.

Each time the statement is executed, another dot is output on the same line. This technique could be used to make a progress bar in a batch file, e.g. each time through a 'for /f' loop.

The statement could also be wrapped in an if-else block to flag errors, e.g.
  1. if %foo% equ 1 (
  2.     <nul set /p=.
  3. ) else (
  4.     <nul set /p=F
  5. )
复制代码
The output is a series of dots for success, with F's marking failures:
....F...F.

Big thanks to arnep for this elegant solution.
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|BC Morning Website ( Best Deal Inc. 001 )

GMT-8, 2026-6-11 03:02 , Processed in 0.013897 second(s), 15 queries .

Supported by Weloment Group X3.5

© 2008-2026 Best Deal Online

快速回复 返回顶部 返回列表