Loop and save as text file

prasadgov

Member
Local time
Today, 13:30
Joined
Oct 12, 2021
Messages
126
Hi,

In my access app, I receive data through text files everyday. These text files have one entry each and I receive close to 200 of these each day.
Due to an error in the upstream module, we did not receive these files for few days.
After finding out the error, the upstream team sent the data but put a day's worth in a single text file rather than the 150-200 separate text files.
I need to loop through this large text file and save each entry into separate text files.

Each entry starts { and ends with -}

Code:
{1:F01ICBKUS33AFIN0000000054}{2:I544IRVTUS3NABDSN2020}{4:
:16R:GENL
:20C::SEME//ICF0000097
:23G:NEWM
:98C::PREP//20241210140635
:16R:LINK
:20C::RELA//G121000050-1
:16S:LINK
:16S:GENL
:16R:TRADDET
:98A::ESET//20241210
:35B:/US/9128282R0
:70E::SPRO///AIMAD/1210QCPIN345000111
/TIME/140635
:16S:TRADDET
:16R:FIAC
:36B::ESTT//FAMT/5000000,
:97A::SAFE//ICF
:16S:FIAC
:16R:SETDET
:22F::SETR//TRAD
:16R:SETPRTY
:95P::PSET//FRNYUS33
:16S:SETPRTY
:16R:SETPRTY
:95R::DEAG/USFW/021000018
:97A::SAFE//FICC GSD
:16S:SETPRTY
:16S:SETDET
-}"
"{1:F01ICBKUS33AFIN0000000018}{2:I547IRVTUS3NABDSN2020}{4:
:16R:GENL
:20C::SEME//ICF5400012
:23G:NEWM
:98C::PREP//20241210080037
:16R:LINK
:20C::RELA//G121000012-1
:16S:LINK
:16S:GENL
:16R:TRADDET
:98A::ESET//20241210
:35B:/US/91282CCW9
:70E::SPRO///AIMAD/1210ICFIN345000027
/TIME/080036
:16S:TRADDET
:16R:FIAC
:36B::ESTT//FAMT/14000000,
:97A::SAFE//ICF
:16S:FIAC
:16R:SETDET
:22F::SETR//TRAD
:16R:SETPRTY
:95P::PSET//FRNYUS33
:16S:SETPRTY
:16R:SETPRTY
:95R::REAG/USFW/021000018
:97A::SAFE//GSCC
:16S:SETPRTY
:16R:AMT
:19A::ESTT//USD13241235,58
:16S:AMT
:16S:SETDET
-}
{1:F01ICBKUS33AFIN0000000031}{2:I544IRVTUS3NABDSN2020}{4:
:16R:GENL
:20C::SEME//ICF0000067
:23G:NEWM
:98C::PREP//20241210082259
:16R:LINK
:20C::RELA//NONREF
:16S:LINK
:16S:GENL
:16R:TRADDET
:98A::ESET//20241210
:35B:/US/9128282R0
:70E::SPRO///AIMAD/1210ICBIN345000002
/TIME/082259
:16S:TRADDET
:16R:FIAC
:36B::ESTT//FAMT/1000000,
:97A::SAFE//ICF
:16S:FIAC
:16R:SETDET
:22F::SETR//OWNI
:16R:SETPRTY
:95P::PSET//FRNYUS33
:16S:SETPRTY
:16R:SETPRTY
:95R::DEAG/USFW/021000018
:97A::SAFE//ICB
:16S:SETPRTY
:16S:SETDET
-}
"{1:F01ICBKUS33AFIN0000000037}{2:I545IRVTUS3NABDSN2020}{4:
:16R:GENL
:20C::SEME//ICF0000076
:23G:NEWM
:98C::PREP//20241210083401
:16R:LINK
:20C::RELA//G121000015-1
:16S:LINK
:16S:GENL
:16R:TRADDET
:98A::ESET//20241210
:35B:/US/91282CBC4
:70E::SPRO///AIMAD/1210GCCIN345030760
/TIME/083401
:16S:TRADDET
:16R:FIAC
:36B::ESTT//FAMT/50000000,
:97A::SAFE//ICF
:16S:FIAC
:16R:SETDET
:22F::SETR//TRAD
:16R:SETPRTY
:95P::PSET//FRNYUS33
:16S:SETPRTY
:16R:SETPRTY
:95R::DEAG/USFW/021000018
:97A::SAFE//GSCC
:16S:SETPRTY
:16R:AMT
:19A::ESTT//USD48095550,27
:16S:AMT
:16S:SETDET
-}
{1:F01ICBKUS33AFIN0000000046}{2:I547IRVTUS3NABDSN2020}{4:
:16R:GENL
:20C::SEME//ICF5400038
:23G:NEWM
:98C::PREP//20241210084619
:16R:LINK
:20C::RELA//G121000044-1
:16S:LINK
:16S:GENL
:16R:TRADDET
:98A::ESET//20241210
:35B:/US/91282CGG0
:70E::SPRO///AIMAD/1210ICFIN345000043
/TIME/084619
:16S:TRADDET
:16R:FIAC
:36B::ESTT//FAMT/50000000,
:97A::SAFE//ICF
:16S:FIAC
:16R:SETDET
:22F::SETR//TRAD
:16R:SETPRTY
:95P::PSET//FRNYUS33
:16S:SETPRTY
:16R:SETPRTY
:95R::REAG/USFW/021000018
:97A::SAFE//GSCC
:16S:SETPRTY
:16R:AMT
:19A::ESTT//USD50687500,
:16S:AMT
:16S:SETDET
-}"
"{1:F01ICBKUS33AFIN0000000038}{2:I545IRVTUS3NABDSN2020}{4:
:16R:GENL
:20C::SEME//ICF0000078
:23G:NEWM
:98C::PREP//20241210083521
:16R:LINK
:20C::RELA//G121000014-1
:16S:LINK
:16S:GENL
:16R:TRADDET
:98A::ESET//20241210
:35B:/US/91282CBC4
:70E::SPRO///AIMAD/1210GCCIN345032650
/TIME/083521
:16S:TRADDET
:16R:FIAC
:36B::ESTT//FAMT/50000000,
:97A::SAFE//ICF
:16S:FIAC
:16R:SETDET
:22F::SETR//TRAD
:16R:SETPRTY
:95P::PSET//FRNYUS33
:16S:SETPRTY
:16R:SETPRTY
:95R::DEAG/USFW/021000018
:97A::SAFE//GSCC
:16S:SETPRTY
:16R:AMT
:19A::ESTT//USD48095550,27
:16S:AMT
:16S:SETDET
-}

