{"id":36573,"date":"2005-01-28T10:02:00","date_gmt":"2005-01-28T10:02:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2005\/01\/28\/capturing-the-current-directory-from-a-batch-file\/"},"modified":"2005-01-28T10:02:00","modified_gmt":"2005-01-28T10:02:00","slug":"capturing-the-current-directory-from-a-batch-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20050128-00\/?p=36573","title":{"rendered":"Capturing the current directory from a batch file"},"content":{"rendered":"<p>\nSometimes people go to great lengths to get information which\nis available in a much simpler way.\nWe saw it a few days ago when we found a 200+-line C# program that\ncould be replaced with\n<a HREF=\"\/oldnewthing\/archive\/2005\/01\/20\/357225.aspx\">\na 90-byte batch file<\/a>.\nHere&#8217;s another example\n<a HREF=\"http:\/\/www.differentpla.net\/node\/view\/255\">\nof a rather roundabout way of\ncapturing the current directory from a batch file<\/a>.\n<\/p>\n<p>\nThe easy way is to use the <code>%CD%<\/code> pseudo-variable.\nIt expands to the current working directory.\n<\/p>\n<pre>\nset OLDDIR=%CD%\n.. do stuff ..\nchdir \/d %OLDDIR% &amp;rem restore current directory\n<\/pre>\n<p>\n(Of course, directory save\/restore could more easily have been\ndone with pushd\/popd, but that&#8217;s not the point here.)\n<\/p>\n<p>\nThe <code>%CD%<\/code> trick is handy even from the command line.\nFor example, I often find myself in a directory where there&#8217;s\na file that I want to operate on but&#8230; oh, I need to\nchdir to some other directory in order to perform that operation.\n<\/p>\n<pre>\nset _=%CD%\\curfile.txt\ncd ... some other directory ...\nsomecommand args %_% args\n<\/pre>\n<p>\n(I like to use <code>%_%<\/code> as my scratch environment variable.)\n<\/p>\n<p>\nType <code>SET \/?<\/code> to see the other pseudo-variables provided by the\ncommand processor.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes people go to great lengths to get information which is available in a much simpler way. We saw it a few days ago when we found a 200+-line C# program that could be replaced with a 90-byte batch file. Here&#8217;s another example of a rather roundabout way of capturing the current directory from a [&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":[104],"class_list":["post-36573","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-tipssupport"],"acf":[],"blog_post_summary":"<p>Sometimes people go to great lengths to get information which is available in a much simpler way. We saw it a few days ago when we found a 200+-line C# program that could be replaced with a 90-byte batch file. Here&#8217;s another example of a rather roundabout way of capturing the current directory from a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36573","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=36573"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/36573\/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=36573"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=36573"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=36573"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}