By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,028 Members | 881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,028 IT Pros & Developers. It's quick & easy.

How do I convert a Text file with variable length records to fixed length records?

P: 21
Hi,
I am looking for an example to convert a flat text file with variable length records to a flat file with fixed length records. Does anyone have an example?
Dec 27 '06 #1
Share this Question
Share on Google+
11 Replies


100+
P: 1,646
Hi,
I am looking for an example to convert a flat text file with variable length records to a flat file with fixed length records. Does anyone have an example?
Hi Jim, you will need to know the maximum field length for any strings in the text file
Dec 27 '06 #2

P: 21
Hi Jim, you will need to know the maximum field length for any strings in the text file
I know the size of each field. The beginning portion of the records is fixed ( the first 100 or so bytes). The rest varies according to the record type (ex: 05, 06,10, 30, 50, etc). I need all the records to be fixed (336 length) with a Ctrl/Lf at the end of each record.
An example of the records is:
20817 C A00059530 376480850 PP001755719 056920 CARMICHAEL AVE BETHESDA, MD 20817
20817 C A00059530 376480850 PP001755719 06MARK F THIMMIG
20817 C A00059530 376480850 PP001755719THIMMIG JORDAN C A0005953002 061030B00786 10 000016
20817 C A00059530 376480850 PP001755719THIMMIG JORDAN C A0005953002 061030B007860100 003001001020067061 99203 +000001 +000008000+000008000PAPBFA +000000000+000002400+000000000+000000000+000000000 +000005600 0000{ +000000000+000000000+000000000+000000000+000000000 1020060001
Dec 27 '06 #3

100+
P: 1,646
I know the size of each field. The beginning portion of the records is fixed ( the first 100 or so bytes). The rest varies according to the record type (ex: 05, 06,10, 30, 50, etc). I need all the records to be fixed (336 length) with a Ctrl/Lf at the end of each record.
An example of the records is:
20817 C A00059530 376480850 PP001755719 056920 CARMICHAEL AVE BETHESDA, MD 20817
20817 C A00059530 376480850 PP001755719 06MARK F THIMMIG
20817 C A00059530 376480850 PP001755719THIMMIG JORDAN C A0005953002 061030B00786 10 000016
20817 C A00059530 376480850 PP001755719THIMMIG JORDAN C A0005953002 061030B007860100 003001001020067061 99203 +000001 +000008000+000008000PAPBFA +000000000+000002400+000000000+000000000+000000000 +000005600 0000{ +000000000+000000000+000000000+000000000+000000000 1020060001
Does the number of fields change from record to record?
Dec 27 '06 #4

P: 21
Does the number of fields change from record to record?
Yes, starting in position 112 (counting from 1) the records have different fields and field sizes. The record types are determined by the record type located in position 110 for a length of 2 characters. Record types are 05, 06, 10, 30, 50, 87, 88.
Dec 27 '06 #5

100+
P: 1,646
Yes, starting in position 112 (counting from 1) the records have different fields and field sizes. The record types are determined by the record type located in position 110 for a length of 2 characters. Record types are 05, 06, 10, 30, 50, 87, 88.
If the data after 112 is significant the you will need to create records that are all as long as the longest one leaving null values in the unused fields of the shorter records
Dec 27 '06 #6

Expert 5K+
P: 8,434
If the data after 112 is significant the you will need to create records that are all as long as the longest one leaving null values in the unused fields of the shorter records
I think that is what he actually asked for. :)

Try this quick sample...
Expand|Select|Wrap|Line Numbers
  1. Dim TheRecord As String
  2. Dim L As Long
  3. Const MaxLength As Long = 336
  4. Open "OldFile.Txt" For Input Access Read Shared As #1
  5. Open "NewFile.Txt" For Output Access Write Lock Write As #2
  6. Do Until Eof(1)
  7.   Line Input #1, TheRecord
  8.   L = Len(TheRecord)
  9.   If L < MaxLength Then
  10.    TheRecord = TheRecord & Space$(MaxLength - L)
  11.   End If
  12.   Print #2, TheRecord
  13. Loop
  14. Close
  15.  
Dec 28 '06 #7

100+
P: 1,646
I think that is what he actually asked for. :)

Try this quick sample...
Expand|Select|Wrap|Line Numbers
  1. Dim TheRecord As String
  2. Dim L As Long
  3. Const MaxLength As Long = 336
  4. Open "OldFile.Txt" For Input Access Read Shared As #1
  5. Open "NewFile.Txt" For Output Access Write Lock Write As #2
  6. Do Until Eof(1)
  7.   Line Input #1, TheRecord
  8.   L = Len(TheRecord)
  9.   If L < MaxLength Then
  10.    TheRecord = TheRecord & Space$(MaxLength - L)
  11.   End If
  12.   Print #2, TheRecord
  13. Loop
  14. Close
  15.  
And where are the NULL values in the blank fields?
You are just making the last field very long here.
I have never worked on a project where this would be useful, but I could be wrong.
Dec 28 '06 #8

Expert 5K+
P: 8,434
And where are the NULL values in the blank fields?
You are just making the last field very long here.
I have never worked on a project where this would be useful, but I could be wrong.
Granted, there may well be more required, but the original request was just to convert to fixed-length records. I would assume this is only one step in the conversion process.

Oh, and as for "that's what he asked for" I need to read more carefully, huh. :(
Dec 28 '06 #9

P: 21
I think that is what he actually asked for. :)

Try this quick sample...
Expand|Select|Wrap|Line Numbers
  1. Dim TheRecord As String
  2. Dim L As Long
  3. Const MaxLength As Long = 336
  4. Open "OldFile.Txt" For Input Access Read Shared As #1
  5. Open "NewFile.Txt" For Output Access Write Lock Write As #2
  6. Do Until Eof(1)
  7.   Line Input #1, TheRecord
  8.   L = Len(TheRecord)
  9.   If L < MaxLength Then
  10.    TheRecord = TheRecord & Space$(MaxLength - L)
  11.   End If
  12.   Print #2, TheRecord
  13. Loop
  14. Close
  15.  
Yes, this is what I was talking about! Depending on the record type the record ends in varioius lengths. I will give this a try and let you know how it works. The ultimate goal is to get an input file that I can create a shcema for and use as input to map to a destination file using Biztalk. I will let you know how it goes. Thanks.
Dec 29 '06 #10

P: 21
Yes, this is what I was talking about! Depending on the record type the record ends in varioius lengths. I will give this a try and let you know how it works. The ultimate goal is to get an input file that I can create a shcema for and use as input to map to a destination file using Biztalk. I will let you know how it goes. Thanks.

This Worked! Thanks for the help. Sorry, I took a while to get back to you.
Jan 10 '07 #11

Expert 5K+
P: 8,434
This Worked! Thanks for the help. Sorry, I took a while to get back to you.
No problem, glad we could help.
Jan 10 '07 #12

Post your reply

Sign in to post your reply or Sign up for a free account.