1

I am trying to batch convert some gif files to animated png files, however, my question is quite clear. How can I input multiple files in a directory to a CLI terminal (I'm using PowerShell)?

I am quite a beginner, so I'm 99% sure I am doing something silly.

I am using a gif to animated png program (gif2apng) It works perfectly when I put something like

gif2apng -i file.gif -o output.png

I have multiple GIFs in that directory, so, I used a wildcard

gif2apng -i *.gif

but then, I get an error

Error: can't open the file '*.gif'

I got this exact error when I tried to select multiple images (with Google's program, img2webp) by not writing all the names like this

img2webp -lossy 1.png 2.png 3.png 4.png 5.png 6.png -o output.png

And by just writing

img2webp -lossy *.png

Please guide me

Thanks, Harshit

Compo
  • 36,585
  • 5
  • 27
  • 39
Harshit Tomar
  • 37
  • 1
  • 1
  • 7

1 Answers1

1

Unlike POSIX-compatible shells (such as bash on Unix-like platforms), PowerShell on Windows[1] does not perform globbing - that is, it does not automatically expand *.gif to the names of the files in the current directory that have extension .gif

Therefore, globbing pattern *.gif is passed verbatim to gif2apng, which seemingly expects literal filenames and therefore fails.

The solution is to let PowerShell perform the globbing explicitly, using the Get-ChildItem cmdlet:

gif2apng -i (Get-ChildItem *.gif).Name -o output.png
  • Get-ChildItem *.gif retrieves all items with extension .gif in the current directory (if you explicitly wanted to exclude directories from matching, you could add the -File switch), as System.IO.FileInfo instances.

  • (...).Name returns their names and passes them as individual arguments to gif2apng.


[1] However, on Unix-like platforms (macOS, Linux) PowerShell (Core} 7+ does perform this automatic globbing for external programs.

mklement0
  • 382,024
  • 64
  • 607
  • 775
  • Thanks a lot! also how can I give a new name to each child file on output?? – Harshit Tomar Jan 17 '21 at 18:07
  • 1
    Glad to hear it was helpful. I'm not familiar with `gif2apng`, unfortunately, but my guess is that if you omit the `-o` argument, you'll get `.png` files with the same base names as the input files, which you can afterwards rename with [`Rename-Item`](https://learn.microsoft.com/powershell/module/microsoft.powershell.management/rename-item) - see [this answer](https://stackoverflow.com/a/53575423/45375), for example. If you need additional assistance, please ask a _new_ question (feel free to comment here to let me know once you have done so). – mklement0 Jan 17 '21 at 18:22
  • 1
    Well, it seems like gif2apng does not support batch execution but you surely clarified my confusion on this topic and img2webp does support batch file, thanks a lot for your support, it is only because of selfless and dedicated guys like you that the community is a helpful place for newbies like me. – Harshit Tomar Jan 17 '21 at 19:35
  • 1
    I'm glad to hear it was helpful, @HarshitTomar, and thank you for the kind words. Final thought: a way to work around the `gif2apng` limitation is to invoke it once for each input file (which will be slower than batch processing by a utility that supports it): `Get-ChildItem *.gif | ForEach-Object { gif2apng -i $_.Name }` – mklement0 Jan 17 '21 at 19:41