在Windows中,是否有一种方法可以执行没有*.bat扩展名的批处理脚本?
这对我来说是一个有趣的话题!我想对它进行一些观察。
首先要明确的是:批处理文件是具有 .BAT 或 .CMD 扩展名的文件。批处理文件除了执行通常的 DOS 命令外,还可以实现特定的批处理文件功能,尤其包括:
现在来说有趣的部分:任何文件都可以被重定向为 CMD.exe 的输入,因此其中包含的 DOS 命令会以类似于批处理文件的方式执行,但存在一些差异。最重要的差异在于之前提到的批处理文件功能将不起作用。另外一些差异在下面的非批处理文件中进行了说明(我称其为 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...
@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
有趣!不是吗?
编辑:现在,在NotBatch.txt文件中可以模拟GOTO和CALL命令!!!请参见此帖子。
安东尼奥
只需使用:
type mybat.txt | cmd
将其拆分...
type mybat.txt
读取 mybat.txt
的文本文件并打印内容。 |
表示捕获命令左侧的所有输出并将其传递给右侧的命令作为输入。然后 cmd
(您可能已经猜到了)将接收到的任何输入解释为命令并执行它们。
如果您想知道... 您可以将 cmd
替换为 bash
并在 Linux 上运行。
这可能是可能的,但可能不是一件容易的事情 =) 因为首先..安全性。 我曾经尝试过做同样的事情,几年前和几个月前,但是我没有找到解决方案..你可以尝试去做
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
希望有所帮助!
这在某种程度上是可能的。您需要具有管理员权限才能运行 assoc
和 ftype
命令。还需要一个“调用者”脚本来使用您的代码:
假设您想要的扩展名为 .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文件,我认为这是一个合法的方法。
原帖中没有描述任何原因或工作流程。但是,如果您只想键入myscript
而不是myscript.bat
,则可以创建脚本的别名并将别名重命名为myscript
。
我的用例:我在wsl和批处理shell中并行工作,想要键入ls
而不是dir
或mv
而不是move
。因此,我创建了一些单行脚本,例如
ls.bat
,其中包含dir %1
的内容,并创建链接ls
for /F "tokens=*" %%g in (file.txt) do (%%g)
这种方法有一些限制(不要使用::
作为注释),但它非常适合配置文件。
例如:
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
.cmd
文件... - Greg Hewgill