Truncate or Pad Each Line In Text File using a DOS Batch File

I have a text file with uneven lines i.e. The first line ends at 185 characters and the next at 201 characters and so on.
I need a way to pad the short lines to the length of the longer lines so I can BULK INSERT into SQL with a format file (.fmt).
The format file uses "\r\n" to find the end of each line so the CrLf has to be in the same place at the end of each line.
I need a batch file to count the columns and insert spaces for the shorter lines.
LVL 9
tonydemarcoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Shift-3Commented:
Paste the script below into a text file with a .vbs extension.  Customize the value of the strInput variable on line 5 with the location of the file to be read.  Customize the value of the strOutput variable on line 6 with the location of the tile to be written to.  Note that these can be set to the same file.  

Running the script will determine the length of the longest line in the input file and then pad each line in the output file accordingly.  The output file will be overwritten without warning.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
strInput = "c:\input.txt"
strOutput = "c:\output.txt"
strPadChar = " "
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInput, ForReading, False, TriStateUseDefault)
arrLines = Split(objInput.ReadAll, vbCrLf)
objInput.Close
 
For Each strLine In arrLines
    intLength = Len(strLine)
    
    If intLength > intMaxLength Then
        intMaxLength = intLength
    End If
Next
 
Set objOutput = objFSO.OpenTextFile(strOutput, ForWriting, True)
 
For Each strLine In arrLines
    intPad = intMaxLength - Len(strLine)
    objOutput.WriteLine strLine & String(intPad, strPadChar)
Next
 
objOutput.Close

Open in new window

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
tonydemarcoAuthor Commented:
Spot On!
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
Windows Batch

From novice to tech pro — start learning today.