Link to home
Start Free TrialLog in
Avatar of SimonUK
SimonUK

asked on

Batch file - echo a backspace

I've love to know how to echo a backspace in a batch file.

For example:

echo Deleting file...
del file.txt
echo Done !

Will give a two line result not including any result from the delete command.  Can I echo a backspace to create a result that would show:

Deleteing file... Done !

WITHOUT a cls and re-echo ?


Simon
Avatar of oBdA
oBdA

Not with the standard NT4/W2k/XP/W2k3 command shell, I'm afraid; the "echo" is always followed by a cr lf, and you can't (re)position the cursor. In Win9x/Me, you can load ansi.sys and use the escape command enabled there. But please don't try to use command.com in NT4 or later just for an optical gimmick; the NTVDM can be really ugly.
I don't think you can.  As I think about, it doesn't seem possible.  I'd like to see if someone else can say otherwise though.
Also don't think so; found this, though really unclear, did have echo backspace as an element in a search query ... looks like a research project.
This chapter defines routines to be used in other programs for
--  converting between internal and textual representations of data,
--  principally
http://web.comlab.ox.ac.uk/oucl/work/geraint.jones/publications/book/Pio2/code-formatted.txt

Have you checked MSDN?  
Yep..  as I mentioned in the other thread, I doubt it can be done either, at least not using a .bat file..

FE
AutoIT might have something for you (I'm not sure if it can be used in batch though.

http://www.hiddensoft.com/AutoIt/
Not sure if thats exactly what you are trying to do.  Without ANSI you cannot go up a line because your Echo ends and automatically drops down to the next line.  However if that is indeed what you are trying to do why not use "del file.txt /q" for quiet and then you will get no output at all which would result in

Deleting File...
Done !

Hope this helps.  
Avatar of SimonUK

ASKER

Thanks so far...

Yes the /q idea is great... but I'd really like to have:

Deleting file... Done !

Looks like it's not possible then... unless there's a batch extension program I could use but I'd rather the scripts worked on any machines with no mods.


Simon
You have an extremely bored person out here.  This requires something besides echo but not ANSI.  

Check out  http://www.chebucto.ns.ca/~af380/Tips.html#Tip011 for the EKKO command which would allow you to do what you want.
You will have to load the ANSI.SYS into memory shell while running the command prompt.
To do that, locate the config.nt file, located under <SystemRoot>\System32. Add the following line:
Device=ANSI.SYS

Save the file and start 'Command' (START => Run... => type 'command' and press 'Enter').

Start the editor by typing 'edit' and press 'Enter'.

See the following link to view all ANSI commands and how to use them. It will allow you to type Arrow Up/BackSpace and many other (including text color and symbols) forms to the batch file:
http://www.evergreen.edu/biophysics/technotes/program/ansi_esc.htm#notes

Good luck.

Cyber
ASKER CERTIFIED SOLUTION
Avatar of stokesj56
stokesj56

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I will add, that if you want to erase characters on the MyVar line message, then you can use the embedding of ASCII codes (I wrote a VB app quickly that just "print"ed to a text file the text of the script, adding a couple of CHR(8)'s in there for the backspaces.

So if you wanted to do a:
Listing all files... and have the final line read
Listing all files - done

you would throw in 3 back spaces.

By the way, I realize this isn't my question, but can someone explain exactly how the redirection of input to NUL and setting that variable is making this work?
Avatar of SimonUK

ASKER

Someone once told me there is NOTHING you can't do in a batch file - they were RIGHT !

Thanks so much everyone.  Here's an example of usage.

@echo off
cls
echo.
(set /P MyVar=Checking internet connection... ) < NUL
ping www.yahoo.com | find "TTL" >NUL
if errorlevel 1 goto fail
echo OK!
echo.
(set /P MyVar=Checking local router... ) < NUL
ping 192.168.0.1 | find "TTL" >NUL
if errorlevel 1 goto fail
echo OK!
echo.
pause
exit

:fail
echo Failed!
echo.
pause
exit


Simon
Well, I certainly learned something new here..  thanks to everyone..

FE
> By the way, I realize this isn't my question, but can someone explain exactly how
> the redirection of input to NUL and setting that variable is making this work?

From "help set":

  SET /P variable=[promptString]

This is a special form of the set command to prompt a user for input to be stored in a variable. Try this at a command (cmd) prompt:

C:\> set MyVar=
C:\> set
blah blah ... (Notice MyVar is not in this list)
C:\> set /P MyVar=Enter something:
Enter something: Something
C:\> set
blah blah ... (Notice MyVar is now set to the text you entered)
C:\> set MyVar=

Adding < NUL to this set line tells it to take input from the NUL device. Thus, it doesn't wait for user input. Because the prompt is presented with out a CR and the user doesn't enter one the cursor is left on the prompt line. Since we are setting the variable to nothing it does not actually get set in the environment (we don't need it).

Understand now? No real magic, just using a command in an unintended way to achieve a desired result.