Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to pad a variable with space character to the left and or right

2,813 views
Skip to first unread message

Batch Newbie

unread,
Nov 17, 2004, 3:25:22 PM11/17/04
to
I started a thread a couple weeks earlier:
"How to position echo output of variables"

Got some excellent advise from Phil, Matthias, and Herbert.

After some thought, I figure the best solution would be to pad the
variables so the variables would be a certain length. Padding on the
left for right justification) and padding to the right for left
justification.

I found an EXCELLENT thread on the subject:
(2000/09/26) - How to pad a variable with zeros?
http://groups.google.com/groups?hl=en&lr=&frame=right&th=acf3422e71a5643f&seekm=39D2451D.273A84A2%40uclink4.berkeley.edu#link1

In that thread, Tom Lavedas came up with a simple batch solution
compatible with NT4:

01. set _Num=%1 000000
02. set _Num=%_Num:~0,7%
03. for /f "tokens=1,2" %%a in ("%_Num%") do set _Num=%%b%%a
04. echo %_Num%

This code works well and you can change the pad character in line 01
and change right/left justification in line 03.

Unfortunately, I can't figure out how to get it to work (if even
possible) using SPACES for the padding. Any tips?

Thanks!
Huy

Matthias Tacke

unread,
Nov 17, 2004, 4:09:53 PM11/17/04
to
Batch Newbie schrieb:

> I started a thread a couple weeks earlier:
> "How to position echo output of variables"
>
> Got some excellent advise from Phil, Matthias, and Herbert.
>
> 01. set _Num=%1 000000
> 02. set _Num=%_Num:~0,7%
> 03. for /f "tokens=1,2" %%a in ("%_Num%") do set _Num=%%b%%a
> 04. echo %_Num%
>

Standard delims is space, change it to any uncommon char

set "_Num=%1° "
set _Num=%_Num:~0,7%
for /f "tokens=1,2 delims=°" %%a in ("%_Num%") do set _Num=%%b%%a
echo %_Num%

HTH

--
Gruesse Greetings Saludos Saluti Salutations
Matthias
---------+---------+---------+---------+---------+---------+---------+

Todd Vargo

unread,
Nov 17, 2004, 9:25:43 PM11/17/04
to

"Batch Newbie" <h...@exitentrance.com> wrote in message
news:81b6cb1b.0411...@posting.google.com...

If you are trying to return a string of x characters, you need to pad the
variable with at least that many characters. I don't know what Tom was
trying to accomplish with the code above, but for just plain padding, the
code below is all that is really needed. For further info, type SET/?.

::For padding with spaces to the left
set _Num= %1
set _Num=%_Num:~-7%
echo _num=[%_num%]


::For padding with spaces to the right
set _Num=%1 .
set _Num=%_Num:~0,7%
echo _num=[%_num%]

--
Todd Vargo (double "L" to reply by email)

Pegasus (MVP)

unread,
Nov 18, 2004, 8:06:35 AM11/18/04
to

"Batch Newbie" <h...@exitentrance.com> wrote in message
news:81b6cb1b.0411...@posting.google.com...

Use a padding character that you know will never occur in
your strings, then change it back to a space:

set Num=%1 ببببببب
set Num=%_Num:~0,7%
for /f "tokens=1,2" %%a in ("%Num%") do set _Num=%%b%%a
set Num=%Num:ب= %
echo %Num%


Batch Newbie

unread,
Nov 18, 2004, 9:57:51 AM11/18/04
to
Hey Matthias,

I forgot about the default delims (is space). Had I remembered, I
probably wouldn't of been able to modify the coding correctly as you
have. Your approach was what exactly what I had in mind.

Embarresed to show my pathetic approach. I used the original code by
Tom and then stuck in code posted by Phil that searches and replaces a
character with space. It worked but it was bloated and slow (at least
I learned some new tricks). here is my test code:

@echo off

set var1=tester
set pad1=%var1:~%
call :pad
set var1=%pad2:~%

set var2=fine
set pad1=%var2:~%
call :pad
set var2=%pad2:~%

goto :EOF

:pad
set pad1=%pad1:~% **********
set pad1=%pad1:~0,11%
for /f "tokens=1,2" %%a in ("%pad1%") do set pad1=%%b%%a
rem for /f "tokens=1,2" %%a in ("%pad1%") do set pad1=%%a%%b

