Create Diamond Batch Script

I’m trying to create a diamond shape in a batch script, but I’m only able to generate the right half. The left part does not display as expected. Here’s the code I’m using:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@echo off
cls
setlocal enabledelayedexpansion
set "max=10"
for /L %%i in (1,1,%max%) do (
set "line="
set "spaces="
rem Create spaces
for /L %%j in (1,1,%max% - %%i) do set "spaces=!spaces! "
rem Create stars
for /L %%j in (1,1,%%i * 2 - 1) do set "line=!line!*"
echo !spaces!!line!
)
for /L %%i in (%max%,-1,1) do (
set "line="
set "spaces="
rem Create spaces
for /L %%j in (1,1,%max% - %%i) do set "spaces=!spaces! "
rem Create stars
for /L %%j in (1,1,%%i * 2 - 1) do set "line=!line!*"
if %%i lss %max% echo !spaces!!line!
)
endlocal
pause
</code>
<code>@echo off cls setlocal enabledelayedexpansion set "max=10" for /L %%i in (1,1,%max%) do ( set "line=" set "spaces=" rem Create spaces for /L %%j in (1,1,%max% - %%i) do set "spaces=!spaces! " rem Create stars for /L %%j in (1,1,%%i * 2 - 1) do set "line=!line!*" echo !spaces!!line! ) for /L %%i in (%max%,-1,1) do ( set "line=" set "spaces=" rem Create spaces for /L %%j in (1,1,%max% - %%i) do set "spaces=!spaces! " rem Create stars for /L %%j in (1,1,%%i * 2 - 1) do set "line=!line!*" if %%i lss %max% echo !spaces!!line! ) endlocal pause </code>
@echo off
cls

setlocal enabledelayedexpansion

set "max=10"

for /L %%i in (1,1,%max%) do (
    set "line="
    set "spaces="
    rem Create spaces
    for /L %%j in (1,1,%max% - %%i) do set "spaces=!spaces! "
    rem Create stars
    for /L %%j in (1,1,%%i * 2 - 1) do set "line=!line!*"
    echo !spaces!!line!
)

for /L %%i in (%max%,-1,1) do (
    set "line="
    set "spaces="
    rem Create spaces
    for /L %%j in (1,1,%max% - %%i) do set "spaces=!spaces! "
    rem Create stars
    for /L %%j in (1,1,%%i * 2 - 1) do set "line=!line!*"
    if %%i lss %max% echo !spaces!!line!
)

endlocal
pause

Batch doesn’t let you do in-line math like you have in the four for loops. You’ll have to create a separate variable first and then use that instead.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@echo off
cls
setlocal enabledelayedexpansion
set "max=10"
for /L %%i in (1,1,%max%) do (
set "line="
set "spaces="
rem Create spaces
set /a space_max=max-%%i
for /L %%j in (1,1,!space_max!) do set "spaces=!spaces! "
rem Create stars
set /a star_max=%%i*2-1
for /L %%j in (1,1,!star_max!) do set "line=!line!*"
echo !spaces!!line!
)
for /L %%i in (%max%,-1,1) do (
set "line="
set "spaces="
rem Create spaces
set /a space_max=max-%%i
for /L %%j in (1,1,!space_max!) do set "spaces=!spaces! "
rem Create stars
set /a star_max=%%i*2-1
for /L %%j in (1,1,!star_max!) do set "line=!line!*"
if %%i lss %max% echo !spaces!!line!
)
endlocal
pause
</code>
<code>@echo off cls setlocal enabledelayedexpansion set "max=10" for /L %%i in (1,1,%max%) do ( set "line=" set "spaces=" rem Create spaces set /a space_max=max-%%i for /L %%j in (1,1,!space_max!) do set "spaces=!spaces! " rem Create stars set /a star_max=%%i*2-1 for /L %%j in (1,1,!star_max!) do set "line=!line!*" echo !spaces!!line! ) for /L %%i in (%max%,-1,1) do ( set "line=" set "spaces=" rem Create spaces set /a space_max=max-%%i for /L %%j in (1,1,!space_max!) do set "spaces=!spaces! " rem Create stars set /a star_max=%%i*2-1 for /L %%j in (1,1,!star_max!) do set "line=!line!*" if %%i lss %max% echo !spaces!!line! ) endlocal pause </code>
@echo off
cls

