如何编写批处理脚本,使用这样的for循环:
@echo off
for /l %%a in (1,1,100) do (
echo %%a
)
并在一行中获得输出,而不是:
1
2
3
4
5
..
..
@echo off
for /l %%a in (1,1,100) do (
echo|set /p="%%a "
)
对于您所显示的用例,您可以只使用<nul set /p =%%a
,但是如果您的实际用例涉及可能包含前导空格或=
字符的字符串,则可以使用操作提示字符串的替代方法:
@echo off
Del "%TEMP%output.~tmp" 2> nul
(Set n=^^^
%= n newline var. Do not modify =%)
Setlocal EnableExtensions DisableDelayedExpansion
:# EWN Echo without newline macro
:# Based on: https://www.dostips.com/forum/viewtopic.php?t=4213#p64718
Set EWN=For %%n in (1 2)Do If %%n==2 (%n%
cmd /d /k ^< nul ^>^>"%TEMP%output.~tmp"%n%
Set "prompt="%n%
) Else Set prompt=
:# Examples:
:# %EWN% Leading space
:# %EWN%= Leading equals
:# %EWN%= Escape requirements: ^& ^> ^< ^| %% ^^^ unbalanced dq: ^"
Cls
For /l %%i in (1 1 100)Do %EWN%%%i
Type "%TEMP%output.~tmp"
Echo(
Pause
Endlocal
其他方法也可以在dostips
找到