1

I have exe of cwebp that basically can be run like

cwebp.exe image1.jpg -o image1.webp

I'm trying to create simple powershell script to run the exe for all image inside a folder using powershell script, I do have create some script like this

param ( [string]$FolderName)
Get-ChildItem $FolderName | ForEach-Object {
    &"D:\Downloads\libwebp\bin\cwebp.exe" $_.FullName -o $_.BaseName + ".webp"
}

so I can run the ps1 file with the folder name like

batch-script.ps1 ".\images"

but when I run it, got error

SHCreateStreamOnFile((const LPTSTR)filename, STGM_READ, stream) failed 80070002
Error opening input file .webp (80070002)
OpenInputStream(filename, &stream) failed 80070002
cannot open input file '.webp'
Error! Could not process file .webp
Error! Cannot read input picture file '.webp'

the folder content just have jpg image name image1.jpg, image2.jpg, did I miss something?

1
  • 1
    You should simply output the created command line you create inside your loop to check what's wrong. Commented Feb 14, 2020 at 15:23

1 Answer 1

1

Try something like this:

param ( [string]$FolderName)

$images = Get-ChildItem $FolderName

foreach ($img in $images) {
  $outputName = $img.DirectoryName + "\" + $img.BaseName + ".webp"
  &"D:\Downloads\libwebp\bin\cwebp.exe" $img.FullName -o $outputName
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this is correct when I test it, looks like I need to create the output file first and my code also missing the DirectoryName that make the converted image is placed in the same folder as the ps1 file.

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.