Send "Backspace" Key in Batch

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Send "Backspace" Key in Batch

#1 Post by alleypuppy » 11 Aug 2011 18:30

Hello,

Is there a way to send the "Backspace" key in a batch program? I know it would be easier to use a VBScript file and incorporate it into the batch program, but I'm not entirely sure how to do that and I would like for the program to stay one file. Thanks for any help.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Send "Backspace" Key in Batch

#2 Post by Ed Dyreen » 11 Aug 2011 18:34

'

Code: Select all

@echo off
setlocal EnableDelayedExpansion

::--------------------------------------------------------------------------------------------------------------------------
set "$Defines=$BS"    &set "$Details=Create $ESC Ascii-0x1B-27, Expansion insensitive"
::
::(
   for /f "delims=#" %%a in (

      '"prompt #$H# &echo on &for %%b in (1) do rem"'

   ) do (
      set "%$Defines%=%%a"
      set "%$Defines%=!$BS:~0,1!"
   )
   ::
   echo.This %$BS%Works
::)
::--------------------------------------------------------------------------------------------------------------------------
Untested !

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Send "Backspace" Key in Batch

#3 Post by alleypuppy » 11 Aug 2011 18:41

Thanks Ed! It works perfectly!

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Send "Backspace" Key in Batch

#4 Post by dbenham » 21 Nov 2012 11:28

Ed's code can be simplified. No need for delayed expansion, # symbols, DELIMS option, ECHO ON, or substring.
Also, the comment in Ed's code is misleading, obviously a carryover from some other code.

Here is a simplified version with corrected comment, preserving Ed's unique style :wink:

Code: Select all

@echo off
setlocal
::--------------------------------------------------------------------------------------------------------------------------
set "$Defines=$BS"    &set "$Details=Create $BS Ascii-0x08-008, Expansion insensitive"
::
::(
   for /f %%a in (

      '"prompt $H&for %%b in (1) do rem"'

   ) do (
      set "%$Defines%=%%a"
   )
   ::
   echo.This %$BS%Works
::)
::--------------------------------------------------------------------------------------------------------------------------


Below is effectively the same code, as I like to write it.

Code: Select all

@echo off
setlocal

:: Define BS to contain a backspace
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"

echo.This %BS%Works


Dave Benham

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Send "Backspace" Key in Batch

#5 Post by Ed Dyreen » 21 Nov 2012 12:13

'
My implementation produces 08 20 08, somewhere you pointed out to strip at 0,1.

Wasn't sure about the 'echo on' requirement, looked into cmd /?.
Couldn't find any evidence that 'echo' can be disabled from registry so it probably safe if left out.

You beat me, or was it jeb :D

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: Send "Backspace" Key in Batch

#6 Post by timbertuck » 21 Nov 2012 14:00

dbenham wrote:Ed's code can be simplified. No need for delayed expansion, # symbols, DELIMS option, ECHO ON, or substring.
Below is effectively the same code, as I like to write it.

Code: Select all

@echo off
setlocal
:: Define BS to contain a backspace
for /f %%a in ('"prompt $H&for %%b in (1) do rem"') do set "BS=%%a"
echo.This %BS%Works

Dave Benham


dave, can you explain a noobish question? why the need for the for %%b loop?

i knowthat Alt+008 does a backspace, is there a way to use that functionality in a macro?

thanks
tt

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Send "Backspace" Key in Batch

#7 Post by dbenham » 22 Nov 2012 06:50

The FOR IN() clause commands are executed by a new CMD.EXE process. They are parsed all at once as a single block of commands.

ECHO is initially off when the commands are executed. We need ECHO ON to get the prompt output for the REM command.

The code block turns ECHO ON, but the change in state is not recognized within the already parsed block of commnds - the entire block uses the ECHO STATE that existed when the block was entered. The one exception is the FOR DO clause uses the current ECHO state that exists at the beginning of each iteration. The extra FOR statement is the only way to get the REM statement to be echoed.


Dave Benham

timbertuck
Posts: 76
Joined: 21 Dec 2011 14:21

Re: Send "Backspace" Key in Batch

#8 Post by timbertuck » 22 Nov 2012 09:21

dbenham wrote:The FOR IN() clause commands are executed by a new CMD.EXE process. They are parsed all at once as a single block of commands.
ECHO is initially off when the commands are executed. We need ECHO ON to get the prompt output for the REM command.
The code block turns ECHO ON, but the change in state is not recognized within the already parsed block of commnds - the entire block uses the ECHO STATE that existed when the block was entered. The one exception is the FOR DO clause uses the current ECHO state that exists at the beginning of each iteration. The extra FOR statement is the only way to get the REM statement to be echoed.
Dave Benham


wunderbar! that was a great explanation...

now that i know...

thanks again Dave

Post Reply