Windows cmd: echo without new line but with CR

前端 未结 3 1573
情歌与酒
情歌与酒 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" <nul      
        set /p ".=*%backspace%%input%%%a" <nul
    )    
    exit /b
    
    0 讨论(0)
  • 2021-01-03 11:08
    @echo off
        setlocal enableextensions enabledelayedexpansion
    
        for /f %%a in ('copy "%~f0" nul /z') do set "CR=%%a"
    
        set "count=0"
        for %%a in (*) do (
            set /a "count+=1"
            <nul set /p ".=working on file !count! !CR!"
        )
    

    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 from nul). 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 parameter

    @echo off
        setlocal enableextensions disabledelayedexpansion
    
        for /f %%a in ('copy "%~f0" nul /z') do (
            for /l %%b in (0 1 1000) do (
                <nul set /p ".=This is the line %%b%%a"
            )
        )
    
    0 讨论(0)
  • 2021-01-03 11:16

    The above no longer works in Windows 7 and later. I found the reason is delayed expansion that should be set after ASCII_13 assignment, maybe someone smart could explain why exactly.

    Anyway, the code below works both on Windows 7 and Windows 10.

    @set licz=0
    @setlocal
    @for /f %%a in ('copy /Z "%~dpf0" nul') do @set "ASCII_13=%%a"
    @setlocal enabledelayedexpansion
    :loop
    @set /a licz=licz+1
    @set /p "=Waiting time: %licz% seconds!ASCII_13!" <NUL
    @Timeout /T 1 /Nobreak > NUL
    @GOTO loop
    

    If you do not like the cursor blinking at beginning of the line, transfer ASCII_13 to the beginning to execute CR before text. Needs to be preceeded by any ASCII character, though, to avoid getting stripped. And this will be visible as the last char on the line, so be wary here :)

    @set /p "=.!ASCII_13!Waiting time: %licz% seconds" <NUL
    
    0 讨论(0)
提交回复
热议问题