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?
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`);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With