{"id":6993,"date":"2012-08-01T07:00:00","date_gmt":"2012-08-01T07:00:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/oldnewthing\/2012\/08\/01\/how-do-i-find-the-most-recently-created-file-in-a-directory-from-a-batch-file\/"},"modified":"2012-08-01T07:00:00","modified_gmt":"2012-08-01T07:00:00","slug":"how-do-i-find-the-most-recently-created-file-in-a-directory-from-a-batch-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20120801-00\/?p=6993","title":{"rendered":"How do I find the most recently created file in a directory from a batch file?"},"content":{"rendered":"<p>\nWe&#8217;ve reached Hump Day of Batch File Week.\nRemember, nobody actually likes batch programming.\nYou merely tolerate it.\n<\/p>\n<p>\nToday, we&#8217;ll find the most recently-created item in a directory.\n(For example, we have a server that holds our daily builds,\nand you might want to write a batch file that automatically\ninstalls the latest build.)\n<\/p>\n<p>\nThere may be better ways, but what I do is ask for a list\nsorted oldest-to-newest,\nand then choose the last one.\n<\/p>\n<pre>\nfor \/f %%i in ('dir \/b\/a-d\/od\/t:c') do set LAST=%%i\necho The most recently created file is %LAST%\n<\/pre>\n<p>\nThis trick works by asking the <code>dir<\/code> command\nto list just the names (<code>\/b<\/code>)\nof just the files <code>\/a-d<\/code>,\nsorted by date (<code>\/od<\/code>),\nbased on the creation time (<code>\/t:c<\/code>).\n<\/p>\n<p>\nEach time a new file is reported,\nits name is stored in the <code>LAST<\/code> variable,\noverwriting the previous one.\nWhen the loop finishes,\nthe <code>LAST<\/code> variable contains the name of the\nnewest file,\nsince that&#8217;s the one that didn&#8217;t get overwritten.\n<\/p>\n<p>\nYou can tweak the command line to perform other queries.\nFor example, if you want the newest file, then just ask\nfor a reverse sort (<code>\/o-d<\/code>).\nIf you want the file sorted by modified time rather than\ncreation time,\nthen use <code>\/t:w<\/code>.\nYou get the idea.\n<\/p>\n<p>\n<b>Limitations<\/b>:\nThe implementation above\nassumes that no files contain spaces in their name.\nRemoving this limitation is left as an exercise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve reached Hump Day of Batch File Week. Remember, nobody actually likes batch programming. You merely tolerate it. Today, we&#8217;ll find the most recently-created item in a directory. (For example, we have a server that holds our daily builds, and you might want to write a batch file that automatically installs the latest build.) There [&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-6993","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>We&#8217;ve reached Hump Day of Batch File Week. Remember, nobody actually likes batch programming. You merely tolerate it. Today, we&#8217;ll find the most recently-created item in a directory. (For example, we have a server that holds our daily builds, and you might want to write a batch file that automatically installs the latest build.) There [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6993","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=6993"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/6993\/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=6993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=6993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=6993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}