cmd-Terminal menu: How to iterate a number of spaces given in a variable into another variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
dimarco76
Posts: 3
Joined: 29 May 2023 06:14

cmd-Terminal menu: How to iterate a number of spaces given in a variable into another variable?

#1 Post by dimarco76 » 06 Jun 2023 06:30

Hello together,

I am working on a new updating-menu in cmd-Terminal and don't know how to get my blocks "installed version", "server version" and "update-suggestion" into a semi-variable position on screen, so that it is screen-printed like it would be set in tabbed-fixed columns. The length of the version numbers differs, so it needs to be variable. Here is a preview how it should look alike (just as example, not in the right dimensions):

Code: Select all

***************************************************************************************************************************
* no  program				installed version		server version			update?           *
***************************************************************************************************************************
*                                                                                                                         *
* 1.  GitforWin Portable		xxx.xxx.xxx_pre-windows.1	yyy.yyy.yyy			X                 *
* 2.  Program B				x.xxx.x				y.yyy.y-prewiev1		X                 *
* 3.  Program C				x.xx				x.xx				-                 *
* 4.  Program D				xx.xxx.xxx							ERROR             *
* ...                                                                                                                     *
*                                                                                                                         *
***************************************************************************************************************************
All programs, different versions and the status of the "update?" are determined and set into variables before the menu opens. It's a CHOICE-menu.
My idea is the following:

Fix are only the total amount of possible characters in the inner menu (possible characters within the *) and the total lenght of the desired "columns". Lets say the first column is 4 characters long (no), the second column (program) to the last column (update?) all have a size of 30 characters. --> 4 columns * 30 characters = 120 characters + 4 characters ("no" column) = 124 characters + 4 characters ("* "-preceeding sequence and " *"-trailing sequence) = 128 characters in total (and 124 to use).

So firstly I need to check the "length in characters" of the variables "program1", "vers_inst1", "vers_serv1" and "update_flag1". Then I need to calculate the "fixed total lengh of one column" = 30 characters minus "length in characters" of one variable and write it into a new variable. Until here I'll be able to get it working.

In the second step I need to itarate the result (30 characters minus "length in characters") as space-characters into another variable. How can I do this?
When the result is 16 I need to set a new variable holding exact 16 space-characters.
In the end the menu-line looks like: * 1. %program1%%space1prog1%%vers_inst1%%space2prog1%%vers_serv1%%space3prog1%%update_flag1%%space4prog1% *

Any ideas how to manage it or is there a more comfortable way to archieve the same result?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: cmd-Terminal menu: How to iterate a number of spaces given in a variable into another variable?

#2 Post by ShadowThief » 06 Jun 2023 14:13

Truncating the output is the best move here, especially since Terminal doesn't respect the mode con command so you can't resize the window on the fly.

miskox
Posts: 633
Joined: 28 Jun 2010 03:46

Re: cmd-Terminal menu: How to iterate a number of spaces given in a variable into another variable?

#3 Post by miskox » 07 Jun 2023 23:44

You could use something like this:

Code: Select all

@echo off
set text1=Some text&rem
set text2=Some more text&rem
set text3=Additional text&rem

set field1=%text1%                    &rem 20 spaces
set field2=%text2%                         &rem 25 spaces
set field3=%text3%                               &rem 30 spaces

set field1=%field1:~0,20%
set field2=%field2:~0,25%
set field3=%field3:~0,30%

REM            1         2          1         2               1         2         3
REM   12345678901234567890 1234567890123456789012345 123456789012345678901234567890
echo +--------------------+-------------------------+------------------------------+
echo !%field1%!%field2%!%field3%!
echo +--------------------+-------------------------+------------------------------+

Output:

Code: Select all

c:\some_batch.cmd
+--------------------+-------------------------+------------------------------+
!Some text           !Some more text           !Additional text               !
+--------------------+-------------------------+------------------------------+
c:\
Of course this code takes first 'n' characters. You could take the last 'n' characters if you want your text to be aligned on the right.

You could even do this:

