Cmedic_techs
asked on
Parse File into Database
I need to read a file into a database with the following structure, years ago I learned how to do xml, and csv, but I can't define this file structure, so I'm floundering a little since I'm a bit out of practice.
filename exported_stocklevels.txt
Item_1 = {
stock = true,
on_hand = 0,
back_order = 5,
reorder = 2,
}
Item_2 = {
stock = no,
on_hand = 0,
back_order = 3,
reorder_point = 1,
I know how to update with cfupdate, etc, but I've always struggled with reading in data from files. especially when I can't control the file structure.
filename exported_stocklevels.txt
Item_1 = {
stock = true,
on_hand = 0,
back_order = 5,
reorder = 2,
}
Item_2 = {
stock = no,
on_hand = 0,
back_order = 3,
reorder_point = 1,
I know how to update with cfupdate, etc, but I've always struggled with reading in data from files. especially when I can't control the file structure.
Hi,
Looks like you will need to use the 'find' and 'mid' functions to extract each value.
To program the function, just imagine you are controlling a cursor.
Lets assume you open the text file and call it variable txtfile.
E.g
<cfset start = find("stock = ", txtfile, 0)>
Then you count the characters to extract the value.
<cfset end = find(",", txtfile, start + 8)>
<cfset value = mid(txtfile, start, end - start)>
This should get the first value, then you change the start postion to get the next value.
The code example above is a bit rough, I can help you further if you let me know about the reorder value below.
One thing I noticed is the
reorder = 2,
and
reorder_point = 1,
Are these consistent in the datafile or just an error in your example?
Hope this helps.
Looks like you will need to use the 'find' and 'mid' functions to extract each value.
To program the function, just imagine you are controlling a cursor.
Lets assume you open the text file and call it variable txtfile.
E.g
<cfset start = find("stock = ", txtfile, 0)>
Then you count the characters to extract the value.
<cfset end = find(",", txtfile, start + 8)>
<cfset value = mid(txtfile, start, end - start)>
This should get the first value, then you change the start postion to get the next value.
The code example above is a bit rough, I can help you further if you let me know about the reorder value below.
One thing I noticed is the
reorder = 2,
and
reorder_point = 1,
Are these consistent in the datafile or just an error in your example?
Hope this helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
It was an error in my example.
the format is consistent. I also dropped the closing brace. my bad. thanks for all the replies, will finish looking them over in a little bit.
the format is consistent. I also dropped the closing brace. my bad. thanks for all the replies, will finish looking them over in a little bit.
ASKER
Thanks guys, I appreciate the quick help.
- What version of CF - 8 or higher?
- Is that the exact format of the data file including new lines?
- Do you need to extract all of the values? If so are those all of the possible keys? (There's differences between item_1 and 2)