I am not much experienced working in PowerShell.
I have been trying to create a script which automates the archive process from one location to other.
I have a Source location as $source = \server\share\folder\
and a destination as $dest = \server\share\
the source location has many subfolders, so I need to have the same hierarchy of folders in the dest location.
I have tried robocopy $Source $Destination and Copy-Item $Source $Destination -Filter {PSIsContainer} -Recurse -Force for the folder structure, which creates the exact folders in the dest as that of source.
But now when I use compress-archive, it should compress the files from the specific folder from $source and archive in the same specific named folder with YYYYMM.zip which i couldnt establish :(
$from = ''\server\share\folder\"
$to = '\server\share\'
$year = 2019
$Paths = Get-ChildItem -Path $from -Recurse -Directory -Force -ErrorAction SilentlyContinue | Select-Object FullName | ForEach-Object { $_.FullName }
Foreach ($path in $Paths){
$destpath = $path.replace($from, $to)
$destpath = "$to\$year\$($.parent)\$($.BaseName)"
If(!(test-path $destpath))
{
New-Item -ItemType Directory -Force -Path $destpath
}
Get-ChildItem $path -Recurse -File -Include *.pdf | ?{$_.LastWriteTime.Year -$year} | Compress-Archive -destination $destpath\2019.zip
Get-ChildItem $to -Recurse -Force -Directory | Sort-Object -Property FullName -Descending | Where-Object { $($_ | Get-ChildItem -Force | Select-Object -First 1).Count -eq 0 } | Remove-Item -Verbose
}
This is helping me to create the folder structure same as that of source but archives with the name 2019.zip :(
At the destination I thought of having a year created at the beginning and then the folder structure. So that it will be clear to analyze the data year wise.
$PDFDirectory ='\server\share\folder\'
$ArchiveLocation = '\server\share\folder\'
$year = 2019
$paths = Get-ChildItem -Path $PDFDirectory -Directory -Recurse -Force -ErrorAction SilentlyContinue | Select-Object FullName | ForEach-Object { $_.FullName}
foreach($path in $paths){
Get-ChildItem $path -Directory | ForEach-Object{
$destination = "$ArchiveLocation\$year\$($.parent)\$($.BaseName)"
if(-not (Test-Path -Path $destination))
{
New-Item -Path $destination -ItemType Directory
}
Get-ChildItem -Path $_.FullName -File *.pdf -Recurse | Where-Object {$_.LastWriteTime.Year -eq $year} | ForEach-Object{
Compress-Archive -Path $_.FullName -DestinationPath $destination\$year$(($_.LastWriteTime.Month).ToString('D2')).zip -CompressionLevel Optimal -Update
#Remove-Item -Path $_.FullName
}
}
}
This helps me to create a beginning year folder as "2019", than the folder structure.
But the problem here is because of using recurse, the subfolders from 1 folder Eg: \server\share\folder1\folder2\folder3 is being created at the folder1 level at the dest Eg: \server\share\2019\folder1
\server\share\2019\folder3