{"id":26213,"date":"2007-06-29T10:00:00","date_gmt":"2007-06-29T10:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2007\/06\/29\/dont-forget-to-pass-the-current-directory-along-with-the-command-line-to-your-single-instance-program\/"},"modified":"2007-06-29T10:00:00","modified_gmt":"2007-06-29T10:00:00","slug":"dont-forget-to-pass-the-current-directory-along-with-the-command-line-to-your-single-instance-program","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20070629-00\/?p=26213","title":{"rendered":"Don&#039;t forget to pass the current directory along with the command line to your single-instance program"},"content":{"rendered":"<p>\nIf you make a single-instance program,\nand somebody runs a second copy of the program and passes a command line,\nthe most common way\nof handling this is to hand the command line to the first\ncopy of the program and let the first copy deal with it.\nWhen you do this, don&#8217;t forget about the current directory.\n<\/p>\n<p>\nIf somebody passes a relative path to the second copy of the program,\nthat relative path needs to be resolved against the current directory\nof the <i>second<\/i> program.\nI&#8217;ve seen programs that fail to take this into account.\nInstead, they pass the command line to the first copy of the program,\nand the first copy resolves the relatives paths against <i>its<\/i>\ncurrent directory.\n<\/p>\n<p>\nAllow me to give a concrete example.\n<\/p>\n<pre>\nC:\\Directory1&gt; start LitWare file1.lit\n... runs LitWare with the file C:\\Directory1\\file1.lit ...\nC:\\Directory1&gt; cd ..\\Directory2\nC:\\Directory2&gt; start LitWare file2.lit\n<\/pre>\n<p>\nWhat you expect to happen is that LitWare opens\nthe file <code>C:\\Directory2\\file2.lit<\/code>,\nsince the relative path <code>file2.lit<\/code> should be\nresolved against the current directory, <code>C:\\Directory2<\/code>.\nUnfortunately, I see some programs&loz; try\nto open the file <code>C:\\Directory1\\file2.lit<\/code>\nsince they passed the command line to the first copy,\nand the first copy then resolved the relative path <code>file2.lit<\/code>\nagainst the current directory of the first copy,\nnamely <code>C:\\Directory1<\/code>.\n<\/p>\n<p>\nResult: &#8220;File not found error.&#8221;\n<\/p>\n<p>\nMoral of the story:\nBe mindful of the current directory when parsing the command line.\nYou can either have the second copy parse the command line\n(and resolve the relative paths against its own current directory),\nor you can pass the current directory to the first copy\n(and resolve the relative paths against that directory).\nEither works.\nWhat doesn&#8217;t work is passing the relative paths to the first\ncopy and having the first copy resolve it against its own current\ndirectory.\n<\/p>\n<p>\nMy workaround for dealing with\nthese programs is using the <code>%CD%<\/code> pseudo-variable.\n<\/p>\n<pre>\nC:\\Directory2&gt; start LitWare %CD%\\file2.lit\n<\/pre>\n<p>\nThe <code>%CD%<\/code> pseudo-variable\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2005\/01\/28\/362565.aspx\">\nexpands to the command prompt&#8217;s current directory<\/a>.\n(Don&#8217;t forget to enclose it in quotation marks if the path contains\na space.)\n<\/p>\n<p>\n<b>Nitpicker&#8217;s corner<\/b>\n<\/p>\n<p>&loz;Remember that the phrase &#8220;some programs&#8221; means\n&#8220;some programs&#8221; and\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2006\/07\/13\/664448.aspx\">\nMicrosoft programs fall under the category of &#8220;programs&#8221;<\/a>.<font FACE=\"Lucida Sans Unicode\">&#x263C;<\/font>\n<\/p>\n<p>\n<font FACE=\"Lucida Sans Unicode\">&#x263C;<\/font>Yes, I&#8217;m using strange symbols as note markers.\nMy hope is that the\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/04\/10\/2065725.aspx#2082757\">\npeople who nitpick<\/a>\nover\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/05\/22\/2777876.aspx#2802902\">\nmy choice of note markers<\/a>\nwill\nget\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/05\/30\/2977229.aspx#2995067\">\nso upset<\/a>\nthat their heads will explode and they therefore will\nbe unable to nitpick further.\nIt appears that some people, on the other hand,\nsuffer from some medical condition that\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2007\/06\/19\/3393109.aspx#3417601\">\ndisables the &#8220;fun&#8221; center of the brain<\/a>.\n<\/p>\n<p>\n[Update: Give IE6 a little font help.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you make a single-instance program, and somebody runs a second copy of the program and passes a command line, the most common way of handling this is to hand the command line to the first copy of the program and let the first copy deal with it. When you do this, don&#8217;t forget about [&hellip;]<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-26213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>If you make a single-instance program, and somebody runs a second copy of the program and passes a command line, the most common way of handling this is to hand the command line to the first copy of the program and let the first copy deal with it. When you do this, don&#8217;t forget about [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/26213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=26213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/26213\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=26213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=26213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=26213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}