The best option is to rename the file to .bat or .cmd (or make a (temporary) copy).
I for some reason it just has to be a .txt file, your options are very limited.
Besides Gerhards suggestion (the one with the for loop), a maybe better option is to "feed" the lines of the textfile to cmd (not recommended; provided just for academic reasons):
<script.txt %comspec%
or without command repetition and header:
<script.txt %comspec% /q /k
NOTES:
- the lines in the script.txt have to be command-line syntax (for example
for %a ..., not batch-file syntax for %%a ...) GOTO and CALL :label won't work (they don't work on command-line) - a "multiline"
for command works, but will clutter your output with a More? prompt per line (not suppressable). - if delayed expansion is needed, you need to add the
/v:on switch to the cmd command (setlocal * has no effect on the command line)
Example-script.txt:
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.
Output:
hello Stephan Antwort von 172.217.21.206: Bytes=32 Zeit=12ms TTL=121 Mehr? Mehr? alpha beta done.
file.txttofile.bat?