通过右键单击任何文件来运行批处理脚本

发布于 2024-11-26 11:30:10 字数 643 浏览 0 评论 0原文

其想法是右键单击任何文件,然后在下拉列表中选择“备份”,该下拉列表仅复制该文件并在其名称末尾添加日期时间,然后可能将该副本移动到另一个驱动器。

我想要一种通过右键单击文件并将其作为参数传递(无需输入)来运行批处理脚本的方法,或者以某种方式知道该文件是应该在脚本中使用的文件。

主要针对 Windows XP,我需要它是本机的,因为我工作的许多站点计算机不属于我们,并且我不想复制超出必要的文件或依赖于超出可用标准命令的任何内容(三分之二的命令)机器没有 powershell 等)

我知道如何更改文件选项以在特定文件夹位置打开 cmd(如下),我可能会更改此设置以适用于 1 个特定文件类型,但我不希望为每种类型的文件添加一个密钥机器。

[HKEY_CLASSES_ROOT\文件夹\shell\1.bat\命令] @="c:\windows\system32\cmd.exe \"%1\""

来自另一个站点的一种解决方法几乎为我解决了这个问题

jvierra - “Windows 一直具有这种能力,无需更改注册表。

放置一个蝙蝠或将文件拖放到桌面上的 VBS 文件中,bat 会收到拖放文件的文件名 %1,vbscript 会收到 WScript.Arguments(0) ,

效果很好。蝙蝠或。从那时起,脚本可以对文件执行任何操作。”

The idea is to right click any file and then select "backup" in a drop down that just copies the file and adds a date time to the end of its name, then possibly moves that copy to another drive.

I would like a method of running a batch script by right clicking on a file and either passing that in as an argument (without typing it in), or somehow knowing that file is the one that should be used in the script.

Mainly for windows XP, I need it to be native as many site computers I work on do not belong to us and I don't want to copy over more files than necessary or rely on anything more than the standard commands available (two thirds of the machines don't have powershell ect)

I know how you can change file options to open a cmd at a specific folder location(below), and I cold probably change this to work for 1 specific file type, but I don't want to add a key for each type of file on the machine.

[HKEY_CLASSES_ROOT\Folder\shell\1.bat\command]
@="c:\windows\system32\cmd.exe \"%1\""

From another site one work around that nearly solves this for me

jvierra - "Windows has always had that ability without changing the registry.

Place a bat or VBS file on your desktop. Drag and drop a file on the bat icon. The bat will receive the file name of the dropped file as %1 and vbscript will receive it as WScript.Arguments(0).

Try it. It works quite well. The bat or script can do anything with the file from that point."

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丶视觉 2024-12-03 11:30:10

您可以将蝙蝠添加到“发送到”菜单。请参阅 http://support.microsoft.com/kb/310270

简而言之,只需将您的 .bat 文件复制到用户中即可发送到文件夹。

COPY MYBACKUP.BAT "%USERPROFILE%\SendTo"

用户调用您的蝙蝠选择“发送到”菜单项的选项。

对于更复杂的参数化,例如更改菜单中显示的文本或图标...您可以创建一个指向 .BAT 的链接,并将该链接放在 SendTo 文件夹中,而不是 .bat 本身

COPY "My very special backup.lnk" "%USERPROFILE%\SendTo"

您可以先运行一个快速测试。使用此内容创建一个 BAT 文件,并将其复制到 Sendto 文件夹中。

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %~dpnx0
echo Subject is %1 %~dpnx1
pause  

编辑:根据一些评论,我通过添加所需的引号更正了 COPY 命令中的 SendTo 文件夹规范;我附加了一个测试示例,并更正了 %~dpnx 语法

you can add your bat to the "Send To" menu. See http://support.microsoft.com/kb/310270

In brief, just copy your .bat file into the user SendTo folder.

COPY MYBACKUP.BAT "%USERPROFILE%\SendTo"

the user invokes your bat selecting the option of the "Send To" menu item.

for more sophisticated parametrization, like changing the text displayed in the menu, or the icon... you may create a link to your .BAT and place the link in the SendTo folder instead of the .bat itself

COPY "My very special backup.lnk" "%USERPROFILE%\SendTo"

You may first run a quick test. Create a BAT file with this content, and copy it over Sendto folder.

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %~dpnx0
echo Subject is %1 %~dpnx1
pause  

Edit: following some of the commments, I have corrected the SendTo folder specification in the COPY command, by adding the required quotes; and I have appended an test example, and corrected the %~dpnx syntax

温柔少女心 2024-12-03 11:30:10

@PA 示例(复制在下面以便于查看)有一点点偏差。

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %dpnx0
echo Subject is %1 %dpnx1
pause  

我没有足够的声誉来回复@PA。您忘记在变量中包含 ~ 。本次问答A 对我帮助很大,所以我希望这对其他人有帮助。感谢@daniel和@PA

更正了下面的daniel测试示例

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %~dpnx0
echo Subject is %1 %~dpnx1
pause

如果您只想回显文件名而不带路径,那么您可以使用 %~n1

示例:

echo Subject is %~n1

@PA example (copied right below for easy viewing) is off by a hair.

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %dpnx0
echo Subject is %1 %dpnx1
pause  

I don't have enough reputation to respond to @PA. You forgot to include the ~ in the variable. This Q & A helped me a lot so I hope this helps someone else out. Thanks @daniel and @PA

Corrected daniel test example below

@echo off
echo Current Directory is %cd%
echo Current batch run is %0 %~dpnx0
echo Subject is %1 %~dpnx1
pause

If you want to just echo the name of the file without the path then you would use %~n1

example:

echo Subject is %~n1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文