Pad unequal length lines with spaces to make all the same.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

Pad unequal length lines with spaces to make all the same.

#1 Post by mid_life_crisis » 17 Jun 2011 11:32

Not having a full grasp of all the commands available, for all I know there might be a command that will do this properly.
Bearing in mind that the line being read in is less than 80 characters, what I have done is read in the short line and write it to the output file defined as a longer line, like this:

set "line=!line:~0,80!"
echo !line!>>outfile.txt

This works in my case, but it somehow seems to leave the possibility of garbage being caught up and written to the file in the additional spaces when I extend the length of the line.
Is this okay, or is there a better way to do it?
Possibly this to ensure no garbage gets picked up?

set "line=!line! " EDIT: the forum editor trimmed this down to one space. I had 80 there.
echo !line:~0,80!>>outfile.txt

Acy Forsythe
Posts: 126
Joined: 10 Jun 2011 10:30

Re: Pad unequal length lines with spaces to make all the sam

#2 Post by Acy Forsythe » 17 Jun 2011 11:45

I'm not sure that's a bad way, but you could do something like this:

Code: Select all

@Echo OFF
::Set Spaces = [80 spaces, but you'd have to count them out or loop and concatenate]
:: The $ are only for display purposes to test that it's functioning.

Set "spaces=                                                                                "
Set "line=Hello There"

Echo %spaces%$
Echo %line%$

Set line=%line%%spaces%
Set line=%line:~0,80%

Echo %line%$


You can get rid of that last set, but I was getting impatient trying to work out the variable expansion of it to get the concatenation and the substring all on the same set line.

EDIT: Ok still trying to work out the expansion on set line=%%line%%space%:~0,80%
The closest I can seem to get is "Hello There [80 spaces] :~0,80$" as my output... or I get "%line%spaces:~0,80$"

I tried Call, but following everyone's direction I seem to get the exact same results no matter how much I call and double-up on %'s.


And to fix your space problem with the forum, use code blocks (code) (/code) except use [ brackets.

mid_life_crisis
Posts: 22
Joined: 26 May 2011 07:56

Re: Pad unequal length lines with spaces to make all the sam

#3 Post by mid_life_crisis » 17 Jun 2011 12:16

The more I think about it, the more I like my second method. It works and it should be clean.
Using the second SET not only sets the value that I want, but also sets the length, which is important because the records are of varying length.

It's nice to have someone else tell me I'm not totally out in left field though, so thank you.

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Pad unequal length lines with spaces to make all the sam

#4 Post by orange_batch » 17 Jun 2011 16:37

mid_life_crisis, first use the [ code ] tags to preserve spaces, give it a fixed-width font and highlight your code.

Whether you're left-aligning or right-aligning text within a single line, set number of lines, or a dynamic number of lines, it changes the difficulty of the code. I programmed a pretty good right-alignment function before. In any case, it's just about padding spaces and then grabbing substrings.

Acy Forsythe has the right idea. :P

Post Reply