Create a script which automates the archive process from one location to other with the folder hierarchy

sanketa sosatti 41 Reputation points
2021-06-05T18:52:42.787+00:00

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

Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,530 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue 37,106 Reputation points Microsoft Vendor
    2021-07-23T09:30:48.367+00:00

    Hi,

    Please see if this works better.

    $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){  
        $destination = $path.replace("$PDFDirectory","$ArchiveLocation\$year\")  
        if(-not (Test-Path -Path $destination))  
        {   
            New-Item -Path $destination -ItemType Directory  
        }  
        Get-ChildItem -Path $path -File *.pdf | ForEach-Object{  
            Compress-Archive -Path $_.FullName -DestinationPath $destination\$year$(($_.LastWriteTime.Month).ToString('D2')).zip -CompressionLevel Optimal -Update  
            #Remove-Item -Path $_.FullName  
        }  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Ian Xue 37,106 Reputation points Microsoft Vendor
    2021-06-07T12:47:39.407+00:00

    Hi,

    Please check to see if this works

    $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 = $_.fullname.replace("$PDFDirectory","$ArchiveLocation\$year\")  
            if(-not (Test-Path -Path $destination))  
            {   
                New-Item -Path $destination -ItemType Directory  
            }        
            Get-ChildItem -Path $_.FullName -File *.txt | ForEach-Object{  
            Compress-Archive -Path $_.FullName -DestinationPath $destination\$year$(($_.LastWriteTime.Month).ToString('D2')).zip -CompressionLevel Optimal -Update  
            #Remove-Item -Path $_.FullName  
            }  
        }  
    }  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


  2. sanketa sosatti 41 Reputation points
    2021-07-21T06:02:33.007+00:00

    foreach($path in $paths){
    Get-ChildItem $path -Directory| ForEach-Object{

    If the root folder doesn't have any directories , it skips and which has directories - for each directory the folder struct is created..

    I have tried removing directories and the chiditem parses all the folders, files from the $PDFDirectory and it creates the folders for all files too.

    is there a way to get pipe the folders which doesn't have subdirectories?

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.