windows 命令提示符:从任何文本文件执行命令(没有“.bat”或“.cmd”扩展名)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23162474/
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
Command Prompt: Execute Commands from Any Text File (Not Having ".bat" or ".cmd" Extensions)
提问by Alexander Shukaev
I can't find any way to do, for example, the following:
我找不到任何方法来做,例如,以下内容:
cmd.exe /C "script.txt"
cmd.exe /C "script.txt"
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:
换句话说,如果文件包含有效的批处理脚本代码,我需要命令提示符(尝试)执行具有任何扩展名(不一定.bat
或.cmd
)的文件。我正在寻找类似于 Unix shell 的行为:
./script.txt
./script.txt
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.
虽然在 Unix 上shebang( #!/bin/sh
) 负责理解该文件实际上是一个脚本,但在 Windows.bat
或.cmd
扩展中似乎扮演着相同的角色,表示命令提示符的批处理脚本文件。
Is it possible to avoid that and force Command Prompt to interpret a file with any name?
是否可以避免这种情况并强制命令提示符解释具有任何名称的文件?
NOTE:Please, no answers like:
注意:请不要回答:
Give your file
.bat
or.cmd
extension.
提供您的文件
.bat
或.cmd
扩展名。
That's not what the question is about.
这不是问题的内容。
回答by Aacini
This depends on the complexity of the NON-Batch file. If the NON-Batch file does not use these facilities:
这取决于非批处理文件的复杂性。如果非批处理文件不使用这些工具:
- Access to Batch file parameters via %1 %2 ... and execution of SHIFT command.
- Execution of GOTO command.
- Execution of CALL :NAME command (internal subroutine).
- Execution of SETLOCAL/ENDLOCAL commands.
- 通过 %1 %2 ... 访问批处理文件参数并执行 SHIFT 命令。
- 执行 GOTO 命令。
- 执行 CALL :NAME 命令(内部子程序)。
- SETLOCAL/ENDLOCAL 命令的执行。
then you may execute anyfile as a "Batch file" via this trick:
那么你可以通过这个技巧将任何文件作为“批处理文件”执行:
cmd < anyFile.ext
Further details at this post
这篇文章的更多细节
回答by npocmaka
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" %*
then try a simple .test
file:
然后尝试一个简单的.test
文件:
@echo off
for /l (1;1;10) do (
echo testing .TEST extension
)
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
事实上ASSOC
,FTYPE
两者都会立即生效,因此您可以.test
在“安装”后立即启动文件。通过直接编辑注册表,您最终可以获得更多控制权 ->如何创建表现为 .cmd/.bat 的文件扩展名?. 还要检查丢弃处理程序 -> http://msdn.microsoft.com/en-us/library/windows/desktop/cc144165%28v=vs.85%29.aspx