How to redirect the output of a batch script to Visual Studio Code's integrated terminal?

69 Views Asked by At

I have a Visual Studio Code task that executes a batch script using the following code:

{
   "version": "2.0.0",
   "tasks": [
      {
         "label": "Run batch file",
         "type": "shell",
         "windows": {
            "command": "${workspaceFolder}\\run_script.bat",
            "args": [
               "arg1",
               "arg2"
            ]
         },
         "presentation": {
            "focus": true,
            "clear": true,
            "panel": "shared"
         }
      }
   ]
}

When I run the task, a new console is opened, output appears for a brief second, and then the console closes.

How to redirect the output to visual studio code's integrated terminal without opening any extra consoles?

UPDATE 1

Here's my vs code settings.json:

{
    "http.proxy": "[A URL I REMOVED ONLY FOR THIS POST]",
    "window.zoomLevel": 1,
    "files.autoSave": "afterDelay",
    "explorer.confirmDelete": false,
    "git.enableSmartCommit": true,
    "git.confirmSync": false,
    "workbench.startupEditor": "none",
    "diffEditor.ignoreTrimWhitespace": false,
    "editor.accessibilitySupport": "off",
    "git.ignoreRebaseWarning": true,
    "autopep8.args": [
        "--ignore=E501"
    ],
    "diffEditor.renderSideBySide": false
}

UPDATE 2

I was experimenting and other batch files outputs are shown in the integrated terminal. Even output of echo commands in the 'run_script.bat' file. So the problem is particular to one specific line in my batch file:

start /w "" "%path_to_exe%" /arg1 /arg2

I think this is the line opening a separate console and outputting there. How do I redirect its output to vs code's integrated terminal?

Thanks.

1

There are 1 best solutions below

0
Andrew.M On

As I wrote in my update, the issue had nothing to do with how the task was configured. It was with the line start /w "" "%path_to_exe%" /arg1 /arg2 of my batch script.

After reading the documentation of the start command, I added the /B option.

/B Start application without creating a new window. In this case Ctrl-C will be ignored - leaving Ctrl-Break as the only way to interrupt the application.

The line was now:

start /b /w "" "%path_to_exe%" /arg1 /arg2

and the output was automatically redirected to VS Code's integrated terminal.