OpenNTF.org - Change fields position in a fl
My Links (Not logged in)
Code Bin Search
 
Hosted by Prominic.NET
Rate This Code
5 - brilliant stuff
4 - very nice
3 - average
2 - needs work
1 - bad
   OpenNTF Code Bin
Edit Document Code By Date > Code Document
About This Code
Brief Description:
Change fields position in a flat text file 
Rating:
Not Rated Yet 
Contributor:
laurent abcdef 
Category:
Lotusscript 
Type:
File I/O 
Last Modified:
18 Sep 2008 
OpenNTF Disclaimer

All of the program code and information presented in the OpenNTF.org Code Bin are provided "as-is", and should be used at your own risk. OpenNTF.org make no express or implied warranty about anything in the Code Bin, and OpenNTF.org will not be responsible or liable for any damage caused by the use or misuse of anything from this site. OpenNTF.org makes no guarantees about anything. Please thoroughly test all of the knowledge and code you find here before you attempt to use them in your production environment.

Code / Description
'---:


Option Declare

'
' FieldName TAB Length TAB Position
'
Const FormatFileInput=|
USR-IDAZE 8 36
USR-MAZEAT 7 44
USR-TIAZETRE 4 51
USR-NAZEOM 25 55
|





'
' FieldName TAB Length TAB Position
'
Const FormatFileOutput=|
USR-IDAZE 8 501
USR-MAZEAT 7 509
USR-TIAZETRE 4 516
USR-NAZEOM 25 520
|



Const FileNameInput = "IN.TXT"
Const FileNameOutput = "OUT.TXT"





Type LineFileFormat
Name As String
Length As Long
Position As Long
End Type

Type FormatConv
In As LineFileFormat
Out As LineFileFormat
End Type

Dim LineOutTotalLength As Long

Sub Initialize

Dim FormatConvertion List As FormatConv
Dim V As Variant
Dim FormatFile_Tmp As Variant
Dim LineFileFormat_Tmp As LineFileFormat


' IN
FormatFile_Tmp = Split( FormatFileInput , Chr$(10) )
Forall Champ In FormatFile_Tmp
V = Split( Champ , Chr$( 9 ) )
If Trim$( V(0) ) <> "" Then
LineFileFormat_Tmp.Name = Trim$( V( 0 ) )
LineFileFormat_Tmp.Length = Val( V( 1 ) )
LineFileFormat_Tmp.Position = Val( V( 2 ) )
FormatConvertion( LineFileFormat_Tmp.Name ).IN = LineFileFormat_Tmp
End If
End Forall

' OUT
FormatFile_Tmp = Split( FormatFileOutput , Chr$(10) )
Forall Champ In FormatFile_Tmp
V = Split( Champ , Chr$( 9 ) )
If Trim$( V(0) ) <> "" Then
LineFileFormat_Tmp.Name = Trim$( V( 0 ) )
LineFileFormat_Tmp.Length = Val( V( 1 ) )
LineFileFormat_Tmp.Position = Val( V( 2 ) )
FormatConvertion( LineFileFormat_Tmp.Name ).OUT = LineFileFormat_Tmp
If LineFileFormat_Tmp.Position + LineFileFormat_Tmp.Length > LineOutTotalLength Then LineOutTotalLength = LineFileFormat_Tmp.Position + LineFileFormat_Tmp.Length
End If
End Forall








'
' Format Control
'
Forall OneFieldConvertion In FormatConvertion
If OneFieldConvertion.In.Name = "" Then
Print OneFieldConvertion.Out.Name + Chr$(9) + Chr$(9) + " The ouput file do not contains this field"
Elseif OneFieldConvertion.Out.Name = "" Then
Print OneFieldConvertion.In.Name + Chr$(9) + Chr$(9) + " The ouput file do not contains this field"
Elseif OneFieldConvertion.In.Length > OneFieldConvertion.Out.Length Then
Print OneFieldConvertion.In.Name + Chr$(9) + Chr$(9) + " This field size will be reduced to " + Cstr(OneFieldConvertion.Out.Length) + " instead of " + Cstr(OneFieldConvertion.In.Length)
End If
End Forall






'
' Convertion
'
Dim FileNumInPut As Long
FileNumInPut = Freefile
Open FileNameInput For Input Access Read As FileNumInPut

Dim FileNumOutPut As Long
FileNumOutPut = Freefile
Open FileNameOutput For Output Access Write As FileNumOutPut


Dim S As String
Do Until Eof(FileNumInPut)
Line Input #FileNumInPut, S
Print #FileNumOutPut, LineConvert( S , FormatConvertion )
Loop

Close #FileNumOutPut
Close #FileNumInPut





End Sub



Function LineConvert( LineIn As String, FormatConvertion List As FormatConv ) As String

Dim S As String

Dim LineOut As String
LineOut = Space$( LineOutTotalLength )

Forall AFieldToConvert In FormatConvertion
If (AFieldToConvert.IN.Name <> "") And (AFieldToConvert.OUT.Name <> "") Then
S = Mid$( LineIn , AFieldToConvert.IN.Position , AFieldToConvert.IN.Length )
If Len(S) < AFieldToConvert.OUT.Length Then
S = S + Space$( AFieldToConvert.OUT.Length - Len(S) )
End If
LineOut = Left$( LineOut , AFieldToConvert.OUT.Position-1 ) + Left$( S , AFieldToConvert.OUT.Length ) + Mid$( LineOut , AFieldToConvert.OUT.Position + AFieldToConvert.OUT.Length )
End If
End Forall

LineConvert = LineOut

End Function

Usage / Example
You have one text file (the data)
two excel sheet describing the fields position,size and name (input file format, output file format)

Update the code with the files names, copy paste the excel sheet in the declaration
Launch the code

The output file will be created with the output file format specified
 Comments

No documents found

 Add your comment!