Search code examples
visual-studio-codevscode-extensions

VSCode API check if path exists


In a VSCode extension, how can I check if a file path exists without opening or reading the file?

The closest thing I've found are vscode.workspace.fs.Readfile and vscode.workspace.openTextDocument (https://code.visualstudio.com/api/references/vscode-api#FileSystem), but again, I do not want to open or read the file. Is there a way to do this?


Solution

  • There is no API for that because there is a builtin function:

    import * as fs from 'fs'; // In NodeJS: 'const fs = require('fs')'
    if (fs.existsSync(path)) {
      // File exists in path
    }