Or alternately, is it possible to read the entries into a table under different rows?
My code read only the first entry and not the subsequent ones.
 
Last edited:
Quick and dirty - use Split() to get each part in an array and then loop and write to a file

Pseudo-code:
Code:
Dim strAll As String, arrParts As Variant, i As Counter

strAll = ReadFromFile("C:\Path\To\YourFile.txt")
arrParts = Split(strAll, "-}")
For i = 0 To UBound(arrParts) - 1
  WriteToFile("C:\Path\To\YourFilePart" & i & ".txt", arrParts(i) & vbNewLine & "-}")
Next i
 
Last edited:
Each entry starts { and ends with -}
I would use the split function on -} to create an array then look through the array - perhaps reinstating the -} if your original importer requires it

edit: see cheekbudda is suggesting the same thing
 
Quick and dirty - use Split() to get each part in an array and then loop and write to a file

Psudo-code:
Code:
Dim strAll As String, arrParts As Variant, i As Counter

strAll = ReadFromFile("C:\Path\To\YourFile.txt")
arrParts = Split(strAll, "-}")
For i = 0 To UBound(arrParts) - 1
  WriteToFile("C:\Path\To\YourFilePart" & i & ".txt", arrParts(i) & vbNewLine & "-}")
Next i
WriteToFile line shows disabled and also I get an error at "i As Counter" User-defined Type not defined. Can I use Integer or long?
 
I get an error at "i As Counter" User-defined Type not defined
Yes, that should read i As Integer

You will need to provide implementations for ReadFromFile() and WriteToFile().

Pseudo-code is not real code - it's just to show you what you have to do.
 

Users who are viewing this thread

Back
Top Bottom