Echoing text in same position on screen

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
MKANET
Posts: 160
Joined: 31 Mar 2012 21:31

Echoing text in same position on screen

#1 Post by MKANET » 01 Jun 2012 15:48

What's the simplest way to place the the OS: [Windows Server 2003] below to always be 4 spaces in from the right-most part of an 80column command window (following the preceding text below)?

If %UserInput% (%location%) had a fixed length, I wouldn't have a problem. I do have a little bit of flexibility when using tab spaces; but not good enough. I'm trying to keep the coding to do this as simple as possible to make it easier to integrate this concept into various parts of my large batch file. This is just a very close example of what I'm trying to do.


Code: Select all

Processing:   %UserInput% (%location%)                  OS: [Windows Server 2003]....

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: Echoing text in same position on screen

#2 Post by Fawers » 01 Jun 2012 16:12

Maybe you can use strLen to extract both strings lenght, and then calculate more or less the amout of spaces you need.

edit: link to strLen: http://www.dostips.com/DtCodeCmdLib.php#Function.strLen

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

Re: Echoing text in same position on screen

#3 Post by dbenham » 01 Jun 2012 17:49

You only need to know the length of "OS: [Windows Server 2003]". If it is a constant (always Windows Server 2003), then it is easy. If it can vary then you need a method to compute the length.

Code: Select all

@echo off
setlocal

::**** Begin Initialization **********
::
::Define a var containing 80 spaces
set "spaces=                                                                                "

::Define your constant string that appears to the right
::I'm assuming this can vary from machine to machine,
::but I'm using a constant string here.
set "right=OS: [Windows Server 2003]"

::Define the number of chars available for your output.
::If the right string is constant, then you don't need the strLen routine.
::You could simply hard code a value of 51 in this case.
call :strLen right rightLength
set /a "rightStart=80-rightLength-4"
::
::**** End Initialization ***********

::build a variable length output string.
set /p "UserInput=Enter some text: "
set /p "location=Enter your location: "
set "output=Processing:   %UserInput% (%location%)"

::call a routine to echo the output with the right string properly placed
call :echoOutput output
exit /b

::***** Begin Subroutine definitions *****

:echoOutput  strVar
setlocal enableDelayedExpansion
set "output=!%1!%spaces%
echo !output:~0,%rightStart%!!right!
exit /b

::The following is taken from http://www.dostips.com/DtCodeCmdLib.php#Function.strLen
::
:strLen string len -- returns the length of a string
::                 -- string [in]  - variable name containing the string being measured for length
::                 -- len    [out] - variable to be used to return the string length
:: Many thanks to 'sowgtsoi', but also 'jeb' and 'amel27' dostips forum users helped making this short and efficient
:$created 20081122 :$changed 20101116 :$categories StringOperation
:$source http://www.dostips.com
(   SETLOCAL ENABLEDELAYEDEXPANSION
    set "str=A!%~1!"&rem keep the A up front to ensure we get the length and not the upper bound
                     rem it also avoids trouble in case of empty string
    set "len=0"
    for /L %%A in (12,-1,0) do (
        set /a "len|=1<<%%A"
        for %%B in (!len!) do if "!str:~%%B,1!"=="" set /a "len&=~1<<%%A"
    )
)
( ENDLOCAL & REM RETURN VALUES
    IF "%~2" NEQ "" SET /a %~2=%len%
)
EXIT /b

If the variable output is too long, it will be truncated by the right string. You may not want that. The echoOutput routine can be modified to append the right string one space after the left output if the output is too long:

Code: Select all

:echoOutput  strVar
setlocal enableDelayedExpansion
set "output=!%1!
if "!output:~%rightStart%!" == "" (
  set "output=!output!%spaces%"
  set "output=!output:~0,%rightStart%!!right!"
) else (
  set "output=!output! !right!"
)
echo !output!
exit /b


Dave Benham

Post Reply