set pad1=%pad1:~%
set pad1=%pad1: =`%
set pad2=
set /a counter=-1

:nxtchar
set /a counter+=1
call :EXEC set padchar=%%pad1:~%counter%,1%%%
if not defined padchar goto :done
if [%padchar%] EQU [*] set padchar=`
set pad2=%pad2%%padchar%
goto :nxtchar

:done
set pad2=%pad2:`= %
echo 123456789012345678901234567890
echo %pad1%
echo %pad2%%pad1%
goto :EOF

:EXEC
%*
goto :EOF

Matthias Tacke <Matt...@Tacke.de> wrote in message news:<cngen4$i6t$02$1...@news.t-online.com>...

Batch Newbie

unread,
Nov 18, 2004, 10:19:04 AM11/18/04
to
Thanks for your input Todd.. your code seems to make sense in
appearance but doesn't seem to work under Windows NT4. The result is:
C:\>padtest.bat 1
_num=[]

Matthias reminded me about the delims and space issue and his addition
works perfectly. Tom's approach pads a variable (with chars) so it's
x characters long (exactly what the OP of Tom's thread wanted).
Matthias' addendum allows for the use of space chars.

Working test code (brackets not necessary but useful to track spacing:

01. @echo off
02. set "_Num=%1* "
03. set _Num=%_Num:~0,11%
04. for /f "tokens=1,2 delims=*" %%a in ("%_Num%") do set _Num=%%b%%a
05. echo 123456789012345678901234567890
06. echo %_Num%

Padding variables with space chars will help in generating results in
column format without a text file output. Phil had contributed an
interesting code that helped another poster take a text file and
display it in a nice columnar format. Unfortunately I couldn't figure
out how to adapt his code to what I wanted.

For other and future viewers.. here's Phil's code that organizes a
text file to a column format with right justification:

From: Phil Robyn
Subject: Re: Is it possibe to format "echo" output?
Newsgroups: alt.msdos.batch.nt
Date: 2003-12-09 11:43:21 PST

By 'NT', I am assuming you mean 'NT 4.0' as opposed to Win2000 or
WinXP.

C:\cmd>type %temp%\Test1234.txt
000182 6 28 IFSPool
000194 16 47 Monthly
000220 1 782 NetBackup
000223 2 71 Monthly
000240 4 8 NetBackup

C:\cmd>demo\PadColumns
000182 6 28 IFSPool
000194 16 47 Monthly
000220 1 782 NetBackup
000223 2 71 Monthly
000240 4 8 NetBackup

This one is for Win2000 or WinXP:
=====begin C:\cmd\demo\PadColumns.cmd ====================
01. @echo off
02. setlocal
03. for /f "tokens=1-4" %%a in (
04. c:\temp\Test1234.txt
05. ) do call :display %%a %%b %%c %%d
06. goto :EOF
07. :display
08. set col1=%1
09. set col2= %2
10. set col3= %3
11. set col4=%4
12. echo %col1% %col2:~-2% %col3:~-3% %col4%
13. goto :EOF
=====end C:\cmd\demo\PadColumns.cmd ====================

C:\cmd>demo\PadColumnsNT
000182 6 28 IFSPool
000194 16 47 Monthly
000220 1 782 NetBackup
000223 2 71 Monthly
000240 4 8 NetBackup

This one is for NT 4.0:
=====begin C:\cmd\demo\PadColumnsNT.cmd ====================
01. @echo off
02. setlocal
03. for /f "tokens=1-4" %%a in (
04. c:\temp\Test1234.txt
05. ) do call :display %%a %%b %%c %%d
06. goto :EOF
07. :display
08. set col1=%1
09. set col2=%2$@
10. set col2=%col2:~0,3%
11. set col3=%3$@@
12. set col3=%col3:~0,4%
13. set col4=%4
14. for /f "tokens=1-2 delims=$" %%a in (
15. 'echo %col2%'
16. ) do set col2=%%b%%a
17. set col2=%col2:@= %
18. for /f "tokens=1-2 delims=$" %%a in (
19. 'echo %col3%'
20. ) do set col3=%%b%%a
21. set col3=%col3:@= %
22. echo %col1% %col2% %col3% %col4%
23. goto :EOF
=====end C:\cmd\demo\PadColumnsNT.cmd ====================


"Todd Vargo" <todd...@alvantage.com> wrote in message news:<302fveF...@uni-berlin.de>...

guard

unread,
Nov 20, 2004, 10:02:24 AM11/20/04
to
"Batch Newbie" <h...@exitentrance.com> wrote in message
news:81b6cb1b.04111...@posting.google.com...

> Thanks for your input Todd.. your code seems to make sense in
> appearance but doesn't seem to work under Windows NT4. The result is:
> C:\>padtest.bat 1
> _num=[]
>
> Matthias reminded me about the delims and space issue and his addition
> works perfectly. Tom's approach pads a variable (with chars) so it's
> x characters long (exactly what the OP of Tom's thread wanted).
> Matthias' addendum allows for the use of space chars.
>
> Working test code (brackets not necessary but useful to track spacing:
>
> 01. @echo off
> 02. set "_Num=%1* "
> 03. set _Num=%_Num:~0,11%
> 04. for /f "tokens=1,2 delims=*" %%a in ("%_Num%") do set _Num=%%b%%a
> 05. echo 123456789012345678901234567890
> 06. echo %_Num%
>
<SNIP>

You could also use the $PAD Function from the Expert Command Library.

CALL NTCmdLib.cmd /Quiet $PAD "%1" 10
ECHO:%_PAD%

*******

More examples at
(http://TheSystemGuard.com/NTCmdLib/Functions/PAD.htm)


-tsg

/-----------------+---------------+----------------------\
| COMPATIBILITY | CLARITY | SPEED |
| Write code ONCE | Make it clear | THEN...Make it fast! |
\-----------------+---------------+----------------------/
400+ command-line resources using ONLY native NT commands!
(http://TheSystemGuard.com/default.asp#MasterCommandList)


Phil Robyn

unread,
Nov 21, 2004, 10:29:14 AM11/21/04
to
Batch Newbie wrote:
> Thanks for your input Todd.. your code seems to make sense in
> appearance but doesn't seem to work under Windows NT4. The result is:
> C:\>padtest.bat 1
> _num=[]
>
> Matthias reminded me about the delims and space issue and his addition
> works perfectly. Tom's approach pads a variable (with chars) so it's
> x characters long (exactly what the OP of Tom's thread wanted).
> Matthias' addendum allows for the use of space chars.
>
> Working test code (brackets not necessary but useful to track spacing:
>
> 01. @echo off
> 02. set "_Num=%1* "
> 03. set _Num=%_Num:~0,11%
> 04. for /f "tokens=1,2 delims=*" %%a in ("%_Num%") do set _Num=%%b%%a
> 05. echo 123456789012345678901234567890
> 06. echo %_Num%
>
> Padding variables with space chars will help in generating results in
> column format without a text file output. Phil had contributed an
> interesting code that helped another poster take a text file and
> display it in a nice columnar format. Unfortunately I couldn't figure
> out how to adapt his code to what I wanted.

Hi, Batch Newbie!

Sorry I haven't been contributing to this thread, but I just returned
from ten days in Beijing, so it will take me a while to catch up.
Matthias, thanks for providing the NT4.0 solution to BN's original
post. BN, let us know if you *still* haven't solved your problem(s)
:-)

--
Phil Robyn
Univ. of California, Berkeley

u n z i p m y a d d r e s s t o s e n d e - m a i l

Batch Newbie

unread,
Nov 22, 2004, 1:56:13 PM11/22/04
to
Hi Phil,

The people of Beijing China were lucky to have you around!

Sorry? I should be sorry I'll never be able to program as well as you
guys... but thankful you guys are willing to take the time to help out
others no matter how basic the question. I find it fun browsing the
programming threads and learning the creative ways around problems.

For fun.. I've created lots of batch tools to help automate the
execution of several repetive tasks here. Batch is great since its
mostly open and free. Eventually I hope to develop a Gui .exe front
(likely in VB) that:
1. Generates batch file / program
2. Passes variables (entered in the VB gui) to the batch program
3. Displays results of batch program back to a "windows"
display/dialog box.

Basically one solution to encoding batch to .exe.

I haven't started yet but I imagine it should be relatively simple and
will post the source here once done.

Huy

Phil Robyn <zipp...@berkeley.edu> wrote in message news:<30bqikF...@uni-berlin.de>...

0 new messages