I have enabled a “convert to JPG
” alternative in my Windows context menu. To do this, I programmed the context menu to execute the command “magick convert %1 %1.jpg
“. Although this approach functions, it retains the initial file extension, resulting in a file named “TestImage.png.jpg
” instead of “TestImage.jpg
“.
I desire to remove the original file extension and have the output file named simply “TestImage.jpg
“.
However, I believe that utilizing variables like %~n1
will only work in a batch script’s FOR block.
Is there any other solution available, or am I taking the wrong approach?
Thank you very much.
3 Answers
Introduction
The Windows context menu is a useful tool that allows users to quickly access frequently-used commands or programs. However, sometimes it may not function as expected, such as when executing an application and retaining the original file extension. In this blog post, we will explore a solution to this problem.
The Problem
The problem arises when executing an application from the Windows context menu and retaining the original file extension. This can result in file names that are unnecessarily long and difficult to manage. For example, if we execute the command “magick convert TestImage.png TestImage.png.jpg
“, the resulting file name will be “TestImage.png.jpg
” instead of “TestImage.jpg
“.
The Solution
One solution is to use a batch script to remove the original file extension and rename the output file. However, this approach may not be feasible for all users, especially those who are not familiar with batch scripting.
Another solution is to modify the context menu command to remove the original file extension and rename the output file. To do this, we can use the following command:
magick convert %1 %~dpn1.jpg
Let’s break down this command:
– %1
: This variable represents the file path of the selected file in the context menu.
– %~dpn1
: This variable represents the file path without the file extension. The d
option specifies the drive letter, the p
option specifies the path, and the n
option specifies the file name without the extension.
– .jpg
: This specifies the new file extension.
Using this command will result in a file name of “TestImage.jpg
” instead of “TestImage.png.jpg
“.
Implementation
To implement this solution, we need to modify the context menu command. Here are the steps:
1. Open the Registry Editor by pressing the Windows key + R, typing “regedit”, and pressing Enter.
2. Navigate to the following key: HKEY_CLASSES_ROOT*shell
3. Right-click on the “shell” key and select “New” -> “Key”. Name the key whatever you want the context menu command to be called (e.g. “Convert to JPG”).
4. Right-click on the new key and select “New” -> “Key”. Name the key “command”.
5. Double-click on the “(Default)” value under the “command” key and enter the following command:
"C:Program FilesImageMagick-7.0.10-Q16-HDRImagick.exe" convert "%1" "%~dpn1.jpg"
Note that the file path may differ depending on where ImageMagick is installed on your system. Also, make sure to enclose the file paths in quotes to handle spaces in file names.
6. Click “OK” to save the command.
Now, when you right-click on a file and select the context menu command you just created, it will execute the modified command that removes the original file extension and renames the output file.
Conclusion
The Windows context menu is a powerful tool that can be customized to fit your needs. However, sometimes it may not function as expected, such as when retaining the original file extension when executing an application. By modifying the context menu command, we can remove the original file extension and rename the output file, resulting in a cleaner and more manageable file name.
You can use the ren
command in a batch script to rename the file, like this:
ren "%1" "%~n1.jpg"
Here’s the complete batch script that you can use in your context menu:
magick convert "%1" "%~n1.jpg"
ren "%1" "%~n1.jpg"
The %~n1
variable expands to the file name of the first argument (%1
) without the file extension. The ren
command renames the original file to the new file name with the .jpg
extension.
Note: You may need to adjust the path to the magick
executable if it is not in your system’s PATH
environment variable.
I hope this helps! Let me know if you have any questions.
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"
The subsequent basic C++ program is used to display all the command line parameters that were provided, and it then stops running to allow observation of 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 example test case illustrates a more complex scenario that involves spaces in both the path and the 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...