Page 1 of 1
How to make an underscore variable "-----" with the length of the argument variable?
Posted: 15 Dec 2023 08:55
by PiotrMP006
How to make an underscore variable "-----" with the length of the argument variable?
ex.
Please help
Re: How to make an underscore variable "-----" with the length of the argument variable?
Posted: 15 Dec 2023 12:13
by Squashman
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
Re: How to make an underscore variable "-----" with the length of the argument variable?
Posted: 15 Dec 2023 21:33
by Batcher
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
)
Re: How to make an underscore variable "-----" with the length of the argument variable?
Posted: 15 Dec 2023 21:38
by Batcher
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
)
Re: How to make an underscore variable "-----" with the length of the argument variable?
Posted: 16 Dec 2023 05:22
by IcarusLives
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
Re: How to make an underscore variable "-----" with the length of the argument variable?
Posted: 16 Dec 2023 21:57
by Squashman
“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.”