1

I'm looking to create an array of files (pdf's specifically) based on filenames in Powershell. All files are in the same directory. I've spent a couple of days looking and can't find anything that has examples of this or something that is close but could be changed. Here is my example of file names:

AR - HELLO.pdf
AF - HELLO.pdf
RT - HELLO.pdf
MH - HELLO.pdf
AR - WORLD.pdf
AF - WORLD.pdf
RT - WORLD.pdf
HT - WORLD.pdf
....

I would like to combine all files ending in 'HELLO' into an array and 'WORLD' into another array and so on.

I'm stuck pretty early on in the process as I'm brand new to creating scripts, but here is my sad start:

Get-ChildItem *.pdf
 Where BaseName -match '(.*) - (\w+)'


Updated Info... I do not know the name after the " - " so using regex is working.
My ultimate goal is to combine PDF's based on the matching text after the " - " in the filename and the most basic code for this is:

$file1 = "1 - HELLO.pdf"
$file2 = "2 - HELLO.PDF"
$mergedfile = "HELLO.PDF"

Merge-PDF -InputFile $file1, $file2 -OututFile $mergedfile

I have also gotten the Merge-PDF to work using this code which merges all PDF's in the directory:

$Files = Get-ChildItem *.pdf
$mergedfiles = "merged.pdf"
Merge-PDF -InputFile $Files -OutputFile $mergedfiles

Using this code from @Mathias the $suffix portion of the -OutputFile works but the -InputFile portion is returning an error "Exception calling "Close" with "0" argument(s)"

$groups = Get-ChildItem *.pdf |Group-Object {$_.BaseName -replace 
'^.*\b(\w+)$','$1'} -AsHashTable

foreach($suffix in $groups.Keys) {Merge-PDF -InputFile $(@($groups[$suffix])) 
-OutputFile "$suffix.pdf"}

For the -InputFile I've tried a lot of different varieties and I keep getting the "0" arguments error. The values in the Hashtable seem to be correct so I'm not sure why this isn't working.

Thanks

2 Answers 2

2

This should do the trick:

$HELLO = Get-ChildItem *HELLO.pdf |Select -Expand Name
$WORLD = Get-ChildItem *WORLD.pdf |Select -Expand Name

If you want to group file names by the last word in the base name and you don't know them up front, regex is indeed an option:

$groups = Get-ChildItem *.pdf |Group-Object {$_.BaseName -replace '^.*\b(\w+)$','$1'} -AsHashTable

And then you can do:

$groups['HELLO'].Name

for all the file names ending with the word HELLO, or, to iterate over all of them:

foreach($suffixGroup in $groups.GetEnumerator()){
  Write-Host "There are $($suffixGroup.Value.Count) files ending in $($suffixGroup.Key)"
}
Sign up to request clarification or add additional context in comments.

5 Comments

Hello, thanks for the response. When I run foreach($suffix in $groups.Keys){ Write-Host "There are $($groups[$suffix].Count) files ending in $suffix" }. The output says that there are '0 files ending in$suffix' for each group. When I enter $groups it appears to have the correct name based on the code and a list of files to the right. Is there another way to handle the $($groups[$suffix].Count? Thanks
@Makawide yeah, this'll happen in Windows PowerShell if theres only 1 file per group, I've updated the answer to take that into account now :)
Hi, so with the updated code and no matter how many files it says there is 1 file. I also updated my question above with more info that may help.. thanks again
@Makawide curious. I've updated the answer with a method that definitely work :)
With the updated code the number of files is correct but the output is displayed as "there are x files ending in System.Collections.DictionaryEntry." I also noticed that when I try to call the data by the key name, nothing is displayed, so when I type $groups['HELLO'], no data is listed. I realized the data should be listed after building a simple hash table and seeing the output.
0

Another option is to get all items with Get-ChildItem and use Where-Object to filter.

$fileNames = Get-ChildItem | Select-Object -ExpandProperty FullName 
#then filter
$fileNames | Where-Object {$_.EndsWith("HELLO.PDF")}

#or use the aliases if you want to do less typing:
$fileNames = gci | select -exp FullName
$fileNames | ? {$_.EndsWith("HELLO.PDF")}

Just wanted to show more options -especially the Where-Object cmdlet which comes in useful when you're calling cmdlets that don't have parameters to filter.

Side note:

You may be asking what -ExpandProperty does.

If you just call gci | select -exp FullName, you will get back an array of PSCustomObjects (each of them with one property called FullName).

This can be confusing for people who don't really see that the objects are typed as it is not visible just by looking at the PowerShell script.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.