Search code examples
windowsvisual-studio-codemkdir

How to create a directory in Visual Studio Code tasks.json in windows?


I'm trying to write a task to create a directory using Visual studio Code tasks (in tasks.json) for windows users using the mkdir command, it's working well except when the folder already exists.

tasks.json

{
    "label": "(release) create build directory",
    "type": "shell",
    "linux": {
        "command": "mkdir -p ./build/release"
    },
    "windows": {
        "command": "mkdir .\\build\\release", // Not working when folder already exists !
    }
},

What I tried:
"command": "IF NOT EXIST .\\build\\release mkdir .\\build\\release"
But then I get the error:

At line:1 char:3
+ IF NOT EXIST .\build\release mkdir .\build\release
+   ~
Missing '(' after 'IF' in if statement.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingOpenParenthesisInIfStatement

If it is not possible to do it this way, is it possible to run this task by ignoring the exit code ? ( so that the tasks continue to build my project)

Environment:
Visual Studio code 1.40.2
Windows 10 Pro x64


Solution

  • I found a way of doing it by using cmd.exe with /C option (Run Command and then terminate)

    {
        "label": "(release) create build directory",
        "type": "shell",
        "linux": {
            "command": "mkdir -p ./build/release"
        },
        "windows": {
            "command": "cmd",
            "args": ["/C", "if not exist .\\build\\release mkdir .\\build\\release"]
        }
    },