2012-11-10 61 views
21

窗口中是否有任何方法可以通过它来执行不带* .bat扩展名的批处理脚本?如何使用* .bat扩展名运行批处理脚本

+4

嗯,你可以将其重命名为'.cmd'文件... –

+3

该脚本必须具有扩展名(.BAT或.CMD)才能让Windows知道它是批处理脚本。我不认为有可能将自己的扩展名定义为批处理文件,也不能指示Windows仅将任何文件用作批处理脚本。 – dbenham

+0

@dbenham没有兄弟,我还没有测试,我通过我的电话回复。但是我相信我早些时候看起来像.batch扩展。但我GOOGLE了一下,发现我错了。谢谢你纠正我。 –

回答

11

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

重要的一点是:批处理文件是扩展名为.BAT或.CMD的文件。期。除了执行通常的DOS命令外,批处理文件还可以实现以下特定的批处理文件功能:

  • 通过%1%2 ...访问批处理文件参数并执行SHIFT命令。
  • 执行GOTO命令。
  • 执行CALL:NAME命令(内部子程序)。
  • 执行SETLOCAL/ENDLOCAL命令。

现在有趣的部分:任何文件都可以重定向为CMD.exe的输入,因此包含在其中的DOS命令以类似于批处理文件的方式执行,但有一些差异。最重要的是前面的批处理文件设施将不起作用。另一个差异是在下面的NOT-批处理文件中所示(I称之为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文件中的命令在命令行上下文而不是批处理文件上下文中执行有关。也许戴夫或杰布可以详细阐述这一点。

编辑:附加观测(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

EDIT更多额外的观察(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

有趣!不是吗?

编辑:现在,可以在NotBatch.txt文件中模拟GOTO和CALL命令!见this post

安东尼

0

这可能是可能肯定的,但可能也没有一个简单的方法=)引起首先..安全的。 我在一年前尝试做同样的事情,并在一个月前,但我没有找到解决方案。你可以尝试做

execu.cmd

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

然后在toLaunch.txt把

@echo off 
echo Hello! 
pause > nul 
exit 

就像例如,它将 “编译” 的代码,然后它会执行“输出“文件,这就是”解析“

而不是解析你也可以只是重命名使用,也许把脚本内部自动重命名使用内部toLaunch.txt

ren %0 %0.txt 

希望它有帮助!

0

在我的情况下,为了使windows运行文件没有扩展名(仅适用于* .cmd,* .exe),我错过了pathext变量(在系统可变参数中)以包含.cmd。一旦添加,我没有更多的运行file.cmd比简单的文件。

环境变量 - >添加/编辑系统变量包括.CMD,.EXE(ofcourse你的文件应该在路径)

相关问题