0

I have one folder with multiple pdf files named like xxx_xxx_01.05.2017.pdf. Each day I have one more file and I start this script to merge them and save D:\123\DB052017.pdf.

The goal is to auto create a new file after merging all the files of the month.

The next files are for the new month like xxx_xxx_01.06.2017.pdf and I want to create a new file with name DB062017.pdf and so on...

Can you give me a clue?

Add-Type -Path C:\assemblies\PdfSharp.dll                        

Function Merge-PDF {            
    Param($path, $filename)                        

    $output = New-Object PdfSharp.Pdf.PdfDocument            
    $PdfReader = [PdfSharp.Pdf.IO.PdfReader]            
    $PdfDocumentOpenMode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode]                        

    foreach($i in (gci $path *.pdf -Recurse)) {            
        $input = New-Object PdfSharp.Pdf.PdfDocument            
        $input = $PdfReader::Open($i.fullname, $PdfDocumentOpenMode::Import)            
        $input.Pages | %{$output.AddPage($_)}            
    }                        

    $output.Save($filename)            
}

Merge-PDF -path c:\fso -filename D:\123\DB052017.pdf

This is what i done for the end file:

Add-Type -Path C:\assemblies\PdfSharp.dll                        
$MonthName =  (Get-Date).Month 
$YearName =   (Get-Date).Year      
Function Merge-PDF {            
    Param($path, $filename)                        

    $output = New-Object PdfSharp.Pdf.PdfDocument            
    $PdfReader = [PdfSharp.Pdf.IO.PdfReader]            
    $PdfDocumentOpenMode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode]                        

    foreach($i in (gci $path *.pdf -Recurse)) {            
        $input = New-Object PdfSharp.Pdf.PdfDocument            
        $input = $PdfReader::Open($i.fullname, $PdfDocumentOpenMode::Import)            
        $input.Pages | %{$output.AddPage($_)}            
    }                        

    $output.Save($filename)            
}
Merge-PDF -path c:\fso -filename D:\Users\h.yordanov\Desktop\WEBDams\Data\CHQ\HQ\DB$MonthName""$YearName.pdf

Now i need to fetch or move the files that are in folder with different month.

Hristian
  • 23
  • 9
  • What is your question? – Martin Brandl May 17 '17 at 08:02
  • The question is how to auto create a new file after merging all the files of the month. The next files are "xxx_xxx_01.06.2017.pdf" and need to create new file with name "DB062017.pdf" and so on... – Hristian May 17 '17 at 08:05
  • [Update your Get-ChildItem](http://stackoverflow.com/questions/15884173/limiting-powershell-get-childitem-by-file-creation-date-range) so that it only gets one months worth of files by creation-date: – henrycarteruk May 17 '17 at 08:09
  • Thanks for edits :) I looked at the example you post. But after i hardcode one month, how can i get auto fetch the new files for the next month and merge them with new name. – Hristian May 17 '17 at 08:18
  • Don't hardcode it, pass in as a param like `$path` and `$filename` are. – henrycarteruk May 17 '17 at 08:26
  • Can you give me example in code? – Hristian May 17 '17 at 08:43
  • Those two are used in your existing code already. – henrycarteruk May 17 '17 at 09:23
  • I cant understand what can accomplish with this. I think about to get the part of the named file and compare it with the month and then i will add it to the new file. Something like this. – Hristian May 17 '17 at 10:19

1 Answers1

0

I think i did it. In every new month new pdf is created and When the the month is finished all files are moved. :)

Add-Type -Path C:\assemblies\PdfSharp.dll                        
$MYName =  Get-Date -format y  
$movepdfs = "C:\fso\*.pdf"
$a = (Get-ChildItem -path c:\fso).Name
$b = $a -replace '[^0-9]', ''
$c = $b.Substring(2,2)
$e= Get-Date -format MM

If ($c -lt $e) {
   Move-Item $movepdfs c:\oldpdfs -WhatIf 
    }


Function Merge-PDF {            
    Param($path, $filename)                        

    $output = New-Object PdfSharp.Pdf.PdfDocument            
    $PdfReader = [PdfSharp.Pdf.IO.PdfReader]            
    $PdfDocumentOpenMode = [PdfSharp.Pdf.IO.PdfDocumentOpenMode]                        

    foreach($i in (gci $path *.pdf -Recurse)) {            
        $input = New-Object PdfSharp.Pdf.PdfDocument            
        $input = $PdfReader::Open($i.fullname, $PdfDocumentOpenMode::Import)            
        $input.Pages | %{$output.AddPage($_)}            
    }                        

    $output.Save($filename)            
}

Merge-PDF -path c:\fso -filename C:\WEB\Apache24\htdocs\WEBDams\Data\CHQ\HQ\1\$MYName.pdf
Hristian
  • 23
  • 9