Need a file conversion utility to fixed record length files

I need a small console utility, which can do the following:

Read a flat file where each record is of variable length, terminated with CR/LF.
Produce a flat file, fixed (adjustable) record length, blank-padded records and terminated with CR/LF.

Example ('.' symbolises a space/blank here):

This is the dataCR/LF
Here are more data in the fileCR/LF
Last recordCR/LF

Outputfile (a record width of 40 bytes chosen):
This is the data......................CR/LF
Here are more data in the file........CR/LF
Last record...........................CR/LF

Anything that will work on 32 bit Windows AND handle long filenames will do.

I know it's out there somewhere - but I just can't locate it...


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

I don't think there is such a specialized command-line utility, you will probably need to create a small program or a script to perform the required task. If you have Windows Script Host installed, a simple script can be created.

If you don't need to process multiple files in a batch, you can also use MS Excel to do exactly what you need. If so, let me know and I'll send you a brief tutorial.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
KDKAuthor Commented:
Hi PodExpert,

No Excel won't do it - need to be run 'in batch'.

I do'nt think my requst is *that* special - and spending a few hours on the net, actualy led me to a Perl script yielding exactly what I needed. It's included below.

Case closed - thanks for your input.


#!C:\Perl\bin -w
#  pad data records with spaces to 126 chars plus '\n'.
#  Usage:
#      the output will be named "$INPUTFILE.out"
#  Note: Ignore the trailing newline character.  If data
#        has 126 chars in it, enter 126 chars on the command
#        line.  Output will be 126 chars plus '\n'.
#        Update:  That's true only when inputting regular text
#                 files.  "bill_print" (?) contains things like:
#                 "^A^T"; that's four chars ^ + A + ..., not to
#                 mention a lot of \210 -ish things.
#                 wc -c on that reveals 400 odd chars.  vi and emacs
#                 see the '^X' as two separate chars.  "more" and
#                 wc -c see the ctrl-qN pair as the _real control char_!
#                 "more" finds an EOF after four lines and quits.
#                 head -1 blah | od -c displays ascii lines
#                 terminated by \n with lots of embedded "0"'s.
#  12Oct2001   s. keeling       0001     start.

use strict;

my ( $line, $input, $output, $start, $rec, $numchars, $list );

# $input = "/home/ecperdi/skeeling/data/P0510DCIPC01OUT_10.dat";
die qq(usage: $0 NUMCHARS INFILE [list]\n)
    if @ARGV < 2;

$numchars = "$ARGV[0]";
$input = "$ARGV[1]";
$list = "$ARGV[2]";

if( defined( $input ) ) {

    chomp( $input );

    $output = "$input" . ".out";

    open( INFILE, qq(< $input) )
        or die qq( $0: cannot open $input: $!\n );

    open( OUTFILE, qq(> $output) )
        or die qq( $0: cannot open $output: $!\n );

    while ( $line = <INFILE> ) {

        chomp( $line );

        if ( $line =~ /^$/ ) {
            print OUTFILE $line . "\n";

        if( defined( $line ) ) {


            $start = length( $line );
            if ( defined( $list ) ) {
#                print "$rec: $start chars long\n";

            while ( $start < $numchars ) {
                $line .= " ";

            print OUTFILE $line . "\n";
as I said, it could be done via a script. But for such a simple task, it is usually easier to write the script instead of speding a few hours finding it - of course, you need to know, how to do it. Or ask an expert :)

KDKAuthor Commented:

You're completely right, and that's what you said in the first place.

Yeah - writing a script like this is easy - just need to be on the right platform and with the right tools available...

Once again: Thanks for your replies, this really is a great forum!

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.