在 Windows 批处理脚本中获取没有扩展名的文件名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1472191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the file name without extension in a Windows Batch Script
提问by Dan Herbert
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:
我正在尝试创建一个右键单击上下文菜单命令,用于使用 YUI 压缩器压缩 JavaScript 文件。我的最终目标是尝试让它在上下文菜单上运行:
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.
我知道我可以使用该变量%1
来引用正在打开的文件名。我无法弄清楚如何将此命令转换为批处理文件语法,并且无法在线找到任何答案。
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:
在我为 JavaScript 文件创建的操作中,我将其用作命令:
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.
对于批处理脚本,请记住上面的代码假定 java.exe 和 yuicompressor 的目录都添加到您的PATH
变量中。如果不将这些添加到路径中,则必须使用文件的完整路径。
The sequence %~dpn
is used to get:
该序列%~dpn
用于获得:
%~d
- The drive%~p
- The path%~n
- The file name
%~d
- 驱动器%~p
- 路径%~n
- 文件名
采纳答案by Jeremy Stein
Change the action to call a batch file:
更改调用批处理文件的操作:
RunCompressor.bat "%1"
Use %~n1
to get the filename without the extension in RunCompressor.bat:
使用%~n1
来获取文件名没有在RunCompressor.bat扩展:
start javaw.exe -jar yuicompressor-2.4.2.jar -o "%~n1.min.js" "%1"
start javaw.exe closes the command window when running the batch file.
start javaw.exe 在运行批处理文件时关闭命令窗口。
回答by lygstate
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
回答by Jeremy Stein
Write your own class that determines the output filename to send to YUI compressor.
编写您自己的类来确定要发送到 YUI 压缩器的输出文件名。
java.exe -cp yuicompressor-2.4.2.jar MyClass "%1"