Search code examples
filebatch-fileadditionspacesflat

Edit flat file to have a specific number of characters on each line?


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.


Solution

  • The FOR loop does omit empty lines. This is by design. To work around that, you could use the following command:

    FIND /N /V "" < sourcefile
    

    It is considered that no line can match "" (empty string), so simply FIND "" < file would produce empty output. But the /V option causes the inversion of the output: instead of the lines that match the search string, FIND is to output those that do not match it. So FIND /V "" < file essentially causes FIND to output all the lines of file. And /N causes every line to be prepended with the line number, like this:

    [number]text
    [number]text

    Accordingly, empty lines will look just like this:

    [number]
    [number]
    …
    

    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:

    @ECHO OFF
    FOR /F "delims=" %%L IN ('FIND /N /V "" ^<text.txt') DO CALL :process "%%L"
    GOTO :EOF
    
    :process
    SET "line=%~1"
    SET "line=%line:*]=%       "
    ECHO(%line:~0,7%#
    

    (The # character is only added for visual indication.)

    UPDATE

    Added ( just after ECHO. This solves the ECHO is off issue. 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 the FOR loop after /F to account for spaces in the text.