Getting a file name without extension in Windows Script

I am trying to create a right-click context menu command to compress JavaScript files using the YUI compressor. My ultimate goal is to try to run this in the context menu:

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

I know that I can use the variable %1 to refer to the file name being opened. I cannot figure out how to get this command in the syntax of a batch file and could not find the answers on the Internet.

Update:
Jeremy's answer (+ comments) worked. For those who stumble upon this, here is what I need to do:

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

 minify.bat "%1" 

Which calls my script package, which looks like this:

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

For the script package, keep in mind that the above code assumes that the directories for java.exe and yuicompressor are added to your PATH variables. If you do not add them to your path, you will have to use the full path for the files.

The sequence %~dpn used to get:

  • %~d - Drive
  • %~p - Path
  • %~n - File Name
+49
command-line windows batch-file
Sep 24 '09 at 14:45
source share
3 answers

Change the action to invoke the batch file:

 RunCompressor.bat "%1" 

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

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

Useful article

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

+43
Sep 24 '09 at 14:49
source share
 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 
+2
Dec 24 '13 at 14:16
source share

Write your own class that defines the name of the output file to send to the YUI compressor.

 java.exe -cp yuicompressor-2.4.2.jar MyClass "%1" 
0
Sep 24 '09 at 15:42
source share



All Articles