{"id":95,"date":"2008-10-08T09:41:24","date_gmt":"2008-10-08T13:41:24","guid":{"rendered":"http:\/\/www.dev-notes.com\/blog\/2008\/10\/08\/format-windows-command-line-date\/"},"modified":"2008-10-08T09:41:24","modified_gmt":"2008-10-08T13:41:24","slug":"format-windows-command-line-date","status":"publish","type":"post","link":"https:\/\/www.dev-notes.com\/blog\/2008\/10\/08\/format-windows-command-line-date\/","title":{"rendered":"Format Windows command line date"},"content":{"rendered":"<p>The need for me to format date in the Windows command line environment came up when I needed to write a batch file to automatically copy the latest copies of certain files in a large folder.  The files are named in the format of ???YYYYMMDD.txt, which presented itself as one easy way for me to query for latest files.<\/p>\n<p>Built in to Windows command line is the %date% variable, which displays the system date as follows based on my regional setting.<\/p>\n<pre class=\"code\">Microsoft Windows XP [Version 5.1.2600]\n(C) Copyright 1985-2001 Microsoft Corp.\n\nC:>echo %date%\nWed 10\/08\/2008<\/pre>\n<p>Please note that your regional settings might be different.  For example, for those in Australia, the date might be presented in a DD\/MM\/YYYY format rather than the typical US MM\/DD\/YYYY format.  If this is the case, you may wish to adjust the code below accordingly.<\/p>\n<p>To suit my needs, I need to format this date to YYYYMMDD.  Knowing that %date%&#8217;s format is consistent in format, we can just parse it as a string.<\/p>\n<pre class=\"code\">C:>echo %date:~10,4%\n2008\n\nC:>echo %date:~4,2%\n10\n\nC:>echo %date:~7,2%\n08<\/pre>\n<p>Bringing it together, I will have the YYYYMMDD format I am looking for.<\/p>\n<pre class=\"code\">C:>echo %date:~10,4%%date:~4,2%%date:~7,2%\n20081008<\/pre>\n<p>Once again, you may need to adjust the above command based on your own regional settings.<\/p>\n<p>Here is how I put this code in action in the form of a batch file that copied only the files with today&#8217;s date.<\/p>\n<pre class=\"code\">set backupcmd=c:windowssystem32xcopy.exe \/c \/d \/e \/h \/r \/y\nset dt=%date:~10,4%%date:~4,2%%date:~7,2%\n%backupcmd% C:stuff*%dt%.txt D:backupstuff<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>This note briefly describes how to format the date in the Windows command line environment.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[26],"tags":[],"class_list":["post-95","post","type-post","status-publish","format-standard","hentry","category-windows"],"_links":{"self":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/95","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/comments?post=95"}],"version-history":[{"count":0,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/posts\/95\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/media?parent=95"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/categories?post=95"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dev-notes.com\/blog\/wp-json\/wp\/v2\/tags?post=95"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}