Let's say you have a total space of 30 spaces and you have a string that is 40 characters long but you need the beggining and the end of the string. So you could make output string to look like this:

Sentense: "How to iterate a number of spaces given in a variable into another variable?"

Output:

Code: Select all

How to iterate a num...other variable?
Saso

Edit: changed last example.

dimarco76
Posts: 3
Joined: 29 May 2023 06:14

Re: cmd-Terminal menu: How to iterate a number of spaces given in a variable into another variable?

#4 Post by dimarco76 » 08 Jun 2023 10:05

Thank you both for your answers. The text manipulation method is a good alternative.

@miskox
Is it possible to include the string length (string manipulation) from a variable to automate it?
set field1=%field1:~0,20%
to
set field1=%field1:~0,%num1%%

Or echo it and include the echoed result within the string manipulation? I get syntax errors trying it or the cmd crashes.

I also tried the following codes to get it done:

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "num1= "
set num2=10 (in my example num2 is calculated)
set "out="
for /L %%i in (1,1,%num2%) do set "out=!out!%num1%"
echo %out%
pause
This leads to error "Echo is off" using spaces, but when I use the code with another character like set="num1=_" it works like a charm.

Maybe that are syntax errors.

I also had a look at this post:
https://stackoverflow.com/questions/956 ... r-of-times
Doesn't work with spaces.

The only possibility to create or print spaces in a row and accepting variables I got to work is using the perl command, but that isn't original windows batch.

@echo off
set number=10
perl -E 'say " " x %number%' > test.txt
pause
Expected result of 10 spaces gets written into a textfile. So it is also possible to set the command output into a new variable.

Any ideas using windows batch commands?

dimarco

dimarco76
Posts: 3
Joined: 29 May 2023 06:14

Re: cmd-Terminal menu: How to iterate a number of spaces given in a variable into another variable?

#5 Post by dimarco76 » 08 Jun 2023 13:15

I just found an old answer of dbenham in the forum of superuser...
https://superuser.com/questions/629596/ ... batch-file

@miskox: So your answer seems to be the perfect solution. ;-)
I just misunderstood, that in your solution, I only need to know the position of text, not the variable text and space length inbetween. Never heard about such a great idea. dbenham seems to be a batch giant.
And thank you very much, without your answer maybe I didn't find the explanations of dbenham which I needed to understand.
I need to think about it.
May I ask you again about some background if somethink is unclear?

By the way: I also found another answer of dbenham about SET /P and leading spaces that is really interesting:
https://stackoverflow.com/questions/986 ... mpt-string
Its a bit off-topic, but may help other users in the handling of space.

miskox
Posts: 633
Joined: 28 Jun 2010 03:46

Re: cmd-Terminal menu: How to iterate a number of spaces given in a variable into another variable?

#6 Post by miskox » 11 Jun 2023 10:26

dimarco76 wrote:
08 Jun 2023 10:05
@miskox
Is it possible to include the string length (string manipulation) from a variable to automate it?
Use this:

Code: Select all

@echo off
set txt=The quick brown fox jumps over the lazy dog.

echo          1         2         3         4
echo 12345678901234567890123456789012345678901234
echo The quick brown fox jumps over the lazy dog.
echo --------------------------------------------

set number_of_characters=10
call set output_string=%%txt:~0,%number_of_characters%%%
echo %output_string%

set number_of_characters=20
call set output_string=%%txt:~0,%number_of_characters%%%
echo %output_string%

set number_of_characters=30
call set output_string=%%txt:~0,%number_of_characters%%%
echo %output_string%

set number_of_characters=40
call set output_string=%%txt:~0,%number_of_characters%%%
echo %output_string%

echo(
echo(



Output:

Code: Select all

c:\>somecmd.cmd
         1         2         3         4
12345678901234567890123456789012345678901234
The quick brown fox jumps over the lazy dog.
--------------------------------------------
The quick
The quick brown fox
The quick brown fox jumps over
The quick brown fox jumps over the lazy


c:\>


Post Reply