Friday, 7 December 2018

Overwrite line in Windows batch file?



Just before I say anything, I have tried this code:


@echo off

setlocal enableextensions enabledelayedexpansion
for /f %%a in ('copy /Z "%~dpf0" nul') do set "ASCII_13=%%a"

set /p "=Example 1"
set /p "=!ASCII_13!Example 2"

My Source: How to overwrite the same line in command output from batch file (The verified answer from it)


And that didn't work for me. It just outputted as Example 1Example 2 when I tried it.

So I just need a way to overwrite the same line in a batch file (eg. It said "Example 1" before, but after that line will say "Example 2" instead).

Thanks!



Answer



is a trick to echo a string (the text prompt) without a line feed at the end, so the cursor will stay in the same line and we can later return to the start of the line (sending a carriage return) to overwrite it.


The problem you have found with the indicated answer is the behaviour change in the set /p. Up to Windows XP, the set /p command was able to echo spaces at the start of the text prompt, but from Vista white characters are removed from the start of the prompt. You can not send the carriage return at the start of the string. But you can use it in another place


@echo off
setlocal enableextensions enabledelayedexpansion

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"


That is, instead of sending the CR at the start of a line, send it at the end of the previous line.


Another option is to include a different character as the start of the text prompt before the CR. Including a visible character generates another problem, as it should be overwritten in the next line so it is not visible. A backspace (ascii 0x08) is a safe (usually) option


@echo off
setlocal enableextensions enabledelayedexpansion

for /f %%a in ('copy /Z "%~dpf0" nul') do set "CR=%%a"
for /f %%a in ('"prompt $H&for %%b in (0) do rem"') do set "BS=%%a"

No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...