Suppose I generate such a script dynamically in a program of mine and hold it in a C++ string variable. How can I run it? Do I have to save it to a file first and then instruct cmd.exe to run it?
I know you can pipe commands into cmd.exe like this:
type script.bat | cmd
But the effect of that is not to run script.bat . Instead, windows starts a new cmd.exe instance, prints the logo and waits for commands which it echos to the screen before executing them. For instance, try
echo echo hello | cmd
Thus every line in script.bat is handled as an independent command, just as if you entered it on the command line (which is what cmd.exe thinks you are doing!). That's no good. You can't run general scripts like that. You may have loops and branches spanning several lines. So what can I do?
I know you can put several commands on one line like this:
cmd /C "echo hello & echo world"
But can every batch script somehow and easily be changed to a oneliner? If the script was:
@echo off
echo hello
echo world
I could replace it by the onliner above, but if it contains a for loop spanning several lines with brackets?
So the title question could be reformulated to: Can every batch script be transformed to a oneliner? If so then how? And if not, is there any good solution to the problem? Can I somehow trick cmd.exe into thinking that it reads a batch script from a file, while it actually gets it from a program of mine?
Edit: An interesting quirk is to do:
echo echo hello | cmd /C "for /f "tokens=*" %a in ('more') do @%a"
But again every line is interpreted as an independent command.