Featured post
vba - ADO Text Driver Trimming Leading Space -
i'm using adodb microsoft text driver parse text file in excel.
the problem lines of file have leading space, , leading space getting chopped.
when getstring(,1) on recordset, line like: " description not use"
gets trimmed to:
"description not use"
...this problematic because i'm parsing cisco config file, , leading space useful in figuring out if i'm still in same object or not.
for example text like:
object-group network abc_group network-object host 192.10.24.71 network-object host 192.10.24.72 network-object host 192.10.24.20 network-object host 192.10.24.21 object-group network xyz_hosts network-object host 192.10.24.55 network-object host 192.10.24.26
...i using leading space tell me still in same object-group.
any ideas on how adodb keep leading space when reads text file?
i tried using filestreamobject, discovered file reading had lines ending in chr(13) , ending in both chr(13) , chr(10), throwing off readline method.
sorry, earlier version of answer used c#. vba, have little more heavy lifting.
if file small enough fit in memory, read whole thing in string, , split it:
private function splitfilecontents(byval filecontents string) string() dim arrresult() string dim resultcount long dim crpos long dim lfpos long dim endpos long while len(filecontents) > 0 crpos = instr(filecontents, vbcr) lfpos = instr(filecontents, vblf) if crpos = 0 , lfpos = 0 redim preserve arrresult(0 resultcount) arrresult(resultcount) = filecontents resultcount = resultcount + 1 exit elseif crpos = 0 endpos = lfpos elseif lfpos = 0 endpos = crpos elseif crpos < lfpos endpos = crpos else endpos = lfpos end if if endpos > 1 redim preserve arrresult(0 resultcount) arrresult(resultcount) = left$(filecontents, endpos - 1) resultcount = resultcount + 1 end if if endpos = len(filecontents) filecontents = "" else filecontents = mid$(filecontents, endpos + 1) end if loop if resultcount = 0 redim arrresult(0 0) arrresult(0) = "" end if splitfilecontents = arrresult end function
alternatively, there several possible approaches using fixed-width format, preserve leading spaces. these articles discuss using schema.ini file specify fixed-width file format:
- Get link
- X
- Other Apps
Comments
Post a Comment