I would like to create a batch file that converts every line in a flat file to e specific amount of characters (add spaces at the end of every row).
Example:
I have a text file called "text.txt", and it looks like this:
1
22
333
4444
55555
I would like to run a batch file on it and recieve the following (all lines are 7 characters long):
1......
22.....
333....
4444...
55555..
(I had to replace my spaces with dots to make it visible)
Is this possible, and how?
Update:
Cool, that works perfect.
Is it also possible to convert a blank line (just CRLF) to 7 spaces? When I run the above on a file with empty lines they are deleted.
Thanks!
EDIT: Just to clarify, this is the file:
1
22
333
22
1
4444
55555
And I want to recieve:
1......
22.....
.......
333....
22.....
.......
1......
4444...
55555..
Thanks again!
Update 2:
Andriys answer is getting me a few steps forward. I’ve got a few issues though. If I leave the # sign out, the output will be like this (still periods instead of spaces):
1.......
22......
ECHO is off.
333.....
22......
ECHO is off.
1.......
4444....
55555...
And if a line starts with a space it will be outputted as:
ECHO is off.
Powershell is unfortunately not an option…
This might be a better example,
text.txt:
This is
the input
file
I
want to convert
Of course now we have to make the records longer, let’s say 20 char.
The
FORloop does omit empty lines. This is by design. To work around that, you could use the following command:It is considered that no line can match
""(empty string), so simplyFIND "" < filewould produce empty output. But the/Voption causes the inversion of the output: instead of the lines that match the search string,FINDis to output those that do not match it. SoFIND /V "" < fileessentially causesFINDto output all the lines offile. And/Ncauses every line to be prepended with the line number, like this:Accordingly, empty lines will look just like this:
So, now we are able to iterate over all the lines. We only need to remove all the
[number]parts, then use @Dave’s idea of appending the spaces and cutting out the first 7 characters. Here’s a full script:(The
#character is only added for visual indication.)UPDATE
Added
(just afterECHO. This solves theECHO is offissue. Basically, you can use a number of different characters instead of(, like., or,for instance. But, as shown in this answer,(seems most reliable.UPDATE 2
Added the
"delims="option to theFORloop after/Fto account for spaces in the text.