扩展名的情况下运行批处理脚本

扩展名的情况下运行批处理脚本

本文介绍了如何在不使用 *.bat 扩展名的情况下运行批处理脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Windows 中有没有什么方法可以执行没有 *.bat 扩展名的批处理脚本?

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

推荐答案

这对我来说是一个有趣的话题!我想对此做一些观察.

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 命令.
  • 执行 CALL :NAME 命令(内部子程序).
  • 执行 SETLOCAL/ENDLOCAL 命令.

现在有趣的部分是:任何文件都可以重定向为 CMD.exe 的输入,因此其中包含的 DOS 命令以类似于批处理文件的方式执行,但存在一些差异.最重要的是以前的批处理文件工具将不起作用.下面的 NOT-Batch 文件(我称之为 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...

要执行上一个文件,请键入:CMD/V:ON

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

更特殊的差异与NOT-Batch 文件中的命令在命令行上下文,而不是Batch-file 上下文中执行的事实有关.或许 Dave 或 jeb 可以详细说明这一点.

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

以这种方式执行前一个文件:CMD <BATCH2.TXT > 空

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 空

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

有趣!不是吗?

EDIT:现在,可以在 NotBatch.txt 文件中模拟 GOTO 和 CALL 命令!!!请参阅这篇文章.

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

安东尼奥

这篇关于如何在不使用 *.bat 扩展名的情况下运行批处理脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 13:16