2

I have a PS script which Zips up the previous months logs and names the zip file FILENAME-YYYY-MM.zip

This works

What I now want to do is copy these zip files off to a network share but keeping some of the folder structure. I currently a folder structure similar to the following;

C:\Folder1\
C:\Folder1\Folder2\
C:\Folder1\Folder3\
C:\Folder1\Folder4\Folder5\

There are .zip files in every folder below c:\Folder1 What I want is for the script to copy files from c:\folder1 to \\networkshare but keeping the folder structure, so I should have 3 folders and another subfolder in folder4.

Currently I can only get it to copy the whole structure so I get c:\folder1\... in my \\networkshare

I keep running into issues such as the new folder structure doesn't exist, I can't use the -recurse switch within the Get-ChildItem command etc...

The script I have so far is;

#This returns the date and formats it for you set value after AddMonths to set archive date -1 = last month
$LastWriteMonth = (Get-Date).AddMonths(-3).ToString('MM')
#Set destination for Zip Files
$DestinationLoc = "\\networkshare\LogArchive\$env:computername"

#Source files
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth} 
Copy-Item $SourceFiles -Destination $DestinationLoc\ZipFiles\
Remove-Item $SourceFiles
Loofer
  • 6,841
  • 9
  • 61
  • 102
Wiggum123
  • 45
  • 2
  • 6
  • 3
    Similar questions I've seen here have been answered by "Don't reinvent the wheel, just use robocopy," so that may be something to consider. – Tony Hinkle Jun 01 '15 at 12:51
  • Yeah I think robocopy can resolve your problem. Look this question if help you: http://stackoverflow.com/questions/21606259/robocopy-copy-files-preserving-folder-structure-but-adding-a-subfolder – wallybh Jun 01 '15 at 12:52
  • @TonyHinkle Using robocopy can produce problems if the robocopy log is to be parsed - I've got troubles with file encoding with that one, also robocopy puts ^H symbols if logged via `> file.log`. And, why not to program one's own solution on Powershell instead of relying on a third party software? – Vesper Jun 01 '15 at 13:35

2 Answers2

3

Sometimes, you just can't (easily) use a "pure PowerShell" solution. This is one of those times, and that's OK.

Robocopy will mirror directory structures, including any empty directories, and select your files (likely faster than a filter with get-childitem will). You can copy anything older than 90 days (about 3 months) like this:

robocopy C:\SourceFiles "\\networkshare\LogArchive\$($env:computername)\ZipFiles" /E /IS /MINAGE:90 *.zip

You can specify an actual date with /MINAGE too, if you have to be that precise.

Wiggum123
  • 45
  • 2
  • 6
alroc
  • 27,574
  • 6
  • 51
  • 97
  • Modified this a bit but thanks a bunch! `$LastWriteMonth = (Get-Date).AddMonths(-3).ToString('yyyyMMdd') $DestinationLoc = "\\networkshare\LogArchive\$env:computername\" Robocopy C:\sourcefiles\ $DestinationLoc -IS -E -MINAGE:$LastWriteMonth *.zip` – Wiggum123 Jun 01 '15 at 14:29
1

How about Copy-Item "C:\SourceFiles\" -dest $DestinationLoc\ZipFiles -container -recurse? I have tested this and have found that it copies the folder structure intact. If you only need *.zip files, you first get them, then for each you call Resolve-Path with -Relative flag set and then add the resultant path into Destination parameter.

$oldloc=get-location
Set-Location "C:\SourceFiles\" # required for relative
$SourceFiles = Get-ChildItem C:\Sourcefiles\*.zip -Recurse | where-object {$_.lastwritetime.month -le $LastWriteMonth}
$SourceFiles | % { 
    $p=Resolve-Path $_.fullname -relative
    copy-item $_ -destination "$DestinationLoc\ZipFiles\$p"
}
set-location $oldloc # return back
Community
  • 1
  • 1
Vesper
  • 18,599
  • 6
  • 39
  • 61