简体   繁体   English

批处理:如何从双击文件中读取路径?

[英]Batch: How to read a path from a double-clicked file?

I am thinking about associating a file type extension to a batch script (.foo), so when double-clicking a file with .foo extension, this script will execute. 我正在考虑将文件类型扩展名与批处理脚本(.foo)相关联,因此当双击扩展名为.foo的文件时,将执行此脚本。 The point is I do not know if there is a way to tell the script the path of the "executed" file (ie, which variable, if any, holds the path of the double clicked file). 关键是我不知道是否有办法告诉脚本“执行”文件的路径(即,哪个变量,如果有的话,保存双击文件的路径)。

Thank you. 谢谢。

The executed file is passed as first argument to the script. 执行的文件作为第一个参数传递给脚本。 A simple example will show. 一个简单的例子将显示。 Create a batchfile (for example foo_command.cmd) which contains: 创建一个包含以下内容的批处理文件(例如foo_command.cmd):

echo %* > %~dpn0.log

Execute your desired filetype and connect it to the batchfile. 执行所需的文件类型并将其连接到批处理文件。 This will write a file named foo_command.log (in the directory of foo_command.cmd) which contains all arguments passed to the script. 这将编写一个名为foo_command.log的文件(在foo_command.cmd目录中),该文件包含传递给脚本的所有参数。 You will see, it is the full path of the executed File. 您将看到,它是执行文件的完整路径。

In general the variable %1 in the script holds the path and there are no other variables set by default. 通常,脚本中的变量%1保存路径,默认情况下不设置其他变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM