{"id":12563,"date":"2010-10-11T07:00:00","date_gmt":"2010-10-11T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2010\/10\/11\/why-does-each-drive-have-its-own-current-directory\/"},"modified":"2010-10-11T07:00:00","modified_gmt":"2010-10-11T07:00:00","slug":"why-does-each-drive-have-its-own-current-directory","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20101011-00\/?p=12563","title":{"rendered":"Why does each drive have its own current directory?"},"content":{"rendered":"<p>\nCommenter Dean Earley asks,\n&#8220;<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/pages\/407234.aspx#1816690\">Why is there a &#8216;current directory&#8217; AND an current drive<\/a>?\nWhy not merge them?&#8221;\n<\/p>\n<p>\nPithy answer:\nOriginally, each drive had its own current directory,\nbut now they don&#8217;t, but it looks like they do.\n<\/p>\n<p>\nOkay, let&#8217;s unwrap that sentence.\nYou actually know enough to answer the question yourself;\nyou just have to put the pieces together.\n<\/p>\n<p>\nSet the wayback machine to DOS&nbsp;1.0.\nEach volume was represented by a drive letter.\nThere were no subdirectories.\nThis behavior was carried forward from CP\/M.\n<\/p>\n<p>\nPrograms from the DOS&nbsp;1.0 era\ndidn&#8217;t understand subdirectories;\nthey referred to files by just drive letter and file name,\nfor example,\n<code>B:PROGRAM.LST<\/code>.\nLet&#8217;s fire up the assembler\n(compilers were for rich people)\nand assemble a program whose source code is on the A drive,\nbut sending the output to the B drive.\n<\/p>\n<table CELLPADDING=\"0\" CELLSPACING=\"0\">\n<tbody>\n<tr>\n<td VALIGN=\"baseline\"><code>A&gt;asm foo<\/code><\/td>\n<td>&nbsp; &nbsp; &nbsp;<\/td>\n<td VALIGN=\"baseline\" ALIGN=\"right\"><i>the &#8220;.asm&#8221; extension on &#8220;foo&#8221; is implied<\/i><\/td>\n<\/tr>\n<tr>\n<td VALIGN=\"baseline\" COLSPAN=\"3\"><code>Assembler version blah blah blah<\/code><\/td>\n<\/tr>\n<tr>\n<td VALIGN=\"baseline\" COLSPAN=\"3\"><code>Source File: FOO.ASM<\/code><\/td>\n<\/tr>\n<tr>\n<td VALIGN=\"baseline\"><code>Listing file [FOO.LST]: <u>NUL<\/u><\/code>\n    <\/td>\n<td><\/td>\n<td VALIGN=\"baseline\" ALIGN=\"right\"><i><a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2003\/10\/22\/55388.aspx\">throw away the listing file<\/a><\/i><\/td>\n<\/tr>\n<tr>\n<td VALIGN=\"baseline\"><code>Object file [FOO.OBJ]: <u>B:<\/u><\/code><\/td>\n<td><\/td>\n<td VALIGN=\"baseline\" ALIGN=\"right\"><i>send the object file to drive B<\/i><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\nSince we gave only a drive letter in response to the\n<code>Object file<\/code> prompt,\nthe assembler defaults to a file name of <code>FOO.OBJ<\/code>,\nresulting in the object file being generated as <code>B:FOO.OBJ<\/code>.\n<\/p>\n<p>\nOkay, now let&#8217;s introduce subdirectories into DOS&nbsp;2.0.\nSuppose you have want to assemble <code>A:\\SRC\\FOO.ASM<\/code>\nand put the result into\n<code>B:\\OBJ\\FOO.OBJ<\/code>.\nHere&#8217;s how you do it:\n<\/p>\n<pre>\nA&gt; B:\nB&gt; CD \\OBJ\nB&gt; A:\nA&gt; CD \\SRC\nA&gt; asm foo\nAssembler version blah blah blah\nSource File: FOO.ASM\nListing file [FOO.LST]: NUL\nObject file [FOO.OBJ]: B:\n<\/pre>\n<p>\nThe assembler reads from <code>A:FOO.ASM<\/code> and writes\nto <code>B:FOO.OBJ<\/code>,\nbut since the current directory is tracked on a per-drive basis,\nthe results are <code>A:\\SRC\\FOO.ASM<\/code> and\n<code>B:\\OBJ\\FOO.OBJ<\/code> as desired.\nIf the current directory were not tracked on a per-drive basis,\nthen there would be no way to tell the assembler to put its\noutput into a subdirectory.\nAs a result,\nDOS&nbsp;1.0 programs were effectively limited to operating on\nfiles in the root directory,\nwhich means that nobody would put files in subdirectories\n(because their programs couldn&#8217;t access them).\n<\/p>\n<p>\nFrom a DOS&nbsp;1.0 standpoint, changing the current directory\non a drive performs the logical equivalent of changing media.\n&#8220;Oh look, a completely different set of files!&#8221;\n<\/p>\n<p>\nShort attention span.\n<\/p>\n<p>\nRemembering the current directory for each drive has been preserved\never since,\nat least for batch files,\nalthough there isn&#8217;t actually such a concept as a <i>per-drive current\ndirectory<\/i> in Win32.\nIn Win32, all you have is <i>a<\/i> current directory.\nThe appearance that each drive has its own current directory\nis a fake-out by <code>cmd.exe<\/code>, which uses\n<a HREF=\"http:\/\/blogs.msdn.com\/oldnewthing\/archive\/2010\/05\/06\/10008132.aspx\">\nstrange environment variables<\/a>\nto create the illusion to batch files that each\ndrive has its own current directory.\n<\/p>\n<p>\nDean continues,\n&#8220;Why not merge them?\nI have to set both the dir and drive if i want a specific working dir.&#8221;\n<\/p>\n<p>\nThe answer to the second question is, &#8220;They already are merged.\nIt&#8217;s <code>cmd.exe<\/code> that tries to pretend that they aren&#8217;t.&#8221;\nAnd if you want to set the directory and the drive from the command\nprompt or a batch file, just use the <code>\/D<\/code> option to the\n<code>CHDIR<\/code> command:\n<\/p>\n<pre>\nD:\\&gt; CD \/D C:\\Program Files\\Windows NT\nC:\\Program Files\\Windows NT&gt; _\n<\/pre>\n<p>\n(Notice that the <code>CHDIR<\/code> command lets you omit quotation\nmarks around paths which contain spaces:\nSince the command takes only one path argument,\nthe lack of quotation marks does not introduce ambiguity.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Commenter Dean Earley asks, &#8220;Why is there a &#8216;current directory&#8217; AND an current drive? Why not merge them?&#8221; Pithy answer: Originally, each drive had its own current directory, but now they don&#8217;t, but it looks like they do. Okay, let&#8217;s unwrap that sentence. You actually know enough to answer the question yourself; you just have [&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":[2],"class_list":["post-12563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-history"],"acf":[],"blog_post_summary":"<p>Commenter Dean Earley asks, &#8220;Why is there a &#8216;current directory&#8217; AND an current drive? Why not merge them?&#8221; Pithy answer: Originally, each drive had its own current directory, but now they don&#8217;t, but it looks like they do. Okay, let&#8217;s unwrap that sentence. You actually know enough to answer the question yourself; you just have [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12563","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=12563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/12563\/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=12563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=12563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=12563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}