0

I'm trying to batch-convert heic images to png images using Powershell. What I have tried:

Get-ChildItem -Include ('*.HEIC', '*.heic') -File | & .\bin\vips.exe copy $_.Name "$(_.BaseName).png"
Pause

and

Get-ChildItem -Include ('*.HEIC', '*.heic') -File | & .\bin\vips.exe copy $_.Name ($_.BaseName + '.png')
Pause

Both times I'm getting an error VipsForeignLoad: file ".png" does not exist which tells me it treats ".png" as the first (and only) argument and ignores the object Name and Basename properties.

9
  • 1
    Piggy backing off @RetiredGeek's answer, try throwing it in a foreach loop: Get-ChildItem -Filter "*.HEIC" -File | Foreach-Object { & .\bin\vips.exe copy $_.Name "$($_.BaseName).png" } Commented May 17, 2021 at 1:45
  • 1
    ForEach did it! Commented May 17, 2021 at 4:26
  • 1
    Also, if you're looking for it to be faster, avoid the pipeline using the grouping operator (...) with the .ForEach() method. Commented May 17, 2021 at 5:03
  • 1
    Well foreach(){} is what I meant by a ForEach statement. That one is supposedly the fastest out of the three (with the .foreach method being close 2nd). Commented May 21, 2021 at 14:34
  • 1
    Big fan of Mr Lee, gonna disagree with the young fellow tho. Commented May 21, 2021 at 15:01

1 Answer 1

1

You're missing a $. "$($_.BaseName).png" I would use -Filter vs -Include as it is more efficient.

Edited: Try this approach bypassing the Pipe and see if you get a different result. I've also added some additional code to insure everything is fully evaluated. If this approach works you can experiment with reducing come of the $() evaluation levels.

Also are you using Linux? Some of my googling led me to believe you might be. If so you should specify this in your tags for clarity.

Clear-Host
$x = Get-ChildItem -Filter "*.HEIC" -File
ForEach ($File in $x) {
  & .\bin\vips.exe copy "$($File.FullName)" $("$($File.BaseName)" + ".pdf")
}

Also note the file extension in the Filter is case insensitive so no need to repeat.

I'd also recommend adding the -Path parameter for clarity rather than assuming the default directory but that's just me.

HTH

Sign up to request clarification or add additional context in comments.

3 Comments

Still getting the same error. & .\bin\vips.exe copy test.HEIC test.png goes through without issues. Thanks for the filter suggestion.
Give the updated answer a go. Please post back your console session if it doesn't work.
ForEach did it (well, I ended up using ForEach-Object, but that's not crucial)! Didn't even have to do any of the insuring. Also thanks for the -Path suggestion. I'm trying to learn to do these things the proper way, so little pieces of advice like that (or the Filter part) are really helpful.

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.