{"id":205,"date":"2011-05-31T15:26:57","date_gmt":"2011-05-31T15:26:57","guid":{"rendered":"http:\/\/craftydba.com\/?p=205"},"modified":"2024-02-17T14:26:47","modified_gmt":"2024-02-17T14:26:47","slug":"text-files","status":"publish","type":"post","link":"https:\/\/craftydba.com\/?p=205","title":{"rendered":"Text Files"},"content":{"rendered":"<p>The first type of file that every programmer writes to and reads from is an ASCII <a href=\"http:\/\/en.wikipedia.org\/wiki\/Text_file\">text file <\/a>. A text file is a kind of computer file that is structured as a sequence of lines. Each line is terminated with a special character such as a line feed (LF) in UNIX or a carriage return (CR) and line feed (LF) in MS DOS.<\/p>\n<p>I have abstracted the details behind reading and writing files by encapsulating the logic into two classes for your use.<\/p>\n<p>The write text file class has the following methods.<\/p>\n<ul>\n<li>OpenDataFile() &#8211; Open a given file in write or append mode.<\/li>\n<li>PushData() &#8211; Write a line of data to the given file.<\/li>\n<li>CloseDataFile() &#8211; Close the given file.<\/li>\n<li>RemoveFile() &#8211; Remove a given file.<\/li>\n<\/ul>\n<p>The read text file class has the following properties and methods.<\/p>\n<ul>\n<li>EOF &#8211; a property that reflects if we are at the end of file.<\/li>\n<li>OpenDataFile() &#8211; Open a given file in read mode.<\/li>\n<li>PullData() &#8211; Return a line of data if not EOF.<\/li>\n<li>CloseDataFile() &#8211; Close the given file.<\/li>\n<li>FileExists() &#8211; Does a given file exist?<\/li>\n<li>PullAllData() &#8211; Return the contents of the file as one big string.<\/li>\n<\/ul>\n<p>One special type of text file is called a Comma Seperated Value (CSV) file.\u00a0 <a href=\"http:\/\/en.wikipedia.org\/wiki\/CSV_file\">CSV<\/a> is a delimited data format that has fields\/columns separated by the comma character and records\/rows terminated by newlines.\u00a0 Fields that contain a special character (comma, newline, or double quote), must be enclosed in double quotes.<\/p>\n<p>In real life practice, CSV files can be problematic if care is not given to scrub special characters from the data fields before writing to the file.<\/p>\n<p>The write sample program creates a CSV file representing the first five US Presidents demographic information.\u00a0 The read sample program\u00a0prints the file information to the MS DOS screen.<\/p>\n<p>In summary, writing to and reading from ASCII text files is very easy.\u00a0 The supplied classes can be used to create files of many different formats such as Tab Delimited and Comma Seperated Values to name a few.<\/p>\n<p>The table below has starter code that you can use in your next Visual Basic (VB) Script.<\/p>\n<table border=\"1\" cellspacing=\"1\" cellpadding=\"1\" width=\"600\" align=\"left\">\n<tbody>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/mod-read-text-file.vbs_.txt\">mod-read-text-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Read Text File Class<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/mod-write-text-file.vbs_.txt\">mod-write-text-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Write Text File Class<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/tst-read-text-file.vbs.txt\">tst-read-text-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Read Sample Program<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/tst-write-text-file.vbs.txt\">tst-write-text-file.vbs<\/a><\/td>\n<td style=\"border: thin solid gray;\">Write Sample Program<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/read.cmd.txt\">read.cmd<\/a><\/td>\n<td style=\"border: thin solid gray;\">Read Batch File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/write.cmd.txt\">write.cmd<\/a><\/td>\n<td style=\"border: thin solid gray;\">Write Batch File<\/td>\n<\/tr>\n<tr>\n<td style=\"border: thin solid gray;\"><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/presidents.csv\">presidents.csv<\/a><\/td>\n<td style=\"border: thin solid gray;\">CSV Sample File<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><\/P><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>\nThis is a screen shot of the read sample VB Script excuting in a MS DOS command shell.<br \/>\n<\/P><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/read-screen-output.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-medium wp-image-226\" title=\"read-screen-output\" src=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/read-screen-output-300x190.jpg\" alt=\"Output from read text file program\" width=\"300\" height=\"190\" srcset=\"https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/read-screen-output-300x190.jpg 300w, https:\/\/craftydba.com\/wp-content\/uploads\/2011\/05\/read-screen-output.jpg 689w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The first type of file that every programmer writes to and reads from is an ASCII text file . A text file is a kind of computer file that is structured as a sequence of lines. Each line is terminated with a special character such as a line feed (LF) in UNIX or a carriage return (CR) and line feed (LF) in MS DOS. I have abstracted the details behind reading and writing files by encapsulating the logic into two classes for your use. The write text file class has&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[12,15,11,13],"class_list":["post-205","post","type-post","status-publish","format-standard","hentry","category-other","tag-free-code","tag-john-f-miner-iii","tag-text-files","tag-vb-script-2"],"_links":{"self":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/205","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=205"}],"version-history":[{"count":0,"href":"https:\/\/craftydba.com\/index.php?rest_route=\/wp\/v2\/posts\/205\/revisions"}],"wp:attachment":[{"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=205"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=205"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/craftydba.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=205"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}