How to replace the script name %~n0 with a string of "-" characters of length %~n0?
Code: Select all
ex. %~n0 = Test.bat
How do I change "Test.bat" to the string "---------" ?
Test.bat
--------
Code: Select all
ex. %~n0 = Test.bat
How do I change "Test.bat" to the string "---------" ?
Test.bat
--------
Code: Select all
SET "mystr=-----------------------------------------------------------------------------------"
Code: Select all
@echo off
setlocal enabledelayedexpansion
:: Get script name
set "scriptName=%~nx0"
:: Calculate script name length
call :strLen scriptName scriptNameLen
echo %scriptName% is %scriptNameLen% characters long
pause
:: Clear screen
cls
:: Display script name with underscores
echo %scriptName%
call :MakeUnderscore %scriptNameLen%
pause
exit /b
::---------------------------------------
:: Function to calculate string length
:strLen
setlocal enabledelayedexpansion
set "str=!%~1!"
set "len=0"
:strLen_Loop
if defined str (
set "str=!str:~1!"
set /A len+=1
goto :strLen_Loop
)
(endlocal & set %2=%len%)
exit /b
::---------------------------------------
:: Function to display underscores
:MakeUnderscore
set "underscore="
for /l %%i in (1,1,%1) do (
set "underscore=_!underscore!"
)
echo %underscore%
exit /b
::---------------------------------------