It's late to play here, but I can't be the only encoder that got tired when my SQL queries were broken by text files using a byte marker. There are very few stack questions that concern the problem - this is one of the closest - so I post an overlapping answer here.
I say โoverlapโ because the code below solves a slightly different problem for you - the main goal is to write a schema file for a folder with a heterogeneous collection of files, but the specification processing segment is clearly marked.
The key functionality is that we iterate over all the โ.csvโ files in the folder, and we test each file with a quick nibble of the first four bytes: and we only select the byte order marker if we see one.
After that, we work with low-level code for processing files from primary C. We should, up to using byte arrays, because everything else you do in VBA will be byte byte markers built into the structure of the string variable .
So, without further adodb, here is the code:
BOM code to delete text files in the schema.ini file:
Public Sub SetSchema (strFolder As String)
On error Continue on
"This is necessary if we do not have registry privileges to install
'correct' ImportMixedTypes = Text 'registry value that overrides IMEX = 1
"Code defined by the OEM code page is not supported: additional encoding required
Dim strSchema As String
Dim strFile As String
Dim hnd file as long
Dim arrFile () as byte
Dim arrBytes (0 to 4) As a byte
"\" Then strFolder = strFolder and "\"
0
"Utility specifications." Byte order characters confuse OLEDB text drivers:
hndFile = FreeFile Open strFolder and strFile for binaries as #hndFile ReDim arrFile (0K LOF (hndFile) - 1) Get #hndFile, arrFile Close #hndFile
0 then
The code is easier to understand if you know that a byte array can be assigned to VBA.String and vice versa. The BigReplace () function is a hack that wraps up some of the inefficient processing of VBA strings, especially distribution: you will find that large files cause serious memory and performance problems if you do this in any other way.
source share