Visual studio code(Windows版)はインストーラーでインストールする方法が一般的です。
しかし、自分仕様にカスタマイズしたVisual studio codeを別PCでも使用する場合を考えたとき、zipファイルを展開したポータブル版を使ったほうが、SSD上で実行もできるようになり、バックアップも容易などいいことずくめです。
ただし、zip展開でインストールした場合はアップデートの度にzipをダウンロードして展開し、Visual studio codeのインストールフォルダに手動でコピーしなくてはいけません。Visual studio codeは頻繁にアップデートがあるので、手動アップデートは結構面倒です。
そこで、ダブルクリックのみでアップデートバッチとPowershellスクリプトを作成し、スケジューラーで定期的にアップデートできるようにしました。そのバッチとPowershellスクリプトが以下になります。
バッチファイルはダブルクリックでアップデートできるようにするためのWrapperになります。Visual studio codeが起動中の場合やアップデートファイルのダウンロードに失敗した場合はアップデート処理を行わないようにしています。
このバッチを定時で起動したり、startupにショートカットを置いて起動時にアップデートをかけたりすることで、手間なく最新版のVisual studio codeが使用できるようになります。
参考になれば幸いです。
@echo off
SET CURRENT_DIR_VSCODE_UPDATE_BAT=%~dp0
cd /d "%CURRENT_DIR_VSCODE_UPDATE_BAT%"
SET BATCH_NAME_VSCODE_UPDATE_BAT=%~n0%~x0
echo [%BATCH_NAME_VSCODE_UPDATE_BAT%] powershell -NoProfile -ExecutionPolicy Bypass ".\VsCodeUpdate.ps1"
CALL powershell -NoProfile -ExecutionPolicy Bypass ".\VsCodeUpdate.ps1"
[string]$script:script_path = Split-Path -Parent $MyInvocation.MyCommand.Path
[string]$script:script_name = Split-Path -Leaf $PSCommandPath
[string]$script:zip_name_body = "updater"
[string]$script:zip_name = $script:zip_name_body +".zip"
[string]$script:zip_path = $script:script_path + "\" + $script:zip_name
try {
Get-Process -Name Code -ErrorAction Stop > $null
Write-Host "[$script:script_name] Error : Close VSCode before updating"
Start-Sleep -m 5000
exit
}
catch {
# do nothing
}
# Remove temp file from portable user data
Remove-Item -Recurse -Force -Path "data/user-data" -Include @("Backups", "Cache", "CachedData", "GPUCache", "logs")
# Download latest stable build
curl.exe -L "https://code.visualstudio.com/sha/download?build=stable&os=win32-x64-archive" -o $script:zip_path
if( Test-Path($script:zip_path) ){
try{
# Unzip it
[string]$extract_folder = $script:zip_path.Substring(0, $script:zip_path.LastIndexOf('.'))
if( Test-Path($extract_folder) ){
Remove-Item -Recurse -Force -Path $extract_folder
}
Expand-Archive -Path $script:zip_path -DestinationPath $extract_folder
try {
Get-Process -Name Code -ErrorAction Stop > $null
Write-Host "[$script:script_name] Error : Close VSCode before updating"
# Delete downloaded package
Remove-Item -Path $script:zip_path
if( Test-Path($extract_folder) ){
Remove-Item -Recurse -Force -Path $extract_folder
}
Start-Sleep -m 5000
exit
}
catch {
# do nothing
}
# Delete anything except user data, update script and downloaded zip file
Get-ChildItem -Exclude @("data", "VSCodeUpdate.*", "ContextMenu_Setting.reg", $script:zip_name, $script:zip_name_body ) | Remove-Item -Recurse -Force
Copy-Item -Path ( $extract_folder + "\*" ) -Destination $script:script_path -Recurse -Force
} catch {
Write-Host "[$script:script_name] Forbidden route : Fail to extract"
Start-Sleep -m 5000
}
# Delete downloaded package
Remove-Item -Path $script:zip_path
if( Test-Path($extract_folder) ){
Remove-Item -Recurse -Force -Path $extract_folder
}
}※zipでインストールする場合、右クリックメニューに”VSCodeで開く”が表示されません。これを表示させるための.regファイルが以下になります。”D:¥Tools¥VSCode”にインストールされていることが前提になります。
Windows Registry Editor Version 5.00
; --------------------------------------------------------
; 1. ファイルを右クリックした場合 (*\shell)
; --------------------------------------------------------
[HKEY_CLASSES_ROOT\*\shell\VSCode]
@="Open w&ith Code"
"Icon"="D:\\Tools\\VSCode\\Code.exe"
[HKEY_CLASSES_ROOT\*\shell\VSCode\command]
@="\"D:\\Tools\\VSCode\\Code.exe\" \"%1\""
; --------------------------------------------------------
; 2. フォルダ自体を右クリックした場合 (Directory\shell)
; --------------------------------------------------------
[HKEY_CLASSES_ROOT\Directory\shell\VSCode]
@="Open w&ith Code"
"Icon"="D:\\Tools\\VSCode\\Code.exe"
[HKEY_CLASSES_ROOT\Directory\shell\VSCode\command]
@="\"D:\\Tools\\VSCode\\Code.exe\" \"%V\""
; --------------------------------------------------------
; 3. フォルダの背景(空白)を右クリックした場合 (Directory\Background\shell)
; --------------------------------------------------------
[HKEY_CLASSES_ROOT\Directory\Background\shell\VSCode]
@="Open w&ith Code"
"Icon"="D:\\Tools\\VSCode\\Code.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\VSCode\command]
@="\"D:\\Tools\\VSCode\\Code.exe\" \"%V\""