1

I have added a "convert to JPG" option to my windows context menu. I have accomplished this by setting the context menu to run magick convert %1 %1.jpg. However, while this works, it will leave the original file extension in there too (so TestImage.png becomes TestImage.png.jpg), but I want to remove this (so it simply becomes TestImage.jpg).

As far as I can tell, things like %~n1 would only work in a FOR block in a batch script.

Is there anything I can do, or am I attacking this problem in the wrong way?

Many thanks.

1 Answer 1

1

Use

cmd /C for /F "delims=" %%G in ("%1") do magick convert "%~G" "%~nG.jpg"

or

cmd /C for /F "delims=" %%G in ("%1") do magick convert "%~G" "%~dpnG.jpg"

The former line tested using the following registry hack

reg query "HKEY_CLASSES_ROOT\pngfile\shell\ForLoop\Command"

HKEY_CLASSES_ROOT\pngfile\shell\ForLoop\Command
    (Default)    REG_SZ    cmd /C for /F "delims=" %%G in ("%1") do CliParserPause.exe convert "%~G" "%~nG.jpg"

and using the following simple C++ program which lists all its command line parameters supplied (and then pauses to enable observing its output):

// CliParserPause.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <wchar.h>
#include <cstdio>
#include <stdlib.h>

int main(int argc, wchar_t* argv[])
{
    for (int i = 0; i < argc; ++i)
    {
        wprintf(L"param %d = %S\n", i, argv[i]);
    }
    wprintf(L"press any key to continue...");
    std::getchar();
    exit(-999 - argc);  /* exitcode to OS = ( -1000 -supplied_paramaters_count ) */
    return 0;
}

Test case output:

C:\WINDOWS\system32> CliParserPause.exe convert "D:\bat\SO\Loading1.png" "Loading1.jpg"
param 0 = CliParserPause.exe
param 1 = convert
param 2 = D:\bat\SO\Loading1.png
param 3 = Loading1.jpg
press any key to continue...

Another test case shows some non-trivial example with spaces in path and file name:

C:\WINDOWS\system32> CliParserPause.exe convert "D:\bat\odds and ends\a b\c d\e f\File Explorer Properties.png" "File Explorer Properties.jpg"
param 0 = CliParserPause.exe
param 1 = convert
param 2 = D:\bat\odds and ends\a b\c d\e f\File Explorer Properties.png
param 3 = File Explorer Properties.jpg
press any key to continue...
1
  • Thank you! The latter of the two examples you posted works perfectly. Commented Aug 10, 2019 at 13:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .