{"id":880,"date":"2013-03-28T11:00:25","date_gmt":"2013-03-28T11:00:25","guid":{"rendered":"http:\/\/www.gennard.net\/blog\/?p=880"},"modified":"2013-03-28T11:00:25","modified_gmt":"2013-03-28T11:00:25","slug":"window_touch_command","status":"publish","type":"post","link":"http:\/\/www.gennard.net\/blog\/2013\/03\/window_touch_command\/","title":{"rendered":"How to avoid using Unix touch command on Windows&#8230;"},"content":{"rendered":"<p>In my company we have a strange habit of using third party tools to do the most trivial things rather than using the native features.<\/p>\n<p>A recent example of this is the use of the &#8220;Unix&#8221; touch command on Windows. It turns out it is quite simple to avoid having to install cygwin or an-other Unix command tools and just use a simple batch file or using the appropriate tasks in MsBuild or Ant.<\/p>\n<p>The batch file solution, is not a perfect solution as I suspect if the file is very large the &#8220;copy to self&#8221; could take a while but for general developer use it works just fine. <\/p>\n<p>So my touch.bat is simply&#8230;<\/p>\n<p><strong>Windows touch.bat<\/strong><\/p>\n<pre lang=\"batch\">@echo off\r\nfor %%i in (%*) do (\r\n  if exist %%i (\r\n    echo Updating timestramp for file : %%i\r\n    copy \/b \"%%i\" +,,\r\n  )\r\n\r\n  if not exist %%i (\r\n    echo Creating zero length file : %%i\r\n    copy nul %%i\r\n  )\r\n)<\/pre>\n<p>If you are using msbuild, then it has a &#8220;<a title=\"Touch Task\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/37fwbyt5.aspx\" target=\"_blank\">Touch task<\/a>&#8221; and similarly if you are using Ant it too has a <a title=\"Touch Task\" href=\"http:\/\/ant.apache.org\/manual\/Tasks\/touch.html\" target=\"_blank\">Touch Task <\/a><\/p>\n<p>Lastly if you are using powershell it can be done with a simple function:<\/p>\n<pre lang=\"powershell\">function touch\r\n{\r\n  $file = $args[0]\r\n  if($file -eq $null) {\r\n    throw \"No filename supplied\"\r\n  }\r\n\r\n  if(Test-Path $file)\r\n  {\r\n    (Get-ChildItem $file).LastWriteTime = Get-Date\r\n  }\r\n  else\r\n  {\r\n    echo $null &gt; $file\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In my company we have a strange habit of using third party tools to do the most trivial things rather than using the native features. A recent example of this is the use of the &#8220;Unix&#8221; touch command on Windows. &hellip; <a href=\"http:\/\/www.gennard.net\/blog\/2013\/03\/window_touch_command\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,19,32,207],"tags":[],"_links":{"self":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/880"}],"collection":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/comments?post=880"}],"version-history":[{"count":19,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/880\/revisions"}],"predecessor-version":[{"id":900,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/posts\/880\/revisions\/900"}],"wp:attachment":[{"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/media?parent=880"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/categories?post=880"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gennard.net\/blog\/wp-json\/wp\/v2\/tags?post=880"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}