'T2CSV Converter - by Steven Bowdenis licensed under a Creative Commons Attribution 4.0 International License. Based on a work at tschema.org/T2CSV/index.html 'This code is just string-play, nothing fancy so anyone with a basic understanding of programming will get it and see 'that there is no voodoo in data extraction and transformation, it's anyones game, I'm even using retro LABELS for the lulz! 'Performance-wise it is pretty impressive and overloads are unexpected, if you get an overload just modify the code to write to the .csv more regularly Imports System.IO Imports System.Text Imports System.Collections Imports Microsoft.VisualBasic Module Module1 Sub Main() RESET_APP: 'Menu variables Console.Title = "T2CSV Converter - by Steve Bowden" Dim menu_select As ConsoleKeyInfo Dim Op1 As String = "D1" Dim Op2 As String = "D2" Dim Op3 As String = "D3" Dim Op4 As String = "D4" Dim Op5 As String = "D5" Dim menu_choice As String 'file checking variables Dim default_path As String = "C:\T2CSV\" Dim path As String = default_path Dim proposed_path As String = "" Try If (Not System.IO.Directory.Exists(path)) Then 'create directory System.IO.Directory.CreateDirectory(path) End If Catch ex As Exception Console.WriteLine("Well well, looks there is a permissions issue preventing directory creation") Console.WriteLine("You will need to set a directory using Option 2 in the main menue, press any key...") Console.ReadKey() path = "unspecified" End Try Dim line As String Dim discrete_path As String = "" Dim filename As String 'setting up main table Dim maintable() As String = {"", "", "", "", "", ""} Dim maintable_explicit() As String = {"", "", "", "", "", ""} Dim uuid_tag As String = "" Dim tag_tag As String = "" Dim uuid_tag_exp As String = "" Dim tag_tag_exp As String = "" Dim lib_tag As String = "" Dim lib_tag_exp As String = "" 'extractor parameters Dim t_loc As Integer Dim t_loc_end As Integer Dim t_length As Integer Dim t_cont_loc As Integer Dim t_cont_length As Integer Dim t_contents As String Dim csv_line As String = "" Dim this_t_uuid As String = "" 'remove comments vars Dim cleanse_pos As Integer Dim cleanse_pos_end As Integer Dim anchor_outcom As Integer Dim rebuild_line As String Dim line_content As String 'n table Dim tag_csv_line As String Dim lib_csv_line As String MAIN_MENU: If (Not System.IO.Directory.Exists(path)) Then 'create directory System.IO.Directory.CreateDirectory(path) End If Dim di As New IO.DirectoryInfo(path) Dim aryFi As IO.FileInfo() Dim fi As IO.FileInfo Console.Clear() Console.WriteLine("T2CSV") Console.WriteLine("--MAIN MENU--") Console.WriteLine("1. Convert files") Console.WriteLine("2. Set filepath") Console.WriteLine("3. How to") Console.WriteLine("4. About") Console.WriteLine("5. Exit") menu_select = Console.ReadKey() menu_choice = menu_select.Key.ToString If menu_choice = Op1 Then GoTo START_CONVERSION End If If menu_choice = Op2 Then GoTo SET_FILEPATH End If If menu_choice = Op3 Then GoTo GUIDE End If If menu_choice = Op4 Then GoTo ABOUT End If If menu_choice = Op5 Then Environment.Exit(True) End If GoTo MAIN_MENU START_CONVERSION: Console.Clear() aryFi = di.GetFiles("*.t") If aryFi.Count < 1 Then 'Check for any files in the directory, issues would arise if the user hasn't put any files in or the directory is bogus Console.WriteLine("Whoops, there doesn't seem to be any files here!") Console.WriteLine() Console.WriteLine("Lets reset the directory and start over, press any key...") Console.ReadKey() GoTo RESET_APP 'pretty much starts the app over from scratch, clean slate End If For Each fi In aryFi Console.WriteLine("Filename: {0}", fi.Name) 'list out .t files in directory Next Console.WriteLine() Console.WriteLine("Are you cool with these files? (Y/N)") menu_select = Console.ReadKey() menu_choice = menu_select.Key.ToString If menu_choice = "Y" Then GoTo DO_CONVERSION End If If menu_choice = "N" Then GoTo MAIN_MENU End If GoTo START_CONVERSION DO_CONVERSION: Console.WriteLine() Console.WriteLine("converting...") 'tacky console candy File.WriteAllText(path + "_maintable.csv", "t_created,t_modified,t_uuid,t_name,t_id_assigned,t_data") 'add column headings File.AppendAllText(path + "_maintable.csv", vbCrLf) File.WriteAllText(path + "_tagtable.csv", "t_uuid, t_tag") File.AppendAllText(path + "_tagtable.csv", vbCrLf) File.WriteAllText(path + "_libtable.csv", "t_uuid, t_tags_uuid") File.AppendAllText(path + "_libtable.csv", vbCrLf) For Each fi In aryFi 'progressing through each t-file csv_line = "" 'predefining the csv row to prevent data leak filename = fi.Name discrete_path = path + filename 'build the file extension line = File.ReadAllText(discrete_path) 'read data from file extension 'cleansing carriage returns line = Replace(line, vbCrLf, "; ") 'cleansing comments cleanse_pos = 0 line_content = "" rebuild_line = "" anchor_outcom = 0 While line.IndexOf("", cleanse_pos) = -1 Then GoTo ESCAPE 'if there is a start comment tag but no end commment tag move along and ignore this method Else cleanse_pos_end = line.IndexOf("-->", cleanse_pos) End If line_content = line.Substring(anchor_outcom, cleanse_pos - anchor_outcom) rebuild_line = rebuild_line + line_content 'rebulding the string around the comments anchor_outcom = cleanse_pos_end + Len("-->") End While If Len(rebuild_line) > 0 Then rebuild_line = rebuild_line + line.Substring(anchor_outcom, Len(line) - anchor_outcom) 'finalise stitching of string back together line = rebuild_line End If ESCAPE: 'get out of the while loop for comments because comments closing tags could not be found If line.IndexOf("", t_loc) Catch outOfRange As ArgumentOutOfRangeException Console.WriteLine("Element ignored: {0}", outOfRange.Message) 'handle the situation when the tag is not there End Try t_cont_loc = t_loc + t_length 'find the starting point of the contents of the current tag t_cont_length = t_loc_end - t_cont_loc 'find the length (in char) of the contents of the current tag t_contents = line.Substring(t_cont_loc, t_cont_length) 'extract the contents of the current tag If tag_explicit = uuid_tag_exp Then this_t_uuid = t_contents End If If t_contents.IndexOf(",") <> -1 Then t_contents = """" + t_contents + """" 'handle commas in csv data' End If If Len(csv_line) = 0 Then csv_line = t_contents 'prevent the csv from starting with a comma Else csv_line = csv_line + "," + t_contents 'add a comma and the current tag contents to the csv row. End If Next t_length = Len(tag_tag_exp) tag_csv_line = "" t_loc_end = 0 While line.IndexOf(tag_tag_exp, t_loc_end) <> -1 'cycle through catching all the instances of t_tag t_loc = line.IndexOf(tag_tag_exp, t_loc_end) t_loc_end = line.IndexOf("", t_loc) t_cont_loc = t_loc + t_length t_cont_length = t_loc_end - t_cont_loc 'pick out the location and length of the data t_contents = line.Substring(t_cont_loc, t_cont_length) If t_contents.IndexOf(",") <> -1 Then t_contents = """" + t_contents + """" 'handle commas in csv data' End If tag_csv_line = tag_csv_line + this_t_uuid + "," + t_contents + vbCrLf 'build csv rows over and over until all tags found End While t_length = Len(lib_tag_exp) lib_csv_line = "" t_loc_end = 0 While line.IndexOf(lib_tag_exp, t_loc_end) <> -1 'cycle through catching all the instances of t_tag t_loc = line.IndexOf(lib_tag_exp, t_loc_end) t_loc_end = line.IndexOf("", t_loc) t_cont_loc = t_loc + t_length t_cont_length = t_loc_end - t_cont_loc 'pick out the location and length of the data t_contents = line.Substring(t_cont_loc, t_cont_length) If t_contents.IndexOf(",") <> -1 Then t_contents = """" + t_contents + """" 'handle commas in csv data' End If lib_csv_line = lib_csv_line + this_t_uuid + "," + t_contents + vbCrLf 'build csv rows over and over until all lib uuids found End While File.AppendAllText(path + "_tagtable.csv", tag_csv_line) 'append line to csv file File.AppendAllText(path + "_libtable.csv", lib_csv_line) 'append line to csv file Else For Each tag As String In maintable 'cycle through tags t_loc = line.IndexOf(tag) 'find current tag in file t_length = Len(tag) 'get length of current tag Dim Close_tag_stage As String = "" Dim Close_tag As String = "" Close_tag_stage = tag.Substring(1) 'take everything but the first character of the current tag Close_tag = "" Then this_t_uuid = t_contents End If If t_contents.IndexOf(",") <> -1 Then t_contents = """" + t_contents + """" 'handle commas in csv End If If Len(csv_line) = 0 Then csv_line = t_contents 'prevent commas from being applied to the beginning of csv row Else csv_line = csv_line + "," + t_contents 'build csv row by adding contents and then a comma for each tag End If Next t_length = Len(tag_tag) tag_csv_line = "" t_loc_end = 0 While line.IndexOf(tag_tag, t_loc_end) <> -1 'get the tag data out of t-file t_loc = line.IndexOf(tag_tag, t_loc_end) t_loc_end = line.IndexOf("", t_loc) t_cont_loc = t_loc + t_length t_cont_length = t_loc_end - t_cont_loc t_contents = line.Substring(t_cont_loc, t_cont_length) If t_contents.IndexOf(",") <> -1 Then t_contents = """" + t_contents + """" 'handle commas in csv data' End If tag_csv_line = tag_csv_line + this_t_uuid + "," + t_contents + vbCrLf 'build csv rows End While t_length = Len(lib_tag) lib_csv_line = "" t_loc_end = 0 While line.IndexOf(lib_tag, t_loc_end) <> -1 t_loc = line.IndexOf(lib_tag, t_loc_end) t_loc_end = line.IndexOf("", t_loc) t_cont_loc = t_loc + t_length t_cont_length = t_loc_end - t_cont_loc 'pick out lib uuid data t_contents = line.Substring(t_cont_loc, t_cont_length) If t_contents.IndexOf(",") <> -1 Then t_contents = """" + t_contents + """" 'handle commas in csv data' End If lib_csv_line = lib_csv_line + this_t_uuid + "," + t_contents + vbCrLf 'build csv rows End While File.AppendAllText(path + "_tagtable.csv", tag_csv_line) 'append line to csv file File.AppendAllText(path + "_libtable.csv", lib_csv_line) 'append line to csv file End If File.AppendAllText(path + "_maintable.csv", csv_line) 'append line to csv file File.AppendAllText(path + "_maintable.csv", vbCrLf) 'ad a new line feed to the csv file Next Console.WriteLine() Console.WriteLine("Done! csv files have been saved to " + path) Console.WriteLine("Press any key...") Console.ReadKey() GoTo MAIN_MENU SET_FILEPATH: Console.Clear() Console.WriteLine("Current path - " + path) Console.WriteLine("Type in new directory path (enter x to cancel)") Console.WriteLine() proposed_path = Console.ReadLine() If proposed_path = "x" Then GoTo MAIN_MENU End If path = proposed_path Console.WriteLine("New path - " + path) Console.WriteLine() Console.WriteLine("Press any key...") Console.ReadKey() GoTo MAIN_MENU GUIDE: Console.Clear() Console.WriteLine("Hi, this tool converts t files (i.e. files conforming to the tschema.xsd) into csv format. " & _ "It works like this, you put your t files in a folder (e.g. C:\T2CSV\) and the app finds the files and " & _ "converts them into three csv tables (i.e. _maintable.csv, _tagtable.csv, _libtable.csv). Some things to note:") Console.WriteLine() Console.WriteLine(" - The leading underscore in the filename is just to promote the csv files to the top of the list in the directory (since there could be thousands of t-files in there too).") Console.WriteLine() Console.WriteLine(" - You can change the conversion directory if you are not happy with the default, that's cool, no hard feelings, I'm sure you have your reasons. ") Console.WriteLine() Console.WriteLine(" - Once you have your csv files you can open and join them in visualization software and see all the powerful features of the tschema in action") Console.WriteLine() Console.WriteLine(" - If you raise an exception which I haven't handled or find a bug then please pass the info on to software.stevebowden@gmail.com") Console.WriteLine() Console.WriteLine("Press any key...") Console.ReadKey() GoTo MAIN_MENU ABOUT: Console.Clear() Console.WriteLine("T2CSV Converter - by Steven Bowden is licensed under a Creative Commons Attribution 4.0 International License. Based on a work at tschema.org/T2CSV/index.html") Console.WriteLine() Console.WriteLine("Performance-wise it is pretty impressive and overloads are unexpected, if you get an overload just modify the code to write to the .csv more regularly. " & _ "The code can be downloaded from tschema.org/T2CSV/index.html") Console.WriteLine() Console.WriteLine("The code is just string-play, nothing fancy so anyone with a basic understanding can get it and see " & _ "that there is no voodoo in data extraction and transformation, it's anyones game.") Console.WriteLine("Check out tschema.org for more information on the t schema and t files etc.") Console.WriteLine() Console.WriteLine("Contact email : software.stevebowden@gmail.com") Console.WriteLine() Console.WriteLine("DISCLAIMER : no guarantees the software is perfect, use at your own risk. Always validate results to ensure the tool is working for your specific usage case") Console.WriteLine() Console.WriteLine("Press any key...") Console.ReadKey() GoTo MAIN_MENU End Sub End Module