如何使用来运行了批处理脚本* .BAT延伸 [英] How to run batch script with out using *.bat extention

查看:244
本文介绍了如何使用来运行了批处理脚本* .BAT延伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有在窗口中的任何方法,通过它我们可以不* .BAT延伸执行批处理脚本?

Is there any method in window through which we can execute a batch script without *.bat extention?

推荐答案

这是一个有趣的话题给我!我想做一些关于它的意见。

This is an interesting topic to me! I want to do some observations about it.

重要的一点第一:一个批处理文件是.bat或.cmd扩展名的文件。期。批处理文件可以实现的,除了通常的DOS的执行命令,某些特定批处理文件设施,特别是:

The important point first: A Batch file is a file with .BAT or .CMD extension. Period. Batch files can achieve, besides the execution of usual DOS commands, certain specific Batch-file facilities, in particular:


  • 通过%访问批处理文件参数1%,2 ...和SHIFT命令的执行。

  • GOTO命令的执行。

  • 调用的执行:NAME命令(内部子程序的)

  • SETLOCAL / ENDLOCAL命令的执行。

现在有趣的部分:任何文件都可以重定向为为CMD.EXE输入,因此DOS命令它包含的批处理文件中以类似的方式执行,有一些差异。其中最重要的是,previous的批处理文件设施的将无法工作。另一个差异是在以下NOT-批处理文件说明(我把它叫做BATCH.TXT):

Now the funny part: Any file can be redirected as input for CMD.exe so the DOS commands contained in it are executed in a similar way of a Batch file, with some differences. The most important one is that previous Batch-file facilities will NOT work. Another differences are illustrated in the NOT-Batch file below (I called it BATCH.TXT):

@echo off
rem Echo off just suppress echoing of the prompt and each loop of FOR command
rem but it does NOT suppress the listing of these commands!

rem Pause command does NOT pause, because it takes the character that follows it
pause
X

rem This behavior allows to put data for a SET /P command after it
set /P var=Enter data: 
This is the data for previous command!
echo Data read: "%var%"

rem Complex FOR/IF commands may be assembled and they execute in the usual way:
for /L %i in (1,1,5) do (
   set /P line=
   if "!line:~0,6!" equ "SHOW: " echo Line read: !line:~6!
)
NOSHOW: First line read
SHOW: Second line
NOSHOW: This is third line
SHOW: The line number 4
NOSHOW: Final line, number five

rem You may suppress the tracing of the execution redirecting CMD output to NUL
rem In this case, redirect output to STDERR to display messages in the screen
echo This is a message redirected to STDERR >&2

rem GOTO command doesn't work:
goto label
goto :EOF
rem but both EXIT and EXIT /B commands works:
exit /B

:label
echo Never reach this point...

要执行previous文件,输入:CMD / V:ON< BATCH.TXT
需要/ V开关启用延迟扩展。

To execute previous file, type: CMD /V:ON < BATCH.TXT The /V switch is needed to enable delayed expansion.

更专业的差异与在命令行上下文的,而不是批处理文件上下文中执行在不批处理文件命令的事实。也许戴夫或杰布能够阐述这一点。

More specialized differences are related to the fact that commands in the NOT-Batch file are executed in the command-line context, NOT the Batch-file context. Perhaps Dave or jeb could elaborate on this point.

编辑: 的补充意见的(batch2.txt):

Additional observations (batch2.txt):

@echo off
rem You may force SET /P command to read the line from keyboard instead of
rem from following lines by redirecting its input to CON device.

rem You may also use CON device to force commands output to console (screen),
rem this is easier to write and read than >&2

echo Standard input/output operations> CON
echo/> CON
< CON set /P var=Enter value: > CON
echo/> CON
echo The value read is: "%var%"> CON

执行previous文件是这样的:CMD&LT; BATCH2.TXT> NUL

Execute previous file this way: CMD < BATCH2.TXT > NUL

修改更多补充意见的(batch3.txt)

EDIT: More additional observations (batch3.txt)

@echo off

rem Dynamic access to variables that usually requires DelayedExpansion via "call" trick

rem Read the next four lines; "next" means placed after the FOR command
rem (this may be used to simulate a Unix "here doc")

for /L %i in (1,1,4) do (
   set /P line[%i]=
)
Line one of immediate data
This is second line
The third one
And the fourth and last one...

(
echo Show the elements of the array read:
echo/
for /L %i in (1,1,4) do call echo Line %i- %line[%i]%
) > CON

执行以通常的方式本文件:CMD&下; BATCH3.TXT> NUL

Execute this file in the usual way: CMD < BATCH3.TXT > NUL

有趣!难道不是吗?

修改:现在,GOTO和CALL指令可能会在NotBatch.txt文件进行模拟!请参见这个帖子

EDIT: Now, GOTO and CALL commands may be simulated in the NotBatch.txt file!!! See this post.

安东尼奥

这篇关于如何使用来运行了批处理脚本* .BAT延伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