{"id":96745,"date":"2017-08-03T07:00:00","date_gmt":"2017-08-03T21:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/?p=96745"},"modified":"2019-03-13T01:14:49","modified_gmt":"2019-03-13T08:14:49","slug":"20170803-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20170803-00\/?p=96745","title":{"rendered":"Decomposing file paths (and extracting other information like file size, date and time, and attributes) from a batch file"},"content":{"rendered":"<p>We saw something very similar to this <a HREF=\"http:\/\/blogs.msdn.com\/b\/oldnewthing\/archive\/2012\/08\/02\/10334559.aspx\">during the first Batch File Week<\/a>. You have a file name in an environment variable, and you want to decompose its path (say, extract the drive letter or get the file base name without any path information or extension). Or you want to get the file&#8217;s size or date or attributes. <\/p>\n<p>The technique we saw in the article was to call a subroutine with the file name, and have the subroutine use the <a HREF=\"http:\/\/www.microsoft.com\/resources\/documentation\/windows\/xp\/all\/proddocs\/en-us\/percent.mspx\">tilde operators<\/a> to extract information from the inbound parameter. <\/p>\n<p>You can also do this inline (without needing a subroutine) by abusing the <code>FOR<\/code> command. Tilde operators work on <code>FOR<\/code> loop variables, so you just need to set up a <code>FOR<\/code> loop that doesn&#8217;t actually loop! <\/p>\n<pre>\nset FILENAME=C:\\Program Files\\desktop.ini\nfor %%i in (\"%FILENAME%\") do set SIZE=%%~zi\n<\/pre>\n<p>Ta-da, you put the file size in the <code>SIZE<\/code> variable. <\/p>\n<p>Of course, you could do actual math, too. Or use the other tilde operators to extract other information. Go nuts. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>FOR to the rescue.<\/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-96745","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>FOR to the rescue.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96745","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=96745"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/96745\/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=96745"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=96745"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=96745"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}