1

I am trying to convert my bash script into a Windows batch file. It's a really simple one liner that's supposed to feed the contents of a file as arguments to script.exe, and send the results to output.txt.

This is the working bash script:

./script.exe $(cat input.txt) > output.txt

I know this might be bad style, but it works. The problem is, I have no idea how to do something like $() in a windows batch file. When I use it it sends the string "$(cat input.txt)" as the argument instead of running the command.

mrFoobles
  • 149
  • 2
  • 9
  • 1
    If the unknown windows script.exe accepts redirected input, either `type input.txt|script.exe >output.txt` or `script.exe output.txt` otherwise you can have bash through wsl or a similar syntax with powershell. –  Nov 11 '18 at 23:28

1 Answers1

3

This bash construct is called command substitution. Here is a great answer from @MichaelBurr.

You can get a similar functionality using cmd.exe scripts with the for /f command:

for /f "usebackq tokens=*" %%a in (`echo Test`) do my_command %%a

Yeah, it's kinda non-obvious (to say the least), but it's what's there.

See for /? for the gory details.

Sidenote: I thought that to use "echo" inside the backticks in a "for /f" command would need to be done using "cmd.exe /c echo Test" since echo is an internal command to cmd.exe, but it works in the more natural way. Windows batch scripts always surprise me somehow (but not usually in a good way).

See also, on Superuser: Is there something like Command Substitution in WIndows CLI?

Mathieu CAROFF
  • 1,230
  • 13
  • 19