setlocal enabledelayedexpansion

set "max=10"

for /L %%i in (1,1,%max%) do (
    set "line="
    set "spaces="
    rem Create spaces
    set /a space_max=max-%%i
    for /L %%j in (1,1,!space_max!) do set "spaces=!spaces! "
    rem Create stars
    set /a star_max=%%i*2-1
    for /L %%j in (1,1,!star_max!) do set "line=!line!*"
    echo !spaces!!line!
)

for /L %%i in (%max%,-1,1) do (
    set "line="
    set "spaces="
    rem Create spaces
    set /a space_max=max-%%i
    for /L %%j in (1,1,!space_max!) do set "spaces=!spaces! "
    rem Create stars
    set /a star_max=%%i*2-1
    for /L %%j in (1,1,!star_max!) do set "line=!line!*"
    if %%i lss %max% echo !spaces!!line!
)

endlocal
pause

A simpler approach is to prebuild some long strings and take what you need via substring modification when you iterate through the diamonds width:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@echo off
Setlocal EnableDelayedExpansion
Set /a max=10
Set "Spaces= "
Set "Spaces=!spaces: = !"
Set "Spaces=!spaces:~0,%max%! "
Set "Char=!spaces: =*!"
For %%_ in ("!max! -1 1" "2 1 !max!")do For /l %%i in (%%~_)Do (
Echo(!Spaces:~-%%i!!Char:~%%i!!Char:~%%i,-1!
)
Pause
Endlocal & goto:eof
</code>
<code>@echo off Setlocal EnableDelayedExpansion Set /a max=10 Set "Spaces= " Set "Spaces=!spaces: = !" Set "Spaces=!spaces:~0,%max%! " Set "Char=!spaces: =*!" For %%_ in ("!max! -1 1" "2 1 !max!")do For /l %%i in (%%~_)Do ( Echo(!Spaces:~-%%i!!Char:~%%i!!Char:~%%i,-1! ) Pause Endlocal & goto:eof </code>
@echo off
Setlocal EnableDelayedExpansion
Set /a max=10
Set "Spaces=                  "
Set "Spaces=!spaces: =       !"
Set "Spaces=!spaces:~0,%max%! "
Set "Char=!spaces: =*!"

For %%_ in ("!max! -1 1" "2 1 !max!")do For /l %%i in (%%~_)Do (
  Echo(!Spaces:~-%%i!!Char:~%%i!!Char:~%%i,-1!
)

Pause
Endlocal & goto:eof

The most efficient approach is to use simple instructions and make good use of every instruction to perform the most possible operations. The simplest way to show variable-length strings is via substring expansion. In this solution a single FOR is used to assemble both the spaces string and the indices used in the second part.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@echo off
setlocal EnableDelayedExpansion
set "max=10"
for /L %%i in (1,1,%max%) do (
set "spaces=!spaces! "
set "indices=%%i !indices! %%i"
)
set "indices=%indices: 1=%"
set "line=%spaces: =*%"
for %%i in (%indices%) do echo !spaces:~-%%i!!line:~%%i!*!line:~%%i!
</code>
<code>@echo off setlocal EnableDelayedExpansion set "max=10" for /L %%i in (1,1,%max%) do ( set "spaces=!spaces! " set "indices=%%i !indices! %%i" ) set "indices=%indices: 1=%" set "line=%spaces: =*%" for %%i in (%indices%) do echo !spaces:~-%%i!!line:~%%i!*!line:~%%i! </code>
@echo off
setlocal EnableDelayedExpansion

set "max=10"

for /L %%i in (1,1,%max%) do (
   set "spaces=!spaces! "
   set "indices=%%i !indices! %%i"
)
set "indices=%indices:  1=%"
set "line=%spaces: =*%"

for %%i in (%indices%) do echo !spaces:~-%%i!!line:~%%i!*!line:~%%i!

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật