{"id":71703,"date":"2004-08-06T12:00:00","date_gmt":"2004-08-06T12:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2004\/08\/06\/how-can-i-use-long-file-names-as-parameters\/"},"modified":"2004-08-06T12:00:00","modified_gmt":"2004-08-06T12:00:00","slug":"how-can-i-use-long-file-names-as-parameters","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-use-long-file-names-as-parameters\/","title":{"rendered":"How Can I Use Long File Names as Parameters?"},"content":{"rendered":"<p><img decoding=\"async\" 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! I\u2019m trying to run a command-line tool from a script, but I need to pass it a long file name like <b>C:\\Documents and Settings\\All Users\\Desktop\\logfile.txt<\/b>. No matter how I try to pass that file name, however, my script blows up on me. Help!<\/p>\n<p>&#8212; WK, Birmingham, Great Britain<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, WK. Ah, yes: you\u2019ve encountered the dreaded double double-quotes problem. What does that mean? Well, let\u2019s say you\u2019re trying to run Notepad and pass it a long file name. In order to do that, you need to type the following command from the command prompt:<\/p>\n<pre class=\"codeSample\">notepad.exe \"c:\\my scripts\\logfile.txt\"\n<\/pre>\n<p>As you no doubt know, any time you need to do something from the command prompt that requires spaces in the path name, you need to enclose the entire path in double quote marks. Hence &#8220;c:\\my scripts\\logfile.txt&#8221;.<\/p>\n<p>So why does that cause problems with your script? Well, you\u2019re trying to start Notepad using the Windows Script Host Run method. To start a program using this method, you must enclose the complete command in double quote marks:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"Wscript.Shell\")\nobjShell.Run \"ping.exe 192.168.1.1\"\n<\/pre>\n<p>That works fine with the Ping example, but here\u2019s what you get when you try to run Notepad and pass it a long file name:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"Wscript.Shell\")\nobjShell.Run \"notepad.exe \"c:\\my scripts\\logfile.txt\"\"\n<\/pre>\n<p>As you have discovered, this doesn\u2019t work: you get an <b>Expected end of statement<\/b> error. That\u2019s because the script interpreter sees <b>&#8220;notepad.exe &#8221; <\/b>as being a complete line of code; consequently, as soon as it sees the rest of the code it freaks out. So now we\u2019ve got a problem: we need to enclose <i>c:\\my scripts\\logfile.txt<\/i> in double quote marks, but WSH freaks out when it sees double quotes inside of quotes. We\u2019re doomed, right?<\/p>\n<p>Right.<\/p>\n<p>No, hey, just kidding. There are actually a couple of ways around this, and here\u2019s one we like because it\u2019s versatile: the same approach can be used to embed single quotes and other reserved characters into command-line parameters. As you probably know, all characters on the keyboard have an ANSI equivalent; that\u2019s just a number that represents the character. For example, the lowercase letter <i>a<\/i> has an ANSI value of 97; the uppercase <i>A<\/i> has an ANSI value of 65. Still not impressed? Well it turns out that 34 is the ANSI value for double quote marks. <i>Now<\/i> you\u2019re impressed, right?<\/p>\n<p>Ok, then we\u2019ll explain it a bit more. VBScript has a function named Chr that converts an ANSI value into its character equivalent. For example, try to guess what you get when you run this script:<\/p>\n<pre class=\"codeSample\">Wscript.Echo Chr(65)\n<\/pre>\n<p>That\u2019s right; an uppercase <i>A<\/i> is echoed to the screen.<\/p>\n<p>Now to try to guess what happens when you run <i>this<\/i> script:<\/p>\n<pre class=\"codeSample\">Wscript.Echo Chr(34)\n<\/pre>\n<p>Yep; double quote marks get echoed to the screen. Now here\u2019s a tricky one:<\/p>\n<pre class=\"codeSample\">strArgument = \"notepad.exe \" &amp; chr(34) &amp; _\n    \"c:\\my scripts\\logfile.txt\" &amp; chr(34)\nWscript.Echo strArgument\n<\/pre>\n<p>Before you give up, here\u2019s the \u201cequation\u201d we\u2019re using:<\/p>\n<pre class=\"codeSample\">Notepad.exe + \" + c:\\my scripts.logfile.txt + \"\n<\/pre>\n<p>Add them altogether and you get this:<\/p>\n<pre class=\"codeSample\">notepad.exe \"c:\\my scripts.logfile.txt\"\n<\/pre>\n<p>In other words, we now have the exact string we\u2019d have to type at the command prompt in order to start Notepad and load in C:\\My Scripts\\Logfile.txt:<\/p>\n<pre class=\"codeSample\">notepad.exe \"c:\\my scripts.logfile.txt\"\n<\/pre>\n<p>Try this revised script, and see what happens:<\/p>\n<pre class=\"codeSample\">Set objShell = CreateObject(\"Wscript.Shell\")\nstrArgument = \"notepad.exe \" &amp; chr(34) &amp; _\n    \"c:\\my scripts\\logfile.txt\" &amp; chr(34)\nobjShell.Run strArgument\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019m trying to run a command-line tool from a script, but I need to pass it a long file name like C:\\Documents and Settings\\All Users\\Desktop\\logfile.txt. No matter how I try to pass that file name, however, my script blows up on me. Help! &#8212; WK, Birmingham, Great Britain Hey, WK. Ah, yes: [&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":[22,3,4,5],"class_list":["post-71703","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-retrieving-input","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I\u2019m trying to run a command-line tool from a script, but I need to pass it a long file name like C:\\Documents and Settings\\All Users\\Desktop\\logfile.txt. No matter how I try to pass that file name, however, my script blows up on me. Help! &#8212; WK, Birmingham, Great Britain Hey, WK. Ah, yes: [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71703","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=71703"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/71703\/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=71703"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=71703"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=71703"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}