Repeat text pattern batch programme

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ecmg
Posts: 1
Joined: 05 Apr 2014 23:03

Repeat text pattern batch programme

#1 Post by ecmg » 05 Apr 2014 23:15

I am trying to create a Batch script to automatically output repeated text based on the input parameters.
for example: I have saved it in a file named pat2.bat in C:\TEMP

If I enter the command as below:
C:\TEMP>pat2 1 3 *
I get the following output
*
**
***
len%
len%

however I want something like
*
**
***
**
*

Following is the code I am using. The first loop works, however 2nd loop is not :(

Code: Select all

@echo off 
setlocal EnableDelayedExpansion
set /a w=%~1
set /a h=%~2
set char=%~3

:Loop
SET /a ln=%ln%+1
FOR /L %%i IN (1,1,%w%) DO set res=!res!%char%
echo %res%
if not %ln%==%h% goto loop
:loop1
SET /a ln=%ln%-1
FOR /L %%i IN (%w%,-1,2) DO set /a res=!res!%char%
SET len=%%i
SET res1=%res:~0,%len%%
echo %res1%
if not %ln%==1 goto loop1
endlocal

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

Re: Repeat text pattern batch programme

#2 Post by Squashman » 06 Apr 2014 00:28

The second for loop is basically not executing because it is expanding to this.

Code: Select all

FOR /L %%i IN (1,-1,2) DO


This is also not going to work.

Code: Select all

set /a res=!res!%char%

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

Re: Repeat text pattern batch programme

#3 Post by Squashman » 06 Apr 2014 00:42

How about his instead.

Code: Select all

@echo off 
setlocal EnableDelayedExpansion
set /a w=%~1
set /a h=%~2
set char=%~3

:Loop
SET /a ln=%ln%+1
FOR /L %%i IN (1,1,%w%) DO set res=!res!%char%
echo %res%
if not %ln%==%h% goto loop
:loop1
SET /a ln=%ln%-1
FOR /L %%i IN (%ln%,-1,2) DO set res=!res!%char%
SET res1=!res:~0,%ln%!
echo %res1%
if not %ln%==1 goto loop1
endlocal


Output

Code: Select all

C:\Batch>ecmg.bat 1 3 *
*
**
***
**
*

C:\Batch>ecmg.bat 1 6 *
*
**
***
****
*****
******
*****
****
***
**
*

C:\Batch>

Aacini
Expert
Posts: 1885
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Repeat text pattern batch programme

#4 Post by Aacini » 06 Apr 2014 10:23

Code: Select all

@echo off 
setlocal EnableDelayedExpansion

set /a w=%~1, h=%~2
set char=%~3

for /L %%l in (1,1,%h%) do (
   FOR /L %%i IN (1,1,%w%) DO set res=!res!%char%
   echo !res!
)

for /L %%l in (%h%,-1,2) do (
   set res=!res:~0,-%w%!
   echo !res!
)

Output:

Code: Select all

C:\> test 1 3 *
*
**
***
**
*

C:\> test 2 6 *
**
****
******
********
**********
************
**********
********
******
****
**

Post Reply