Windows cmd: echo without new line but with CR

前端 未结 3 1593
情歌与酒
情歌与酒 2021-01-03 10:53

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 (*) d         


        
3条回答
  •  庸人自扰
    2021-01-03 10:54

    Thanks 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:

    @echo off
    
    call :echocr "good morning"
    PING -n 2 127.0.0.1>nul
    call :echocr "good afternoon"
    PING -n 2 127.0.0.1>nul
    call :echocr "bye now"
    PING -n 2 127.0.0.1>nul
    pause
    
    :echocr
    
    :: (echo string with carriage return, no line feed)
    
    for /F "tokens=1 delims=# " %%a in (
    '"prompt #$H# & echo on & for %%b in (1) do rem"'
    ) do set "backspace=%%a"
    
    set input=%~1
    set "spaces40=                                       "
    set "spaces120=%spaces40%%spaces40%%spaces40%
    for /f %%a in ('copy "%~f0" nul /z') do (  
        set /p ".=*%backspace%%spaces120%%%a" 

提交回复
热议问题