I would like to write on the same line inside a loop in a windows batch file. For example:
setlocal EnableDelayedExpansion
set file_number=0
for %%f in (*) do (
set /a file_number+=1
echo working on file number !file_number!
something.exe %%f
)
setlocal DisableDelayedExpansion
This will result in:
echo working on file number 1
echo working on file number 2
echo working on file number 3
. . .
I would like all of them to be on the same line. I found a hack to remove the new line (e.g. here: Windows batch: echo without new line), but this will produce one long line.
Thanks!
The first
for
command executes a copy operation that leaves a carriage return character inside the variable.Now, in the file loop, each line is echoed using a
<nul set /p
that will output the prompt string without a line feed and without waiting for the input (we are reading fromnul
). But inside the data echoed, we include the carriage return previously obtained.BUT for it to work, the
CR
variable needs to be echoed with delayed expansion. Otherwise it will not work.If for some reason you need to disable delayed expansion, this can be done without the CR variable using the
for
command replaceable parameterThanks to the answer of MC ND I have a created a subroutine,
echocr
, that you can call without delayed expansion, that will echo a string with only a carriage return, and no newline. (The spaces after%input%
are adjusted to cover all previous messages).You can use it to overwrite a line as shown in the modified code: