I need a batch file that receives two parameters: a command (filename with some arguments) and a path to a file - for output redirection of the command execution. I tried some variants like call %1 > %2, start %1 > %2 in file 'q.cmd', with calling as q "echo test" "log.txt" and q 'echo test' 'log.txt' and %1 or call %1 with q "echo test > log.txt" but nothing works. I also tried it with ^ before >. In most cases it prints " 'echo is not recognized as an internal or external commad..." or something like this and sometimes create empty 123.txt or with some service lines, but not with "test" as I need. How can I solve the problem?


q "echo test" "log.txt" would be feeding two parameters.

"echo test" would be by itself, and is no longer "Echo" followed by "test" as an argument to Echo, but instead a single command called "echo test", which is invalid.

You need to separate out the arguments from the commands, and really batch scripting is NOT the best way to do this, especially if you want a dynamic amount of arguments. Look into using PowerShell instead.

Having said that, if you want something simple, not so flexible, and is still a batch file, then perhaps try the following in your batch file: %2 %3 > %1.

Then just specify the output file first, followed by the command, followed by the argument you want to use with that command.

I.E.: q log.txt echo Test, or q log.txt echo "string with spaces to echo".

These work for me. :)

| improve this answer | |
  • At least you've clarified why my attempts were useless. But problem is that "command" can be a command or exe + several (variative) arguments. Is the way to make parameter be treated as command? – SerG Jul 7 '14 at 19:44
  • 1
    You can add %4 %5 %6 %7 %8 %9... before the < if you want to accept more arguments. But if you truly need a variable amount of arguments then batch is not the way to do it, look into using PowerShell instead, as PS scripts can be programmed to accept named arguments, as well as an arbitrary amount of arguments. – Ƭᴇcʜιᴇ007 Jul 7 '14 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.