473,513 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamically Determine Delimited File Using VBA?

Ben
Hi all -

I am having a bit of trouble and thought maybe someone in this group
could shed some light. Here's the skinny...

I am creating an automated process to import a bunch of text files
into Access. I want to avoid creating a separate "Spec" for each file
(there are over 180 files) and instead want to code my own dynamic
importing rules. So far it's been going fine, except for one item...

I can't figure out a reliable way to determine in VBA whether a given
text file is comma-delimited, tab-delimited, etc. Obviously there is
nothing inherent in the file's properties, at least as far as I know.
Sometimes the extension is the ticket (e.g. "csv" usually means
comma-delimited), but that's not always reliable, since "txt" could
mean a million things. The only thing I can think of is to open the
file via IO and come up with some logic (e.g. if each line contains a
certain number of commas, then assume the file is comma-delimited),
but that too sounds like a half-baked method.

I am hoping that I'm just missing something incredibly obvious and can
benefit from the great minds in this forum. Thanks in advance for any
ideas.

- Ben
Nov 12 '05 #1
3 7016
TC
Most comma-delimited files would seldom contain tabs, IMO. So perhaps you
could say: "if the first line contains any tabs, it is tab delimited,
otherwise it is comma delimited".

Or - since you clearly need the code to do both - why not try an initial
parse of the first 10 lines of each file, using each of the two different
methods, & see how many columns resulted?

For example, say parsing those lines gave a # of columns of 5, 5, 5, 5, 5,
5, 5, 5, 5, 5 assuming tab delimiting, but 0, 3, 6, 0, 0, 3, 5, 1, 1, 0
using comma delimiting. That file is clearly tab delimited! If the results
were reversed, it would be comma delimited. If the results were *mixed*,
then maybe that file is your grocery list!

Of course, if you have control of the process creating those files, you
could just put "TABS!" or "COMMAS!' as the first line in each file.

HTH,
TC
"Ben" <be*@hoffstein.net> wrote in message
news:1b**************************@posting.google.c om...
Hi all -

I am having a bit of trouble and thought maybe someone in this group
could shed some light. Here's the skinny...

I am creating an automated process to import a bunch of text files
into Access. I want to avoid creating a separate "Spec" for each file
(there are over 180 files) and instead want to code my own dynamic
importing rules. So far it's been going fine, except for one item...

I can't figure out a reliable way to determine in VBA whether a given
text file is comma-delimited, tab-delimited, etc. Obviously there is
nothing inherent in the file's properties, at least as far as I know.
Sometimes the extension is the ticket (e.g. "csv" usually means
comma-delimited), but that's not always reliable, since "txt" could
mean a million things. The only thing I can think of is to open the
file via IO and come up with some logic (e.g. if each line contains a
certain number of commas, then assume the file is comma-delimited),
but that too sounds like a half-baked method.

I am hoping that I'm just missing something incredibly obvious and can
benefit from the great minds in this forum. Thanks in advance for any
ideas.

- Ben

Nov 12 '05 #2
Ben
Thanks for the advice TC. I think the parsing/testing method might
work, so I'll give that a shot today. It is less efficient than I had
hoped (i.e. having to test each scenario, count the columns, make
final decision, delete other imports), but it's certainly better than
anything I've come up with. One complication is that I'm dealing with
tab-delimited, comma-delimited, semicolon-delimited, and
pipe-delimited possibilities, so I need to run all of those tests for
each file.

If anyone else has ideas, I'm all ears. Unfortunately I don't have
control over the input files, so putting "TABS" or the like inside the
file won't work. But I certainly could code logic that runs this test
for each file initially, records the results, and then uses those
settings going forward.

Thanks again,
Ben

"TC" <a@b.c.d> wrote in message news:<1068000629.281517@teuthos>...
Most comma-delimited files would seldom contain tabs, IMO. So perhaps you
could say: "if the first line contains any tabs, it is tab delimited,
otherwise it is comma delimited".

Or - since you clearly need the code to do both - why not try an initial
parse of the first 10 lines of each file, using each of the two different
methods, & see how many columns resulted?

For example, say parsing those lines gave a # of columns of 5, 5, 5, 5, 5,
5, 5, 5, 5, 5 assuming tab delimiting, but 0, 3, 6, 0, 0, 3, 5, 1, 1, 0
using comma delimiting. That file is clearly tab delimited! If the results
were reversed, it would be comma delimited. If the results were *mixed*,
then maybe that file is your grocery list!

Of course, if you have control of the process creating those files, you
could just put "TABS!" or "COMMAS!' as the first line in each file.

HTH,
TC
"Ben" <be*@hoffstein.net> wrote in message
news:1b**************************@posting.google.c om...
Hi all -

I am having a bit of trouble and thought maybe someone in this group
could shed some light. Here's the skinny...

I am creating an automated process to import a bunch of text files
into Access. I want to avoid creating a separate "Spec" for each file
(there are over 180 files) and instead want to code my own dynamic
importing rules. So far it's been going fine, except for one item...

I can't figure out a reliable way to determine in VBA whether a given
text file is comma-delimited, tab-delimited, etc. Obviously there is
nothing inherent in the file's properties, at least as far as I know.
Sometimes the extension is the ticket (e.g. "csv" usually means
comma-delimited), but that's not always reliable, since "txt" could
mean a million things. The only thing I can think of is to open the
file via IO and come up with some logic (e.g. if each line contains a
certain number of commas, then assume the file is comma-delimited),
but that too sounds like a half-baked method.

