{"id":335,"date":"2011-09-13T16:29:39","date_gmt":"2011-09-13T21:29:39","guid":{"rendered":"synetech.ddns.net\/blogs\/smarticles\/?p=335"},"modified":"2011-09-13T23:48:07","modified_gmt":"2011-09-14T04:48:07","slug":"current-working-directory-in-windows-console-programs","status":"publish","type":"post","link":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/2011\/09\/13\/current-working-directory-in-windows-console-programs\/","title":{"rendered":"Current Working Directory in Windows Console Programs"},"content":{"rendered":"<p>Pursuant\u00a0to an argument at this <a title=\"What is the difference between \u201cC:FILE.TXT\u201d and \u201cC:\\FILE.TXT\u201d?\" href=\"http:\/\/superuser.com\/questions\/314606\/what-is-the-difference-between-cfile-txt-and-c-file-txt\/\" target=\"_blank\">Super User question<\/a>, I have done some testing and written a test program to try to clarify the situation with current-working-directories.<\/p>\n<p>As Raymond Chen <a title=\"Why does each drive have its own current directory?\" href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/10\/11\/10073890.aspx\" target=\"_blank\">explains<\/a>, per-drive CWDs have been around since DOS, which makes sense because DOS\u2019s only interface was the command-line. He goes on to <a title=\"What are these strange =C: environment variables?\" href=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2010\/05\/06\/10008132.aspx\" target=\"_blank\">explain<\/a> that in Windows, the command prompt keeps track of per-drive CWDs by using environment variables.<\/p>\n<p>The point is that while the actual manner in which they are tracked may differ, there are per-drive CWDs in all Microsoft operating systems from DOS, up to Windows 7. This can be easily demonstrated with the following program (adjusting drive letters and paths as appropriate).<\/p>\n<blockquote style=\"font-style:normal;\">\n<pre>#include\t<conio.h>\r\n#include\t<stdio.h>\r\n#include\t<direct.h>\r\n\r\nvoid PrintCWDs() {\r\n\tchar cwd[260]=\"\";\r\n\tfor (int i=3; i<27; i++) {\r\n\t\tif (!_chdrive(i)) {\r\n\t\t\tgetcwd(cwd,260);\r\n\t\t\tprintf(\"  %c: = %s\\n\", 64+i, cwd);\r\n\t\t}\r\n\t}\r\n\tprintf(\"Press a key...\\n\");\r\n\tchar key=getch();\r\n}\r\n\r\nvoid ChangeCWDs() {\r\n\tfor (int i=3; i<27; i++) {\r\n\t\tif (!_chdrive(i)) {\r\n\t\t\tswitch (i) {\r\n\t\t\t\tcase 'C'-'A'+1\t:chdir(\"\\\\Windows\\\\System32\\\\Drivers\");\t\tbreak;\r\n\t\t\t\tcase 'H'-'A'+1\t:chdir(\"\\\\Temp\\\\Chromium\\\\Cache\");\t\t\tbreak;\r\n\t\t\t\tcase 'K'-'A'+1\t:chdir(\"\\\\Data\\\\eMail\\\\OE\");\t\t\t\tbreak;\r\n\t\t\t\tcase 'P'-'A'+1\t:chdir(\"\\\\Videos\\\\Commercials\");\t\t\tbreak;\r\n\t\t\t\tcase 'T'-'A'+1\t:chdir(\"\\\\Program Files\\\\Common Files\");\tbreak;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n}\r\n\r\nint main(int argc, char**argv) {\r\n\tprintf(\"\\nOriginal CWDs:\\n\");\r\n\tPrintCWDs();\r\n\tprintf(\"\\nChanging CWDs...\\n\");\r\n\tChangeCWDs();\r\n\tprintf(\"\\nNew CWDs:\\n\");\r\n\tPrintCWDs();\r\n\tprintf(\"\\nAnd again:\\n\");\r\n\tPrintCWDs();\r\n\r\n\treturn 0;\r\n}\r\n<\/pre>\n<\/blockquote>\n<p>This program shows that the CWD is maintained for each drive in DOS, Windows XP, and Windows 7 as seen below.<\/p>\n<p>DOS:<\/p>\n<p><a href=\"\/blogs\/smarticles\/files\/2011\/09\/Without-NTFS.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blogs\/smarticles\/files\/2011\/09\/Without-NTFS-300x220.png\" alt=\"CWDTest run in DOS without NTFS support.\" title=\"Without NTFS\" width=\"300\" height=\"220\" class=\"alignnone size-medium wp-image-358\" srcset=\"https:\/\/synetech.ddns.net\/blogs\/smarticles\/files\/2011\/09\/Without-NTFS-300x220.png 300w, https:\/\/synetech.ddns.net\/blogs\/smarticles\/files\/2011\/09\/Without-NTFS.png 720w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p><a href=\"\/blogs\/smarticles\/files\/2011\/09\/With-NTFS.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blogs\/smarticles\/files\/2011\/09\/With-NTFS-300x250.png\" alt=\"CWDTest run in DOS with NTFS support.\" title=\"With NTFS\" width=\"300\" height=\"250\" class=\"alignnone size-medium wp-image-357\" srcset=\"https:\/\/synetech.ddns.net\/blogs\/smarticles\/files\/2011\/09\/With-NTFS-300x250.png 300w, https:\/\/synetech.ddns.net\/blogs\/smarticles\/files\/2011\/09\/With-NTFS.png 720w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>XP:<\/p>\n<p><a href=\"\/blogs\/smarticles\/files\/2011\/09\/From-Command-Prompt.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blogs\/smarticles\/files\/2011\/09\/From-Command-Prompt-105x300.png\" alt=\"CWDTest run in XP from the command-prompt.\" title=\"From Command-Prompt\" width=\"105\" height=\"300\" class=\"alignnone size-medium wp-image-359\" \/><\/a><\/p>\n<p><a href=\"\/blogs\/smarticles\/files\/2011\/09\/From-Desktop-or-Folder.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blogs\/smarticles\/files\/2011\/09\/From-Desktop-or-Folder-105x300.png\" alt=\"CWDTest run in XP from the desktop or an Explorer folder.\" title=\"From Desktop or Folder\" width=\"105\" height=\"300\" class=\"alignnone size-medium wp-image-360\" \/><\/a><\/p>\n<p><a href=\"\/blogs\/smarticles\/files\/2011\/09\/From-Run-Dialog.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blogs\/smarticles\/files\/2011\/09\/From-Run-Dialog-105x300.png\" alt=\"CWDTest run in XP from the Run dialog.\" title=\"From Run Dialog\" width=\"105\" height=\"300\" class=\"alignnone size-medium wp-image-361\" \/><\/a><\/p>\n<p>7:<\/p>\n<p><a href=\"\/blogs\/smarticles\/files\/2011\/09\/From-Command-Prompt1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blogs\/smarticles\/files\/2011\/09\/From-Command-Prompt1-105x300.png\" alt=\"CWDTest run in 7 from the command-prompt.\" title=\"From Command Prompt\" width=\"105\" height=\"300\" class=\"alignnone size-medium wp-image-362\" \/><\/a><\/p>\n<p><a href=\"\/blogs\/smarticles\/files\/2011\/09\/From-Desktop-or-Folder1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blogs\/smarticles\/files\/2011\/09\/From-Desktop-or-Folder1-105x300.png\" alt=\"CWDTest run in 7 from the desktop or an Explorer folder.\" title=\"From Desktop or Folder\" width=\"105\" height=\"300\" class=\"alignnone size-medium wp-image-363\" \/><\/a><\/p>\n<p><a href=\"\/blogs\/smarticles\/files\/2011\/09\/From-Run-Dialog1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"\/blogs\/smarticles\/files\/2011\/09\/From-Run-Dialog1-105x300.png\" alt=\"CWDTest run in XP from the Run dialog.\" title=\"From Run Dialog\" width=\"105\" height=\"300\" class=\"alignnone size-medium wp-image-364\" \/><\/a><\/p>\n<div class=\"tagcloud\"><a href=\"https:\/\/synetech.ddns.net\/blogs\/smarticles\/tag\/dos\/\" rel=\"tag\">DOS<\/a>, <a href=\"https:\/\/synetech.ddns.net\/blogs\/smarticles\/tag\/windows\/\" rel=\"tag\">Windows<\/a>, <a href=\"https:\/\/synetech.ddns.net\/blogs\/smarticles\/tag\/windows-7\/\" rel=\"tag\">Windows 7<\/a>, <a href=\"https:\/\/synetech.ddns.net\/blogs\/smarticles\/tag\/windows-xp\/\" rel=\"tag\">Windows XP<\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Pursuant\u00a0to an argument at this Super User question, I have done some testing and written a test program to try to clarify the situation with current-working-directories. As Raymond Chen explains, per-drive CWDs have been around since DOS, which makes sense because DOS\u2019s only interface was the command-line. He goes on to explain that in Windows, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[20,10,23,21],"class_list":["post-335","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-dos","tag-windows","tag-windows-7","tag-windows-xp"],"_links":{"self":[{"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/posts\/335","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/comments?post=335"}],"version-history":[{"count":0,"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/posts\/335\/revisions"}],"wp:attachment":[{"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/media?parent=335"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/categories?post=335"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/synetech.ddns.net\/blogs\/smarticles\/wp-json\/wp\/v2\/tags?post=335"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}