Home > Blockchain >  For loop output in one line
For loop output in one line

Time:10-09

How can I write a Batch script, with a for loop like this:

@echo off    

for /l %%a in (1,1,100) do (
    echo %%a
)

and get the output in one line, instead of:

1
2
3
4
5
..
..

CodePudding user response:

For your shown use case, you could just use <nul set /p =%%a however if your actual usecase involves strings that may contain leading whitespace or = characters an alternative that manipulates the prompt string can be used:

@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

Other methods can also be found at dostips

CodePudding user response:

@echo off

for /l %%a in (1,1,100) do ( echo|set /p="%%a " )

  • Related