Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 263
kindohm Avatar asked Dec 17 '22 15:12

kindohm


1 Answers

Per @realappie's comment on another answer, you can use vscode.workspace.fs.

It doesn't have a method like exists, but you can use .stat() for that. An example from the repo:

try {
    await vscode.workspace.fs.stat(jsUri);
    vscode.window.showTextDocument(jsUri, { viewColumn: vscode.ViewColumn.Beside });
} catch {
    vscode.window.showInformationMessage(`${jsUri.toString(true)} file does *not* exist`);
}
like image 96
nickf Avatar answered Dec 28 '22 10:12

nickf