0

I saw these posts:

But all of the commands in it result in errors. Such as:

PS D:\> find . -name '*.png' -exec mogrify -format jpg {} +
FIND: Parameter format not correct
PS D:\> for /r . %a in (*.png) do mogrify -resample 90 -format jpg "%~a"
At line:1 char:4
+ for /r . %a in (*.png) do mogrify -resample 90 -format jpg "%~a"
+    ~
Missing opening '(' after keyword 'for'.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingOpenParenthesisAfterKeyword

PS D:\> for(/r . %a) in (*.png) do mogrify -resample 90 -format jpg "%~a"
At line:1 char:13
+ for(/r . %a) in (*.png) do mogrify -resample 90 -format jpg "%~a"
+             ~
Missing statement body in for loop.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingLoopStatement

PS D:\> for(/r . %a) {in (*.png) do mogrify -resample 90 -format jpg "%~a"}
/r : The term '/r' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:5
+ for(/r . %a) {in (*.png) do mogrify -resample 90 -format jpg "%~a"}
+     ~~
    + CategoryInfo          : ObjectNotFound: (/r:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS D:\> magickrec . 90% png jpg
magickrec : The term 'magickrec' is not recognized as the name of a cmdlet, function, script file, or operable
program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ magickrec . 90% png jpg
+ ~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (magickrec:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

PS D:\>

I don't have enough reputation to comment/etc. on any questions here on superuser, so I cannot reply to anything. Also, I want to do this universally, so don't give me static paths like "D:" I want it in current directory, so I can use the command anywhere. In addition, the ImageMagick command executed should be mogrify NOT convert.

  • 1
    Welcome to SuperUser! I'm not here to help you with your question, I just wanted to tell you that you can comment on any of your questions and its answers, irrespective of your reputation – Saaransh Garg Nov 07 '21 at 07:11
  • Yes, my own questions. But not other questions. I wanted to reply to a comment since I didn't want to make a new question, since there were questions asked about this before, with answers even. But I cannot comment on the answers telling them that the commands don't work, because I don't have the reputation needed to comment on said answers to say the commands don't work. So, I have to ask a new question since none of the answers on old questions work. – coldreactive Nov 07 '21 at 07:13
  • 3
    `find . -name '*.png' -exec mogrify -format jpg {} +` is a \*nix command so obviously it won't work in Windows unless you have some POSIX subsystem installed and call specifically the \*nix `find` instead of Windows `find` – phuclv Nov 07 '21 at 07:40
  • 2
    `for /r` is a cmd command so obviously you can't run it in powershell. All your commands have wrong syntax. Run cmd and then run `for /?` to know the syntax. Or learn powershell which has a cleaner syntax without weird legacy issues like cmd – phuclv Nov 07 '21 at 07:42
  • I tried using `dir -Filter *.png -Recurse | mogrify -format jpg` but nothing happened. I tried appending the file output of the previous command: `dir -Filter *.png -Recurse | mogrify -format jpg %{$_.FullName}` but it just spat out errors like "No such file or directory. I've read up on what pipelines do, they send the result of the first command to the next. So I know that much. But this whole system is really weird. I don't really care to use CMD because I'd rather be able to SHIFT+RightClick and open powershell in any directory I want to do this in. – coldreactive Nov 07 '21 at 07:54
  • And if I use `dir -Filter *.png -Recurse | mogrify -format jpg FullName` like the Microsoft examples show; it won't work, mogrify spits out an error stating that FullName is an invalid filename. – coldreactive Nov 07 '21 at 07:57

2 Answers2

1

The command I needed to run is as follows:

dir -Filter *.png -Recurse | %{mogrify -format jpg $_.FullName}

Executing this in the folder you want it to will mogrify all files in the current directory and in any directories in it. If you want to delete all of the PNGs that are left behind after, you should use...

dir -Filter *.png -Recurse | del
  • coldreactive: `dir -Filter *.png -Recurse | %{mogrify -format jpg $_.FullName}` produces the following error: `'%{mogrify' is not recognized as an internal or external command, operable program or batch file.` – jpeni Dec 07 '21 at 21:05
  • @jpeni I've converted this to a comment for feedback - as it isn't an answer. Thanks for letting me know! – studiohack Dec 09 '21 at 21:32
1

I'd rather be able to Shift + Right Click and open PowerShell in any directory I want to do this in.

If the folder is open in Explorer, you can just type cmd where the file path is normally given and then press Enter to open a command window (i.e. not PowerShell) in that folder. The batch command to do what you are looking for would be e.g.:

for /r . %a in (*.jpg) do (magick mogrify -resample 90 -format png "%~a")

as already noted in your question.

(This answer skips a PowerShell solution, as one is already provided in the self-answer.)

Anaksunaman
  • 16,718
  • 4
  • 38
  • 45