如何使用* .bat扩展名运行批处理脚本

有没有方法在窗口中,我们可以执行批处理脚本没有* .bat扩展?

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

重要的一点是:batch file是扩展名为.BAT或.CMD的文件。 期。 batch file除了执行常用的DOS命令之外,还可以实现某些特定的batch filefunction,特别是:

  • 通过%1%2 …访问batch file参数并执行SHIFT命令。
  • 执行GOTO命令。
  • 执行CALL:NAME命令( 内部子程序 )。
  • 执行SETLOCAL / ENDLOCAL命令。

现在有趣的部分:任何文件都可以被redirect为CMD.exe的input,所以包含在其中的DOS命令与batch file类似的方式执行,有一些不同之处。 最重要的是以前的batch filefunction将不起作用。 另一个区别在下面的NOT-Batch文件中(我称之为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 <BATCH.TXT需要/ V开关以启用延迟扩展。

更特殊的差异与NOT-Batch文件中的命令命令行上下文中执行而不是batch file上下文有关。 也许戴夫或杰布可以详细说明这一点。

编辑: 其他意见 (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> NUL

编辑更多额外的意见 (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

有趣! 不是吗?

编辑 :现在,GOTO和CALL命令可能会在NotBatch.txt文件中模拟! 看到这个职位 。

安东尼奥

这可能是的,但可能也不是一个简单的方法=)首先是安全。 我在一年前尝试做同样的事情,还有一个月前,但是我没有find解决办法..你可以试着去做

execu.cmd

 type toLaunch.txt >> bin.cmd call bin.cmd pause > nul exit 

然后在toLaunch.txt放

 @echo off echo Hello! pause > nul exit 

例如,它将“编译”代码,然后执行“输出”文件,这就是“parsing”

而不是parsing,你也可以重命名使用,也许在脚本内使用toLaunch.txt里面的自动重命名

 ren %0 %0.txt 

希望它有帮助!

在我的情况下,使Windows运行文件没有扩展名(只为* .cmd,* .exe)观察到,我错过了pathextvariables(在系统可变参数)包括.cmd。 一旦添加我没有更多的运行file.cmd比简单的文件。

环境variables – >添加/编辑系统variables包括.cmd; .exe(当然你的文件应该在path中)