opening vscode to latest directory in specified path

Viewed 213

Is it possible to have vscode automatically open the newest folder within a specific path?

For example, with this configuration:

{
    "folders": [
        {
            "path": "\\\\FromABC\\Archive",
            "name":"From ABC"
        },
        {
            "path": "\\\\FromXYZ\\Archive",
            "name":"From XYZ"
        }
    ]
}

I would expect these folders in the workspace to be pointing to \07\07 because those were created today:

\\\\FromABC\\Archive\\2021\\07\\07
\\\\FromXYZ\\Archive\\2021\\07\\07

Is it possible to create a workspace where the folders are opened to the latest folder within each configured path?

2 Answers

There's not enough information in the original question to fully answer it, however, I can suggest a few avenues of attack

  • Custom Command (error-prone and picky)
  • Modify Upstream Process (likely the best overall)
  • Combining Both (perhaps the best for your immediate case)

Creating a Custom Command

Create a new command per https://code.visualstudio.com/api/extension-guides/command#creating-new-commands

VSCode Commands Listing: https://code.visualstudio.com/api/references/commands

  • new command
    • detect latest folder through whatever logic you like
    • call vscode.openFolder to navigate to it
  • call your custom command through Activation Events (activationEvents) at either onStartupFinished or * (Start Up; less-preferable, but may be required to avoid confusing the editor)
    https://code.visualstudio.com/api/references/activation-events#Start-up

Check out Start app when opening project in VS Code? for a few answers related to this


Modifying the Upstream Process

Cutting the gordian knot, it's likely some process (perhaps a human) is creating the directories for you

Change the upstream process so when it creates the directories, it also creates/updates a link to the directory labeled something like latest

/FromABC/Archive/2021/06/03
/FromABC/Archive/2021/07/05
/FromABC/Archive/2021/07/07
/FromABC/Archive/latest  --> /FromABC/Archive/2021/07/07
/FromXYZ/Archive/2020/04/12
/FromXYZ/Archive/2021/08/18
/FromXYZ/Archive/latest  --> /FromXYZ/Archive/2021/08/18

Then you can always refer to the latest directory and it will always be correct

This is quite common when something can change frequently, but another process is expecting a static path and/or has no way to know the schedule of change

I don't have any Windows systems to try this out with and you may be able to create a regular shortcut for this .. however, you may need a Junction (soft-link) or Hard Link to convince VSCode that the directory is a real directory
https://docs.microsoft.com/en-us/windows/win32/fileio/hard-links-and-junctions

This also provides an opportunity to include more files, such a beta versions of some software, which it's desirable to package into the same directory structure, but not truly the latest stable!


Combining Both

If your upstream process is either not modifiable (or some manual process it's annoying or error-prone to add extras steps to) you can likely combine both solutions to get what you really want

Use the * Action Event to call a script to detect and create the new directory - create a binary or PowerShell script to make your link

In this and with the upstream change, just point VS Code to the latest directory and it shouldn't mind

Not sure on which platform you are, I assume windows, but essentially similar.

Instead of trying to get VSCode to open the latest folder, I would create a script that updates a softlink for each folder to the latest subfolder in it. Then you can point VSCode to the softlink, which can be updated whenever needed to the latest subfolders.

Related