Compress and archive PDF files from a particular year to the destaination share with a folder format YYMM

sanketa sosatti 41 Reputation points
2021-03-30T07:34:02.217+00:00

Team,

i am not a scripting girl, but in need of your help.

we have a share \usslcstor01\usslcnts under which
\APAC
\Australia
\Cambodia
\Hong Kong ........etc
\NAM
\Canada
\Trinidad
\CostaRica ... etc
\CAM
\SAM

under each country PDFs are generated.
i wanted to compress and move these 2019 year pdfs to an different share and delete after moved.

i did it manually last year for 2018 files before which the folders are created.

(Get-ChildItem -Path "\usslcstor01\usslcnts\APAC\Australia" *.pdf| Where-Object {$.LastWriteTime.Month -eq 01 -and $.LastWriteTime.Year -eq 2018 } | Compress-Archive -DestinationPath " \usslcstor01\usslcnts_archive\2018\APAC\Australia\201801.zip" -CompressionLevel Optimal)
(Get-ChildItem -Path "\usslcstor01\usslcnts\APAC\Australia" *.pdf| Where-Object {$.LastWriteTime.Month -eq 02 -and $.LastWriteTime.Year -eq 2018 } | Compress-Archive -DestinationPath " \usslcstor01\usslcnts_archive\2018\APAC\Australia\201802.zip" -CompressionLevel Optimal)

Now wanted to compress and move with a folder YYMM.

i was trying out with the below info, i know the commands but unable to form a script.

$PDFDirectory = "\usws1t1optio02\APAC\R12_Testing\PRS\US_WCS\CirrusCarson"
$ArchiveLocation = "\usws1t1optio02\Archive_Test\Sanketa"
$Date = Get-Date -UFormat "%Y%m"
$Year = "2019"
$month = 01
$month = $month + 1

Get-ChildItem -Path 'E:\APAC\R12_Testing\PRS\' -Recurse -File '*.pdf'

New-Item -ItemType directory -Path $Year$month

Compress-Archive -DestinationPath "\usws1t1optio02\Archive_Test\Sanketa\201801.zip" -CompressionLevel Optimal

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,353 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,476 Reputation points Microsoft Vendor
    2021-03-30T10:25:57.247+00:00

    Hi,

    Please see if this works for you

    $PDFDirectory  ='\\usslcstor01\usslcnts'  
    $ArchiveLocation  = '\\usslcstor01\usslcnts_archive'  
    $year = 2019  
    Get-ChildItem -Path $PDFDirectory | ForEach-Object{  
        Get-ChildItem -Path $_.FullName | ForEach-Object{  
            $destination = "$ArchiveLocation\$year\$($_.parent)\$($_.BaseName)"  
            if(-not (Test-Path -Path $destination)){ New-Item -Path $destination -ItemType Directory | Out-Null }  
            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  
            }  
        }   
    }  
    

    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.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful