65

I'm trying to create a right-click context menu command for compressing JavaScript files with YUI compressor. My ultimate goal is to try to get this to run on a context menu:

java.exe -jar yuicompressor-2.4.2.jar -o <filename>.min.js <filename>.js

I know I can use the variable %1 to reference the file name being opened. I can't figure out how to get this command into a batch file syntax and haven't been able to find any answers online.

Update:
Jeremy's answer (+comments) worked. For anyone who stumbles upon this, here is what I had to do:

In the action I created for the JavaScript file, I used this as the command:

minify.bat "%1"

Which calls my batch script, which looks like this:

java.exe -jar yuicompressor-2.4.2.jar -o "%~dpn1.min.js" %1

For the batch script, keep in mind that the code above assumes the directories for java.exe & yuicompressor are both added to your PATH variables. If you don't add these to your path, you'll have to use the full path for the files.

The sequence %~dpn is used to get:

  1. %~d - The drive
  2. %~p - The path
  3. %~n - The file name
1
  • 6
    See CALL /? for the full list of enhancements to windows batch parameters, including %~x1 for the extension. Commented Aug 29, 2012 at 8:03

3 Answers 3

57

Change the action to call a batch file:

RunCompressor.bat "%1"

Use %~n1 to get the filename without the extension in RunCompressor.bat:

start javaw.exe -jar yuicompressor-2.4.2.jar -o "%~n1.min.js" "%1"

Helpful article

start javaw.exe closes the command window when running the batch file.

Sign up to request clarification or add additional context in comments.

6 Comments

I tried using that, and instead of it evaluating to the name of the file, I ended up with a file with a name of "%~n1.min.js".
What version of Windows are you using?
Oh, is that command line what you're entering into the File Types dialog as a new action?
I'm using the Folder Options settings to add the new action, like this: imgur.com/y5nuF
OK. Use %1 there, but call a .bat or .vbs file. I've updated my answer.
|
12
echo path of this file name is: %~dp0
echo file name of this file without extension is:%~n0
echo file extention of this file is:%~x0
echo The file name of this file is: %~nx0

Comments

0

Write your own class that determines the output filename to send to YUI compressor.

java.exe -cp yuicompressor-2.4.2.jar MyClass "%1"

Comments

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.