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
Pad unequal length lines with spaces to make all the same.
Moderator: DosItHelp
-
- Posts: 22
- Joined: 26 May 2011 07:56
-
- Posts: 126
- Joined: 10 Jun 2011 10:30
Re: Pad unequal length lines with spaces to make all the sam
I'm not sure that's a bad way, but you could do something like this:
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.
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.
-
- Posts: 22
- Joined: 26 May 2011 07:56
Re: Pad unequal length lines with spaces to make all the sam
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.
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.
-
- 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
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.
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.