How to make an underscore variable "-----" with the length of the argument variable?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PiotrMP006
Posts: 29
Joined: 08 Sep 2017 06:10

How to make an underscore variable "-----" with the length of the argument variable?

#1 Post by PiotrMP006 » 15 Dec 2023 08:55

How to make an underscore variable "-----" with the length of the argument variable?

ex.

Code: Select all

Script Name
-----------
Please help

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: How to make an underscore variable "-----" with the length of the argument variable?

#2 Post by Squashman » 15 Dec 2023 12:13

Start by using the String Length function from the Dostips Library.
https://www.dostips.com/DtTipsStringOpe ... ion.strLen

Then you have a few options from there.
You could use a for /l loop to build the underscore variable to the correct length.
You could also have a predefined underscore variable with 100 underscores and then just substring that variable.

Essentially you can use the code that Antonio gave you here.
viewtopic.php?f=3&t=10500&p=67379#p67391

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: How to make an underscore variable "-----" with the length of the argument variable?

#3 Post by Batcher » 15 Dec 2023 21:33

1.bat

Code: Select all

@echo off
echo Script Name
call :MakeUnderscore 11
echo,
echo Hello World
pause
exit /b

:MakeUnderscore
for /l %%i in (1,1,%1) do (
    set /p =-<nul
)

Batcher
Posts: 74
Joined: 16 Apr 2009 10:36

Re: How to make an underscore variable "-----" with the length of the argument variable?

#4 Post by Batcher » 15 Dec 2023 21:38

2.bat

Code: Select all

@echo off
call :MakeUnderscore "Script Name"
echo,
echo Hello World
pause
exit /b

:MakeUnderscore
echo,%~1
for /f "skip=1 delims=:" %%a in ('^(echo "%~1"^&echo.^)^|findstr /o ".*"') do (
    set /a StrLen=%%a-5
)
for /l %%i in (1,1,%StrLen%) do (
    set /p =-<nul
)

IcarusLives
Posts: 164
Joined: 17 Jan 2016 23:55

Re: How to make an underscore variable "-----" with the length of the argument variable?

#5 Post by IcarusLives » 16 Dec 2023 05:22

Here is the second concept that Squashman recommended where you take a long buffer of "----" and substring to the correct length, saving you time on doing a for loop each time.

Code: Select all

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set "hiddenChars=----------------------------------------------------------------------------------------------------"




	set "string=Hello World"

	call :strLen "%string%" out

	echo %string%
	echo !hiddenChars:~0,%out%!



pause & exit





:strLen
(
    set "str=X%~1"
	set "length=0"
	for /l %%b in (10,-1,0) do (
		set /a "length|=1<<%%b"
		for %%c in (!length!) do if "!str:~%%c,1!" equ "" (
			set /a "length&=~1<<%%b"
		)
	)
)
IF "%~2" NEQ "" SET /a %~2=%length%
EXIT /b

Squashman
Expert
Posts: 4471
Joined: 23 Dec 2011 13:59

Re: How to make an underscore variable "-----" with the length of the argument variable?

#6 Post by Squashman » 16 Dec 2023 21:57

“If you give a man a fish, you feed him for a day. If you teach a man to fish, you feed him for a lifetime.”

Post Reply