I am hoping that I'm just missing something incredibly obvious and can
benefit from the great minds in this forum. Thanks in advance for any
ideas.

- Ben

Nov 12 '05 #3
TC
I think the "try each method & compare results" approach would work quite
well. It would probably only be a few lines of extra code. The only problem
would be, how to decide which set of values was "better". For example,
5,5,5,5,5 is clearly better than 5,1,2,3,8. But is 5,4,6,5,1 better or
worse than 6,6,1,5,7?

HTH,
TC
Ben <be*@hoffstein.net> wrote in message
news:1b**************************@posting.google.c om...
Thanks for the advice TC. I think the parsing/testing method might
work, so I'll give that a shot today. It is less efficient than I had
hoped (i.e. having to test each scenario, count the columns, make
final decision, delete other imports), but it's certainly better than
anything I've come up with. One complication is that I'm dealing with
tab-delimited, comma-delimited, semicolon-delimited, and
pipe-delimited possibilities, so I need to run all of those tests for
each file.

If anyone else has ideas, I'm all ears. Unfortunately I don't have
control over the input files, so putting "TABS" or the like inside the
file won't work. But I certainly could code logic that runs this test
for each file initially, records the results, and then uses those
settings going forward.

Thanks again,
Ben

"TC" <a@b.c.d> wrote in message news:<1068000629.281517@teuthos>...
Most comma-delimited files would seldom contain tabs, IMO. So perhaps you could say: "if the first line contains any tabs, it is tab delimited,
otherwise it is comma delimited".

Or - since you clearly need the code to do both - why not try an initial
parse of the first 10 lines of each file, using each of the two different methods, & see how many columns resulted?

For example, say parsing those lines gave a # of columns of 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 assuming tab delimiting, but 0, 3, 6, 0, 0, 3, 5, 1, 1, 0
using comma delimiting. That file is clearly tab delimited! If the results were reversed, it would be comma delimited. If the results were *mixed*,
then maybe that file is your grocery list!

Of course, if you have control of the process creating those files, you
could just put "TABS!" or "COMMAS!' as the first line in each file.

HTH,
TC
"Ben" <be*@hoffstein.net> wrote in message
news:1b**************************@posting.google.c om...
Hi all -

I am having a bit of trouble and thought maybe someone in this group
could shed some light. Here's the skinny...

I am creating an automated process to import a bunch of text files
into Access. I want to avoid creating a separate "Spec" for each file
(there are over 180 files) and instead want to code my own dynamic
importing rules. So far it's been going fine, except for one item...

I can't figure out a reliable way to determine in VBA whether a given
text file is comma-delimited, tab-delimited, etc. Obviously there is
nothing inherent in the file's properties, at least as far as I know.
Sometimes the extension is the ticket (e.g. "csv" usually means
comma-delimited), but that's not always reliable, since "txt" could
mean a million things. The only thing I can think of is to open the
file via IO and come up with some logic (e.g. if each line contains a
certain number of commas, then assume the file is comma-delimited),
but that too sounds like a half-baked method.

I am hoping that I'm just missing something incredibly obvious and can
benefit from the great minds in this forum. Thanks in advance for any
ideas.

- Ben

Nov 12 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2216
by: Hennie de Nooijer | last post by:
Hi, This is a diffcult issue to explain. I hope to make my problem clear to you. SITUATION I'm building A SLA Query for a customer. This customer has an awkward way to determine the SLA results...
3
5414
by: Elmo Watson | last post by:
I've been asked to develop a semi-automated type situation where we have a database table (sql server) and periodically, there will be a comma delimited file from which we need to import the data,...
6
1763
by: Skc | last post by:
I am trying to import a file using a custom VB.net procedure, but the problem is it works on a file with pure comma separation and not inverted commas and commas, i.e. it works for AAA,BBB,CCC,DDD...
0
1808
by: TJS | last post by:
attempting to read a delimited text file into a dataset using oledb text file connection getting this error message when trying to open connection...
9
3608
by: Bernie Yaeger | last post by:
Is there a way to convert or copy a .xml file to a comma delimited text file using vb .net? Thanks for any help. Bernie Yaeger
6
1865
by: Smugsboy | last post by:
Need some help. I would like to create in my site a picture that when a user clicks on it, a javascript code which determines which file is to be downloaded to the user, is ran. My problem is...
2
1720
by: Astra | last post by:
Hi All Creating an rss.xml file dynamically via ASP/ADO/DB, but find errors in the file. Don't think it's an ASP prob to be honest. Think its more to do with the fact that the ampersands are...
6
22486
by: =?Utf-8?B?UmljaA==?= | last post by:
'--this code works but only reads text into one column when contains multiple cols Dim ds1x As New DataSet Dim ConStr As String = _ "Provider=Microsoft.Jet.OLEDB.4.0;Data...
5
2307
by: RyanL | last post by:
I'm a newbie! I have a non-delimited data file that I'd like to convert to delimited. Example... Line in non-delimited file: 0139725635999992000010100534+42050-102800FM-15+1198KAIA Should...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7157
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7379
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1591
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
798
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
455
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.