{"id":69743,"date":"2005-05-24T11:18:00","date_gmt":"2005-05-24T11:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/05\/24\/how-can-i-use-ado-to-open-a-text-file-that-has-spaces-in-the-file-name\/"},"modified":"2005-05-24T11:18:00","modified_gmt":"2005-05-24T11:18:00","slug":"how-can-i-use-ado-to-open-a-text-file-that-has-spaces-in-the-file-name","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-ado-to-open-a-text-file-that-has-spaces-in-the-file-name\/","title":{"rendered":"How Can I Use ADO to Open a Text File That has Spaces in the File Name?"},"content":{"rendered":"<p><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\"> <\/P>\n<P>Hey, Scripting Guy! How can I use ADO to open a text file that has spaces in the file name?<BR><BR>&#8212; TL<\/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, TL. You might not even remember sending us this question; to say it\u2019s been awhile would be an understatement. (You should have seen all the dust and cobwebs we had to brush off this question before we could post it in the Script Center.) A <I>long<\/I> time ago you wrote to us asking a simple question: how can you use ADO to open a text file if that file happens to have spaces in the file name? That seemed easy enough, but &#8211; to tell you the truth &#8211; we couldn\u2019t figure it out. We tried inserting double quotes and single quotes, we tried using short file names, and we scoured the Web looking for the answer. In baseball parlance, we struck out. <\/P>\n<TABLE id=\"E3C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. We even read the collected works of Albert Einstein and Sir Isaac Newton, hoping one of them had come up with an answer. Unfortunately, they don\u2019t appear to have had any more luck than we did.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So did we give up? As a matter of fact, we did. Nevertheless, we always had this in the back of our minds, and every now and then we\u2019d try something different, just in case. Each time we tried something new we failed. <\/P>\n<P>And then the other day, while looking up some information on Microsoft Excel, we stumbled upon the answer. (Actually we stumbled upon the answer to a different question, but as it turned out the same technique that worked for Excel also works for text files.) So at long last, TL, we can give you (or at least your great-great-grandchildren) an answer to the question, \u201cHow can I use ADO to open a text file that has spaces in the file name?\u201d<\/P>\n<P>Before we do that we should probably explain the situation to the rest of the world. ADO (ActiveX Data Objects) enables you to use database techniques to read data from a text file. We won\u2019t go into all the details today; for more information, take a look at this <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/dnclinic\/html\/scripting03092004.asp\" target=\"_blank\"><B>Scripting Clinic column<\/B><\/A> on MSDN. But suppose you have a simple text file that looks like this (just pretend there are a thousand or so lines in the file):<\/P><PRE class=\"codeSample\">Name,Department,Title\nKen Myer,Finance,Fiscal Specialist\nPilar Ackerman,Research,Manager\nJonathan Haas,Headquarters,Fiscal Specialist\n<\/PRE>\n<P>If you\u2019d like a list of all the Fiscal Specialists, you can easily retrieve that information using an ADO script similar to this, and without having to read through the text file line-by-line:<\/P><PRE class=\"codeSample\">Const adOpenStatic = 3\nConst adLockOptimistic = 3\nConst adCmdText = &amp;H0001<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objRecordSet = CreateObject(&#8220;ADODB.Recordset&#8221;)<\/p>\n<p>strPathtoTextFile = &#8220;C:\\Scripts\\Text files&#8221;<\/p>\n<p>objConnection.Open &#8220;Provider=Microsoft.Jet.OLEDB.4.0;&#8221; &amp; _\n    &#8220;Data Source=&#8221; &amp; strPathtoTextFile &amp; &#8220;;&#8221; &amp; _\n        &#8220;Extended Properties=&#8221;&#8221;text;HDR=YES;FMT=Delimited&#8221;&#8221;&#8221;<\/p>\n<p>strFile = &#8220;Employees.txt&#8221;<\/p>\n<p>objRecordset.Open &#8220;SELECT * FROM &#8221; &amp; strFile &amp; &#8221; WHERE Title = &#8216;Fiscal Specialist'&#8221;, _\n    objConnection, adOpenStatic, adLockOptimistic, adCmdText<\/p>\n<p>Do Until objRecordset.EOF\n    Wscript.Echo objRecordset.Fields.Item(&#8220;Name&#8221;)\n    Wscript.Echo objRecordset.Fields.Item(&#8220;Department&#8221;)\n    Wscript.Echo objRecordset.Fields.Item(&#8220;Title&#8221;)   \n    objRecordset.MoveNext\nLoop\n<\/PRE>\n<P>What we\u2019re doing here is connecting to the folder <B>C:\\Scripts\\Text files<\/B> and then querying the file <B>Employees.txt<\/B>. Here\u2019s the line of code that specifies the name of the file we want to work with:<\/P><PRE class=\"codeSample\">strFile = &#8220;Employees.txt&#8221;\n<\/PRE>\n<P>This works great: the script will run, and it will report back the name, department, and title of all the Fiscal Specialists.<\/P>\n<P>So what\u2019s the problem? Well, suppose our text file is named <B>Fabrikam Employees.txt<\/B>. No big deal, right? After all, you just change the value of the variable strFile to this:<\/P><PRE class=\"codeSample\">strFile = &#8220;Fabrikam Employees.txt&#8221;\n<\/PRE>\n<P>Well, that\u2019s what we thought, too. But here\u2019s what we got when we ran the script:<\/P><PRE class=\"codeSample\">Microsoft JET Database Engine: Syntax error in FROM clause.\n<\/PRE>\n<P>Yikes. Obviously the space in the file name <B>Fabrikam Employees.txt<\/B> is causing problems; that we could figure out. What we couldn\u2019t figure out was how to work around that problem. We tried all sorts of different permutations and combinations of single quotes and double quotes, only to discover that the answer is actually much easier: all we have to do is enclose the file name in square brackets.<\/P>\n<P>Yep, square brackets. If we set the value of strFile to <I>this<\/I> the script will work just fine:<\/P><PRE class=\"codeSample\">strFile = &#8220;[Fabrikam Employees.txt]&#8221;\n<\/PRE>\n<P>To be honest, this was a little disappointing. After spending all that time and effort we were hoping the answer would be really complicated; that way we could say, \u201cWell, it took us a long time, but that\u2019s only because this is such a complex problem that it required incredible feats of intellectual reasoning and ability before we could determine the answer.\u201d Instead all we can say is, \u201cJust enclose your file name in square brackets.\u201d But, hey, at least it works, right? And, like we said, we couldn\u2019t find anything in their writings to indicate that either Albert Einstein or Isaac Newton <I>ever<\/I> figured this out. So there.<\/P>\n<P>By the way, here\u2019s what the complete script looks like:<\/P><PRE class=\"codeSample\">Const adOpenStatic = 3\nConst adLockOptimistic = 3\nConst adCmdText = &amp;H0001<\/p>\n<p>Set objConnection = CreateObject(&#8220;ADODB.Connection&#8221;)\nSet objRecordSet = CreateObject(&#8220;ADODB.Recordset&#8221;)<\/p>\n<p>strPathtoTextFile = &#8220;C:\\Scripts\\Text files&#8221;<\/p>\n<p>objConnection.Open &#8220;Provider=Microsoft.Jet.OLEDB.4.0;&#8221; &amp; _\n    &#8220;Data Source=&#8221; &amp; strPathtoTextFile &amp; &#8220;;&#8221; &amp; _\n        &#8220;Extended Properties=&#8221;&#8221;text;HDR=YES;FMT=Delimited&#8221;&#8221;&#8221;<\/p>\n<p>strFile = &#8220;[Fabrikam Employees.txt]&#8221;<\/p>\n<p>objRecordset.Open &#8220;SELECT * FROM &#8221; &amp; strFile &amp; &#8221; WHERE Title = &#8216;Fiscal Specialist'&#8221;, _\n    objConnection, adOpenStatic, adLockOptimistic, adCmdText<\/p>\n<p>Do Until objRecordset.EOF\n    Wscript.Echo objRecordset.Fields.Item(&#8220;Name&#8221;)\n    Wscript.Echo objRecordset.Fields.Item(&#8220;Department&#8221;)\n    Wscript.Echo objRecordset.Fields.Item(&#8220;Title&#8221;)   \n    objRecordset.MoveNext\nLoop\n<\/PRE>\n<P><B>Added bonus<\/B>. Ironically, after trying so hard to find <I>one<\/I> way to access text files that have spaces in their file names we ended up finding <I>two<\/I> ways. You can also enclose the file name using the ` character. This isn\u2019t the single quote mark; instead it\u2019s the accent grave character found on the same key as the ~ character (on U.S. English keyboards anyway). In other words, you can also set the value of strFile to this and your script will work just fine:<\/P><PRE class=\"codeSample\">strFile = &#8220;`Fabrikam Employees.txt`&#8221;\n<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I use ADO to open a text file that has spaces in the file name?&#8212; TL Hey, TL. You might not even remember sending us this question; to say it\u2019s been awhile would be an understatement. (You should have seen all the dust and cobwebs we had to brush off [&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":[19,146,3,5],"class_list":["post-69743","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-activex-data-objects-ado","tag-databases","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I use ADO to open a text file that has spaces in the file name?&#8212; TL Hey, TL. You might not even remember sending us this question; to say it\u2019s been awhile would be an understatement. (You should have seen all the dust and cobwebs we had to brush off [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69743","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=69743"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69743\/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=69743"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69743"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69743"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}