【问题标题】:How to run batch script without using *.bat extension如何在不使用 *.bat 扩展名的情况下运行批处理脚本
【发布时间】:2012-10-30 12:15:56
【问题描述】:

在 Windows 中是否有任何方法可以执行没有 *.bat 扩展名的批处理脚本?

【问题讨论】:

  • 好吧,你可以将它重命名为.cmd 文件...
  • 脚本必须有一个扩展名(.BAT 或 .CMD)才能让 Windows 知道它是一个批处理脚本。我不认为可以定义您自己的等同于批处理文件的扩展名,也不可能指示 Windows 即时使用任何文件作为批处理脚本。
  • @dbenham 不,兄弟,我没有测试过,我正在通过手机回复。但我相信我之前似乎有 .batch 扩展名。但我用谷歌搜索发现我不正确。感谢您纠正我。
  • 是什么引发了这个问题?

标签: execution batch-file


【解决方案1】:

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

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

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

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

更具体的差异与 NOT-Batch 文件中的命令在 命令行上下文而非批处理文件上下文中执行的事实有关。也许 Dave 或 jeb 可以详细说明这一点。

编辑: 补充意见 (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 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 NUL

有趣!不是吗?

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

安东尼奥

【讨论】:

  • 是的,很有趣但是 - 唉! - 麻烦。
【解决方案2】:

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

执行.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

希望对您有所帮助!

【讨论】:

    【解决方案3】:

    在我的情况下,为了让 Windows 运行没有扩展名的文件(仅适用于 *.cmd、*.exe),我错过了包含 .cmd 的 pathext 变量(在系统变量中)。添加后,我不再需要运行 file.cmd 而只是文件。

    环境变量 --> 添加/编辑系统变量以包含 .cmd;.exe(当然你的文件应该在路径中)

    【讨论】:

      【解决方案4】:

      只需使用:

      type mybat.txt | cmd
      

      打破它...

      type mybat.txtmybat.txt 读取为文本文件并打印内容。 | 表示捕获其左侧命令打印的任何内容,并将其作为输入传递给右侧命令。然后cmd(您可能猜到了)将它接收到的任何输入解释为命令并执行它们。

      如果您想知道...您可以将 cmd 替换为 bash 以在 Linux 上运行。

      【讨论】:

        【解决方案5】:

        在某种程度上是可能的。您需要管理员权限才能运行 assocftype 命令。还有一个将使用您的代码的“调用者”脚本:

        假设您想要的扩展名为.scr。 然后以管理员身份执行此脚本:

           @echo off
           :: requires Admin permissions
           :: allows a files with .scr (in this case ) extension to act like .bat/.cmd files.
           :: Will create a 'caller.bat' associated with the extension
           :: which will create a temp .bat file on each call (you can consider this as cheating)
           :: and will call it.
           :: Have on mind that the %0 argument will be lost.
        
        
            rem :: "installing" a caller.
            if not exist "c:\scrCaller.bat" (
               echo @echo off
               echo copy "%%~nx1"  "%%temp%%\%%~nx1.bat" /Y ^>nul
               echo "%%temp%%\%%~nx1.bat"  %%*
            ) > c:\scrCaller.bat
        
            rem :: associating file extension
            assoc .scr=scrfile
            ftype scrfile=c:\scrCaller "%%1" %%*
        

        您甚至可以使用 GOTO 和 CALL 以及您知道的其他技巧。唯一的限制是%0 参数将丢失,很难在创建临时文件时对其进行硬编码。 例如,由于许多语言编译一个 .exe 文件,我认为这是一种合法的方法。

        【讨论】:

          【解决方案6】:

          如果您希望将变量导出到调用批处理文件,您可以使用

          for /F "tokens=*" %%g in (file.txt) do (%%g)
          

          此方法有几个限制(不要将:: 用于 cmets),但它非常适合配置文件。

          例子:

          rem Filename: "foo.conf"
          rem 
          set option1=true
          set option2=false
          set option3=true
          
          @echo off
          for /F "tokens=*" %%g in (foo.conf) do (%%g)
          echo %option1%
          echo %option2%
          echo %option3%
          pause
          

          【讨论】:

            猜你喜欢
            • 2021-08-17
            • 2011-09-30
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2023-04-10
            • 2011-06-15
            • 2021-08-17
            相关资源
            最近更新 更多