命令提示符:从任何文本文件执行命令(没有“.bat"或“.cmd"扩展名) [英] Command Prompt: Execute Commands from Any Text File (Not Having ".bat" or ".cmd" Extensions)
问题描述
我找不到任何方法,例如:
I can't find any way to do, for example, the following:
cmd.exe/C "script.txt"
换句话说,如果文件包含有效的批处理脚本,我需要命令提示符来(尝试)执行具有任何扩展名(不一定是 .bat
或 .cmd
)的文件代码.我正在寻找类似于 Unix shell 的行为:
In other words, I need Command Prompt to (try) to execute file with any extension (not necessarily .bat
or .cmd
) if it contains valid batch script code. I'm looking for behavior similar to Unix shells:
./script.txt
虽然在 Unix 上 shebang (#!/bin/sh
) 负责理解该文件实际上是一个脚本,但它似乎在 Windows .bat
或 .cmd
扩展名起到相同的作用,表示命令提示符的批处理脚本文件.
While on Unix the shebang (#!/bin/sh
) is responsible for understanding that the file is actually a script, it seems like on Windows .bat
or .cmd
extensions play the same role, indicating a batch script file for Command Prompt.
是否可以避免这种情况并强制命令提示符解释具有任何名称的文件?
Is it possible to avoid that and force Command Prompt to interpret a file with any name?
注意:请不要回答:
给你的文件 .bat
或 .cmd
扩展名.
Give your file
.bat
or.cmd
extension.
这不是问题所在.
推荐答案
您首先需要一个安装"脚本:
you first need an "installation" script :
@echo off
rem :: A files with .TEST extension will be able to execute batch code but is not perfect as the %0 argument is lost
rem :: "installing" a caller.
if not exist "c:caller.bat" (
echo @echo off
echo copy "%%~nx1" "%%temp%%\%%~nx1.bat" /Y ^>nul
echo "%%temp%%\%%~nx1.bat" %%*
) > c:caller.bat
rem :: associating file extension
assoc .test=batps
ftype batps=c:caller "%%1" %*
然后尝试一个简单的 .test
文件:
then try a simple .test
file:
@echo off
for /l (1;1;10) do (
echo testing .TEST extension
)
事实上ASSOC
和FTYPE
都立即生效,因此您可以在安装"后立即启动.test
文件.通过直接编辑注册表,您最终可以获得更多控制 -> 如何创建表现为 .cmd/.bat 的文件扩展名? .还要检查丢弃处理程序 -> http://msdn.microsoft.com/en-us/library/windows/desktop/cc144165%28v=vs.85%29.aspx
In fact ASSOC
and FTYPE
both have immediate effect so you can start a .test
file right after "installation". With direct editing of the registry eventually you can get more control -> How to create file extension that behaves as .cmd/.bat? . Check also drop handlers -> http://msdn.microsoft.com/en-us/library/windows/desktop/cc144165%28v=vs.85%29.aspx
这篇关于命令提示符:从任何文本文件执行命令(没有“.bat"或“.cmd"扩展名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!