Follow Me On...

Entries in VBS (1)


VBS Script Code for File Versions

I had written build scripts for various products and the end result is typically some archive file (zip,msi,iso). My build scripts would create a file such as but i wanted something todo

That’s what the script below does.

' Author: Blake Robertson (10-21-06)
' This script gets the file version of a particular file.  
' It then renames (or copies) the file with a versioned suffix.
' Example: say test.EXE had a version of and based on that you wanted
' to rename to testPackage-v2.1.95.0"
' To do so, then set:
'    versionedFilePath = "test.exe"
'    nonversionedFilename = ""
' REQUIREMENTS: the original filename cannot have more then one '.' in it.' 
Dim versionedFilePath, nonversionedFilename, seperator

versionedFilePath = "c:\path\to\exe"
nonversionedFilename = "c:\path\to\nonversioned\"
seperator = "-v"

Dim verStr, versionedFileName, filenameArr
Set FS = CreateObject("Scripting.FileSystemObject")
verStr = FS.GetFileVersion( versionedFilePath )

' Seperates filename from file extention
filenameArr = Split( nonversionedFilename, "." )

versionedFileName = filenameArr(0) & seperator & verStr & "." & filenameArr(1)

' Renames the Original File, if copy is desired then uncomment 2nd line and comment first
'FS.MoveFile nonVersionedFileName, versionedFileName
FS.CopyFile nonVersionedFileName, versionedFileName

If your project doesn’t create versioned executable files but you can obtain information from registry key or from a file, then replace the verStr = FS.GetFileVersion(…) line above with one of the snippets below:

Read Version Info in from a registry key:

verStr = WshShell.RegRead("HKLM\SOFTWARE\Your Company\Your Product\Version")

Snippet to read version info from a text file.

Set FS = CreateObject(“Scripting.FileSystemObject”)
verStr = FS.OpenTextFile(“version.txt”).ReadAll

Tip for SVN Users

If you are using SVN or some file versioning system, you can get version info by calling the update command twice. The second time redirect the output to a txt file. The second consecutive call to update will not update any files so the output should be just an informative version note.
svn update myCodeDir 
svn update myCodeDir > version.txt
type version.txt
At revision 342.