Batch file 我想使用批处理文件从文本文件执行几个cmd命令

Batch file 我想使用批处理文件从文本文件执行几个cmd命令,batch-file,Batch File,我用以下命令创建了一个txt文件 cd.. cd <Folder location> del *_extract.txt /Q cd。。 光盘 del*\u extract.txt/Q 现在,我想创建一个批处理文件,它将打开cmd并执行文本文件中提到的命令。 我创建的批处理文件包含以下内容 @echo off start "clear" cmd.exe -m <textfilelocation> @echo关闭 启动“清除”cmd.exe-m 最

我用以下命令创建了一个txt文件

cd..
cd <Folder location>
del *_extract.txt /Q
cd。。
光盘
del*\u extract.txt/Q
现在,我想创建一个批处理文件,它将打开cmd并执行文本文件中提到的命令。 我创建的批处理文件包含以下内容

@echo off
start "clear" cmd.exe -m <textfilelocation>
@echo关闭
启动“清除”cmd.exe-m

最简单的方法是斯蒂芬在评论中说的。将文件从
.txt
重命名为
.cmd
.bat
。然后您可以自己运行它,或者从其他批处理文件中调用它,如下所示:

call "C:\Some Location\file.cmd"
如果确实要保留txt文件,请运行for循环:

@echo off
for /f "usebackq delims=" %%i in ("C:\Some path\file.txt") do %%i
考虑到带有注释的文本文件,在这种情况下,使用
#
(这是一种非批处理文件标准),我们可以执行非注释行

@echo off
for /f "delims=" %%i in ('type "C:\Some Path\file.txt" ^| findstr /v "#"') do %%i

最好的选择是将文件重命名为
.bat
.cmd
(或制作(临时)副本)

由于某种原因,它必须是一个
.txt
文件,您的选项非常有限

除了Gerhard的建议(带有
for
循环的建议),可能更好的选择是将文本文件的行“馈送”到
cmd
(不推荐;仅出于学术原因提供):

输出:

hello Stephan
Antwort von 172.217.21.206: Bytes=32 Zeit=12ms TTL=121
Mehr? Mehr? alpha
beta
done.

你为什么不把你的
file.txt
重命名为
file.bat
<script.txt %comspec% /q /k
echo off
echo/
:start [ignored]
echo hello %username%
ping -n 1 www.google.com | find "TTL="
for %a in (alpha beta) do (
  echo %a
)
goto :start [ignored]
echo done.
hello Stephan
Antwort von 172.217.21.206: Bytes=32 Zeit=12ms TTL=121
Mehr? Mehr? alpha
beta
done.