Search code examples
filehelpers

Fixed field start position


I just discovered the amazing FileHelpers library. (on the past i'm already developed my own import/export library based on Attributes).

I wanna know if exists the possibility for the fixed record to specify the start character for each field.

Example: INPUT FILE

3232432GIANLUIGI

4234234MARIO

I just wanna import the name (Gianluigi) from the 8th character.

I just want to read the CustomerName, without the filed i've to ignore (especially if we talk about performance).

My real case is about a record that have reserved record position for future implementation.

Example:

-From 1 to 100 real fields

-From 101 to 150 no field - Position reserved for future implementation

-From 151 to 160 real fields

-From 161 to 185 another time no field - position reserved for future implementation

-From 186 to ...N real fields.

I want avoid to create fake field for read nothing (Ex. from 101 to 150 and from 161 to 185).

For each FieldFixedLength attributes i imagine start position.


Solution

  • Yes, that is completely do-able. There are several ways to do this, manually as I would to produce:

    [FixedLengthRecord(FixedMode.AllowVariableLength)]
    public class Customer
    {
        [FieldFixedLength(8)]
        [FieldValueDiscarded]
        public int ignoredFieldOne;
    
        [FieldTrim(TrimMode.Both)]
        [FieldFixedLength(80)]
        public String CustomerName;
    
        // Skip fields 3 - 5
        // field 3 (length 20) 
        // field 4 (length 5) 
        // field 5 (length 3)
        [FieldFixedLength(28)]
        [FieldValueDiscarded]
        public string ignoredFieldsThreeThroughFive;
    
        [FieldTrim(TrimMode.Both)]
        [FieldFixedLength(20)]
        public String AnotherField;
    }
    

    Alternatively, and probably easier for you is to use the Class Wizard that comes with FileHelpers where you can select the language you want the class built in.

    1. Set the class name to Customer
    2. Select Fixed Length Record
    3. Set FixedMode as desired
    4. Click next
    5. Set Number of Fields you want, eg 2
    6. If you have a header row, set Ignore the first to 1
    7. Tick the Ignore Empty Lines
    8. Click next
    9. In the first box, enter Name as CustomerID, Type as int and Length as 8
    10. In the second box, enter CustomerName, keep Type as string, set Length to your maximum (eg, 80) and Trim to Both (or Right if you want spaces at the start).

    At this point, on the right hand side will be the class that you have just built. This is exactly what I would have typed manually since I already know how to build these classes.

    Note 1: FixedMode set to ExactLength means ALL records have to be the same length

    Note 2: The class builder will automatically make your class sealed so it won't be inheritable for performance purposes.

    Note 3: For any position you are just not interested in, you will have to create a field but add the attribute as above and it will not populate it (see above where three fields are skipped as they are consecutive but only one field is created)