What Visual Studio Code extension API should be used to add folders and move files in a Visual Studio Code workspace?

Viewed 548

I want to write an extension that will:

  • Read the folders and files in the current workspace
  • Reorganise those files by creating new folders and moving the files into them

So I need an API to do the above. I am not clear on whether I should use

I read the answer here, but it doesn't say what to use to create folders and move files within the workspace.

2 Answers

The FileSystemProvider is to be used if you want to serve files from non-local storage like FTP sites or virtual file systems inside remote devices and present them to Visual Studio Code as storage directories. The FileSystemProvider is a view/controller of the remote storage. In other words, you have to implement all the file manipulation operations by communicating with the remote storage.

If you want to just manipulate the files in the current workspace and also be able to use URI's from FileSystemProviders, you use vscode.workspace.fs.

You can also use the Node.js fs module, but that only handles local disk workspaces (URI's with scheme file:). I recommend to use the synchronous versions of the fs methods. I had some troubles using the asynchronous fs methods in Visual Studio Code (I did not know of vscode.workspace.fs at that time).

I found extension.ts, sample code for implementing FileSystemProvider provided by Microsoft.
The below steps are provided which helps you understand how to create a new folder (createDirectory) and move files within the workspace (using the copy command, copying all files in an old folder to a new folder and use the delete command if you don’t wish for files to remain in the old folder).

  1. The first step in FileSystemProvider is to register the filesystem provider for a given scheme using registerFileSystemProvider:
     const memFs = new MemFS();

     context.subscriptions.push(
              vscode.workspace.registerFileSystemProvider('memfs', memFs, {
                    isCaseSensitive: true
        }));

  1. Next is to register the command registerCommand to perform your operations in FileSystemProvider like readDirectory, readFile, createDirectory, copy, delete.
context.subscriptions.push(
              vscode.commands.registerCommand('memfs.init', _ =>
             {
               #TODO: add required functionalities

          }));
  • Read folders and read files
      for (const [name] of memFs.readDirectory(vscode.Uri.parse('memfs:/'))) {
                    memFs.readFile(vscode.Uri.parse(`memfs:/${name}`));
                }
  • Create the new folder
memFs.createDirectory(vscode.Uri.parse(`memfs:/folder/`));

  • Move files to newly created folder. Unfortunately there doesn't seem to be a separate move file command, but you can use the Copy command to copy to the new folder and then delete:

    COPY copy(source: Uri, destination: Uri, options: {overwrite: boolean})

    DELETE

     context.subscriptions.push(vscode.commands.registerCommand('memfs.reset', _ => {
               for (const [name] of memFs.readDirectory(vscode.Uri.parse('memfs:/'))) {
                    memFs.delete(vscode.Uri.parse(`memfs:/${name}`));
               }

            }));

Related