窗口中是否有任何方法可以通过它来执行不带* .bat扩展名的批处理脚本?如何使用* .bat扩展名运行批处理脚本
回答
这对我来说是一个有趣的话题!我想对此做一些观察。
重要的一点是:批处理文件是扩展名为.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。
安东尼
这可能是可能肯定的,但可能也没有一个简单的方法=)引起首先..安全的。 我在一年前尝试做同样的事情,并在一个月前,但我没有找到解决方案。你可以尝试做
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
希望它有帮助!
在我的情况下,为了使windows运行文件没有扩展名(仅适用于* .cmd,* .exe),我错过了pathext变量(在系统可变参数中)以包含.cmd。一旦添加,我没有更多的运行file.cmd比简单的文件。
环境变量 - >添加/编辑系统变量包括.CMD,.EXE(ofcourse你的文件应该在路径)
- 1. 如何从批处理脚本中运行批处理脚本?
- 2. 使用批处理文件(.bat)运行PS1脚本
- 3. 如何在批处理脚本中运行powershell脚本和批处理脚本?
- 4. 在批处理脚本中没有扩展名的文件名
- 5. 运行批处理脚本,PowerShell脚本
- 6. 从.bat(批处理文件)运行R脚本
- 7. Windows批处理脚本 - 如何使用定义的扩展名筛选文件
- 8. 如何运行带有.bat脚本的.lnk扩展名的应用程序
- 9. 如何在没有扩展名的情况下使用批处理运行exe
- 10. Dos批处理脚本排除某些扩展名的文件
- 11. Exlcude某些文件扩展名 - 批处理脚本
- 12. 如何运行批处理文件(.Bat)使用SQL触发器
- 13. 如何使用批处理脚本运行多个SpecFlow场景?
- 14. 如何使用python脚本运行批处理文件?
- 15. 使用批处理脚本中的参数运行PowerShell脚本
- 16. 如何使用WMI发现正在运行的批处理(.bat)文件名?
- 17. 调试前运行批处理脚本
- 18. 如何编写使用BTEQ执行Teradata查询的批处理(* .bat)脚本?
- 19. 从Quality Center运行批处理脚本
- 20. 在批处理文件中运行批处理脚本
- 21. 批处理mysql脚本/计划运行批处理作业
- 22. 批处理文件扩展名chanage
- 23. 连续运行nohup脚本批处理
- 24. 如何使用批处理脚本
- 25. 如何使用批处理脚本
- 26. 在Windows批处理脚本中获取没有扩展名的文件名
- 27. 批处理文件运行JMeter脚本
- 28. 使用批处理脚本
- 29. 使用批处理脚本
- 30. 使用批处理脚本
嗯,你可以将其重命名为'.cmd'文件... –
该脚本必须具有扩展名(.BAT或.CMD)才能让Windows知道它是批处理脚本。我不认为有可能将自己的扩展名定义为批处理文件,也不能指示Windows仅将任何文件用作批处理脚本。 – dbenham
@dbenham没有兄弟,我还没有测试,我通过我的电话回复。但是我相信我早些时候看起来像.batch扩展。但我GOOGLE了一下,发现我错了。谢谢你纠正我。 –