{"id":932,"date":"2014-10-27T06:15:09","date_gmt":"2014-10-27T06:15:09","guid":{"rendered":"https:\/\/poiseddevelopers.com\/reality-tech\/?p=932"},"modified":"2024-04-26T12:32:29","modified_gmt":"2024-04-26T12:32:29","slug":"copying-folder-hierarchy-with-date-filter-and-elimination-of-empty-folders","status":"publish","type":"post","link":"https:\/\/poiseddevelopers.com\/reality-tech\/copying-folder-hierarchy-with-date-filter-and-elimination-of-empty-folders\/","title":{"rendered":"Copying folder hierarchy with date filter and elimination of empty folders"},"content":{"rendered":"<p>Ever need to copy a tree of folders? I had to do this only for files older than a specified date. Plus empty folders were not welcomed. How to go about this?<\/p>\n<p>We\u2019ll approach this in four easy steps. First we\u2019ll set the source, destination and threshold date, followed by recreating the empty folder structure on the target:<\/p>\n<pre lang=\"php\">$sourceLocation = \"\\ny-srv-fs3Reserve\"\r\n$DestLocation = \"D:plautjACTTMP\"\r\n$thresh = get-date \"December 31, 2006\"\r\nxcopy $sourceLocation $DestLocation \/T\r\n<\/pre>\n<p>Next we\u2019ll get the full set of files and folders:<\/p>\n<pre lang=\"php\">$q = Get-ChildItem $sourceLocation -Recurse\r\n<\/pre>\n<p>We will now copy all the files older than the threshold:<\/p>\n<pre lang=\"php\">foreach ($qItem in $q)\r\n{\r\n    if (!$qitem.PSiscontainer)\r\n    {\r\n        if ($qItem.LastWriteTime -lt $thresh)\r\n        {\r\n            $DestItemLoc = $qitem.FullName.replace($sourceLocation,$DestLocation)\r\n            copy-item $qitem.FullName  $DestItemLoc\r\n        }\r\n    }\r\n}\r\n<\/pre>\n<p>Lastly, let\u2019s delete empty folders. The kep is specifying the AllDirectories search option, otherwise it will delete folders that are devoid of immediate files, but which have files in subfolders:<\/p>\n<pre lang=\"php\">\r\n$a = Get-ChildItem $DestLocation -recurse | Where-Object {$_.PSIsContainer -eq $True}\r\n$a | Where-Object {$_.getfiles(&quot;*&quot;,[System.IO.SearchOption]::AllDirectories).Count -lt 1} | Select-Object FullName | ForEach-Object {remove-item $_.fullname -recurse}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Ever need to copy a tree of folders? I had to do this only for files older than a specified date. Plus empty folders were not welcomed. How to go about this? We\u2019ll approach this in four easy steps. First we\u2019ll set the source, destination and threshold date, followed by recreating the empty folder structure [&hellip;]<\/p>\n","protected":false},"author":6,"featured_media":936,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[26],"tags":[],"class_list":["post-932","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"_links":{"self":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/932","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/comments?post=932"}],"version-history":[{"count":11,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/932\/revisions"}],"predecessor-version":[{"id":958,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/posts\/932\/revisions\/958"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media\/936"}],"wp:attachment":[{"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/media?parent=932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/categories?post=932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/poiseddevelopers.com\/reality-tech\/wp-json\/wp\/v2\/tags?post=932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}