question

sanketasosatti-5722 avatar image
0 Votes"
sanketasosatti-5722 asked sanketasosatti-5722 action

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

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

IanXue-MSFT avatar image
1 Vote"
IanXue-MSFT answered sanketasosatti-5722 commented

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

83717-screenshot-2021-04-01-224645.png





Hello IanXue-MSFT,

please see the attached, along with the compression deletion and creating the folders same as of source location, the script is creating some extra folders and those are empty at the destination.

0 Votes 0 ·