Dmitry Ryumin
Dmitry Ryumin

Reputation: 33

How to make a character line across the width of the command line?

How to make a character line across the width of the command line Windows?

In Linux, I can do like this

printf '\n%*s\n\n' \"${COLUMNS:-$(tput cols)}\" '' | tr ' ' -

In Windows, I have so far only

echo -----------------------

Upvotes: 1

Views: 216

Answers (3)

lit
lit

Reputation: 16256

If you are on a supported version of Windows, this can easily be done using PowerShell. PowerShell also runs on Linux/*NIX and Mac.

powershell -NoLogo -NoProfile -Command "'-' * $Host.UI.RawUI.WindowSize.Width"

Upvotes: 1

Stephan
Stephan

Reputation: 56188

you can get the current width of your window with the mode command.

@echo off
setlocal enabledelayedexpansion
"set width="
for /f "tokens=2 delims=:" %%a in ('mode con^|more +4') do if not defined width set /a width=%%a
for /l %%a in (1,1,%width%) do set "line=!line!-"
echo %line%

Upvotes: 4

Gh3ttoKinG.
Gh3ttoKinG.

Reputation: 114

I only know a workaround for it by determining the width of the command line window and repeating the character ofter enough. Since I don't know if you only want it in the command prompt oder in a batch file I post what I made while ago for me. It only works in a batch file or when you save the second part in a batch file and call it in a command prompt window.

:RepeatChar <Char> <Count> <Variable>
setlocal enabledelayedexpansion
set tempRepChar=
for /L %%l in (1,1,%~2) do (
  set tempRepChar=!tempRepChar!%~1
)
if /i "%~3"=="" (
  echo %tempRepChar%
) else (
  set %~3=%tempRepChar%
  set tempRepChar=
)
goto :EOF
exit /b

(the extra exit /b in the RepeatChar function isn't really necessary, but I just do it for myself)

You can then call it within a batch file with

for /f %%f in ('powershell.exe -command $host.UI.RawUI.WindowSize.Width') do set WindowsWidth=%%f
call :RepeatChar "-" %WindowsWidth% Stipline
echo %Stripline%
exit /b

if you don't give it the 3rd parameter then it just echos the line so if you only need it once you can just use

call :RepeatChar "-" %WindowsWidth% 

Or you can also store or use it via a for loop, like

for /f %%f in ('powershell.exe -command $host.UI.RawUI.WindowSize.Width') do set WindowsWidth=%f
for /f %%f in ('call temp.bat "-" "%WindowsWidth%"') do (
  echo %%f
  set Stripline=%%f
)

Upvotes: 0

Related Questions