<!--
  canonical-url: https://www.avonture.be/blog/vbs-auto-update
  published:     2024-03-19
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# VBS - Auto update script

> Automate updates for your VBS (Visual Basic Script) console scripts! Learn how to create a self-updating VBS file that connects to GitHub, checks for new versions, and overwrites itself.

---
![VBS - Auto update script](/img/v2/vbs.webp)

**TL;DR**

This article shows how to give a VBScript (VBS) file a self-update capability: on each run, the script downloads its own source from a public GitHub URL, compares it to the local copy, and overwrites itself if a newer version is found — letting distributed `.vbs` utilities stay current without manual redistribution.

Before switching to [WSL2](/blog/tags/wsl) and the Linux console, I wrote VBS scripts from time to time. It looks like VBA but for the DOS console.

A VBS script for DOS is a text file written in the Visual Basic Scripting Edition (VBScript) programming language that can be executed directly from the DOS command prompt. It allows you to automate tasks and perform repetitive operations on your computer.

It's just like [Linux Bash](/blog/tags/bash) scripts but for DOS.

*Three VBS utilities of mine that would benefit from exactly this self-update mechanism: [VBS - Retrieve the list of fields in a MS Access Database](/blog/vbs-msaccess-get-fields), [Export MS Access objects](/blog/vba-access-export) and [VBS - Get list of files and generate a CSV](/blog/vbs-files-csv).*

Do you think it would be possible to offer an auto-update function in such scripts? The answer is yes.

Imagine a script called `get_folder_size.vbs` you've publicly saved on GitHub (source <https://github.com/cavo789/vbs_utilities/blob/master/src/folders/get_folder_size/get_folder_size.vbs>).

Someone downloads it to their computer and enjoys using it.

By adding a new *auto-update* function to it, each time the script starts, a connection to GitHub is made first, the script is downloaded from there, and a check is made to see if the downloaded version is different; if so, the script overwrites itself.

Here is the content of such function:

**get\_folder\_size.vbs**

```vbs
' -----------------------------------------------------------------
'
' Connect to GitHub, get the raw version of a file and download it
'
' The script will check for newer version on GitHub and if
' there is one, the script will overwrite himself with
' that newer version.
'
' Based on a script of
' @author Michi Lehenauer §https://github.com/michiil)
' then modified by Christophe Avonture
'
' @src https://github.com/michiil/vbs_scrips/blob/master/WZV-Excel.vbs
' -----------------------------------------------------------------

' The content to download
url = "https://raw.githubusercontent.com/cavo789/vbs_utilities/master/src/folders/get_folder_size/get_folder_size.vbs"

Set req = CreateObject("Msxml2.ServerXMLHttp.6.0")

req.setTimeouts 500, 500, 500, 500

' If you're behind a firewall, uncomment the following line
' and mention the proxy address and port
'req.setProxy 2, "your.proxy.net:8080", ""

req.open "GET", url, False
req.send

If Err.Number = 0 Then
  If req.Status = 200 Then
    ' Get the content, just downloaded
    downloadedContent = req.responseText

    ' Get the original content, this script

    scriptName = wScript.ScriptFullName
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set textFile = objFSO.OpenTextFile(scriptName, 1)
    originalContent = textFile.ReadAll
    textFile.Close

    ' Compare if the two contents are different
    If (originalContent <> downloadedContent) Then
      ' If yes, for instance, rewrite this script by
      ' the new content ==> auto-update
      Set textFile = objFSO.OpenTextFile(scriptName, 2)

      textFile.Write (downloadedContent)
      textFile.Close

      'wScript.echo scriptName & " has been updated"
    End If

    Set textFile = Nothing
    Set objFSO = Nothing

  End If
Else
    ' Ok, in case of error, don't panic, just do nothing
    Err.Clear
End If
```
