{"id":70583,"date":"2005-01-25T09:33:00","date_gmt":"2005-01-25T09:33:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/25\/how-can-i-read-just-a-single-specified-line-from-a-text-file\/"},"modified":"2005-01-25T09:33:00","modified_gmt":"2005-01-25T09:33:00","slug":"how-can-i-read-just-a-single-specified-line-from-a-text-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-read-just-a-single-specified-line-from-a-text-file\/","title":{"rendered":"How Can I Read Just a Single, Specified Line from a Text File?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I read only a specific line in a text file? For example, how can I write a script that reads only line 16 in a text file and then stores the value of that line in a variable?<BR><BR>&#8212; DL<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, DL. You know we haven\u2019t answered a question about text files in awhile, and that\u2019s not something we should brag about; after all, it sometimes seems like every other question we get has to do with reading from and writing to text files. People always ask if we\u2019ve ever met Bill Gates; the truth is we always try to avoid Bill Gates, because we\u2019re afraid <I>he\u2019ll<\/I> ask us questions about text files. <\/P>\n<P>Of course, the reason we get so many questions about text files is the fact that the scripting technologies built into Windows don\u2019t have very sophisticated text handling capabilities. Take this question, for example. You might think that WSH or VBScript or <I>some<\/I> scripting object would have a method in which you could say, \u201cHey, go read line 16 and only line 16 of this file.\u201d No such luck. The FileSystemObject &#8211; which provides most of the text-handling capabilities available to scripters &#8211; can only start with line 1 of a file and then works its way down. There\u2019s no way to quickly jump to a specified line.<\/P>\n<P>Now, that doesn\u2019t mean we can\u2019t store just the value of line 16 in a variable; it just means we have to use a somewhat less-than-elegant approach. For the sake of brevity, let\u2019s consider a simple text file with five lines:<\/P><PRE class=\"codeSample\">1\n2\n3\nThis is line no. 4 in the text file.\n5\n<\/PRE>\n<P>We want to store the contents of line 4 &#8211; and just the contents of line 4 &#8211; in a variable. We can\u2019t jump to line 4 directly, but we <I>can<\/I> achieve the desired result by using a script like this:<\/P><PRE class=\"codeSample\">Const ForReading = 1<\/p>\n<p>Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objTextFile = objFSO.OpenTextFile(&#8220;mylogfile.log&#8221;, ForReading)<\/p>\n<p>For i = 1 to 3\n    objTextFile.ReadLine\nNext<\/p>\n<p>strLine = objTextFile.ReadLine\nWscript.Echo strLine<\/p>\n<p>objTextFile.Close\n<\/PRE>\n<P>So what\u2019s going on here? Well, we begin by creating an instance of the FileSystemObject and opening the file Mylogfile.log for reading. We then create a For Next loop that loops from 1 to 3. Inside that loop, we use the ReadLine method to read the current line in the text file. <\/P>\n<P>Why? Well, remember, all we\u2019re really interested in is line 4, but the FileSystemObject won\u2019t let us go directly to line 4; instead, we have to get through lines 1, 2, and 3 first. That\u2019s exactly what this block of code does. The first time through the loop, it calls the ReadLine method; this reads the first line in the text file (although we don\u2019t do anything with the information we read), and then automatically drops us down to the next line in the file (line 2). The second time through, we read line 2, and drop down to line 3. The third time through &#8211; that\u2019s right, we read line 3, and then drop down to line 4, the line we\u2019re interested it.<\/P>\n<P>At this point, we\u2019re done with our loop. We call the ReadLine method again, only this time we store the contents of that line in the variable strLine. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">strLine = objTextFile.ReadLine\n<\/PRE>\n<P>For the purposes of our demonstration script, we then echo the value of strLine and close the text file. We weren\u2019t able to use any nifty shortcuts, but we achieved our goal: we managed to get the value of line 4 into the variable strLine.<\/P>\n<P>Like we said, not very elegant, but it works. And it works reasonably fast, too. For example, create a sample log file with 10,000 lines, and modify the script so it stores just the value of line 9999 in a variable. (How would you do that? Change the For Next loop so that it loops from 1 to 9998.) Then echo the value of line 9999. If you try this, you\u2019ll see that &#8211; elegance aside &#8211; the whole process only takes a second or so. Beauty is in the eye of the beholder, and we find any script that takes only a second or two to complete to be very beautiful indeed.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I read only a specific line in a text file? For example, how can I write a script that reads only line 16 in a text file and then stores the value of that line in a variable?&#8212; DL Hey, DL. You know we haven\u2019t answered a question about text [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,14,5],"class_list":["post-70583","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I read only a specific line in a text file? For example, how can I write a script that reads only line 16 in a text file and then stores the value of that line in a variable?&#8212; DL Hey, DL. You know we haven\u2019t answered a question about text [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70583","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70583"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70583\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70583"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70583"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70583"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}