|
1 | | -Function GetFileVersionAsString(ByRef strFileVersion, ByVal strFilePath) |
2 | | - 'region FunctionMetadata #################################################### |
3 | | - ' Safely obtains the file version of a binary file. This is the "file version" displayed in |
4 | | - ' the properties of the file, details tab, when viewed from Windows Explorer. |
5 | | - ' |
6 | | - ' Function takes two positional arguments: |
7 | | - ' The first argument (strFileVersion) will be the string representation of the |
8 | | - ' file's version (whose path is strFilePath). |
9 | | - ' The second argument (strFilePath) is the path to the file for which we want to know the |
10 | | - ' file version. |
11 | | - ' |
12 | | - ' The function returns 0 if the file's version was retrieved successfully. A negative |
13 | | - ' number is returned if the file's product version was not retrieved successfully. |
14 | | - ' |
15 | | - ' Example: |
16 | | - ' strFilePath = "C:\Windows\System32\hal.dll" |
17 | | - ' intReturnCode = GetFileVersionAsString(strFileVersion, strFilePath) |
18 | | - ' If intReturnCode = 0 Then |
19 | | - ' ' The product version of hal.dll was retrieved successfully and is stored in |
20 | | - ' ' strFileVersion in string format. |
21 | | - ' End If |
22 | | - ' |
23 | | - ' Note: the technique used in this function requires Windows Scripting Host 2.0 or newer, |
24 | | - ' which was included in Windows releases beginning with Windows 2000 and Windows ME. It was |
25 | | - ' available as a separate download for Windows 95, 98, and NT 4.0. |
26 | | - ' |
27 | | - ' Version: 1.0.20210614.0 |
28 | | - 'endregion FunctionMetadata #################################################### |
29 | | - |
30 | | - 'region License #################################################### |
31 | | - ' Copyright 2021 Frank Lesniak |
32 | | - ' |
33 | | - ' Permission is hereby granted, free of charge, to any person obtaining a copy of this |
34 | | - ' software and associated documentation files (the "Software"), to deal in the Software |
35 | | - ' without restriction, including without limitation the rights to use, copy, modify, merge, |
36 | | - ' publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
37 | | - ' persons to whom the Software is furnished to do so, subject to the following conditions: |
38 | | - ' |
39 | | - ' The above copyright notice and this permission notice shall be included in all copies or |
40 | | - ' substantial portions of the Software. |
41 | | - ' |
42 | | - ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
43 | | - ' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
44 | | - ' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
45 | | - ' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
46 | | - ' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
47 | | - ' DEALINGS IN THE SOFTWARE. |
48 | | - 'endregion License #################################################### |
49 | | - |
50 | | - 'region DownloadLocationNotice #################################################### |
51 | | - ' The most up-to-date version of this script can be found on the author's GitHub repository |
52 | | - ' at https://github.com/franklesniak/VBScript_Resources |
53 | | - 'endregion DownloadLocationNotice #################################################### |
54 | | - |
55 | | - 'region Acknowledgements #################################################### |
56 | | - ' Andrew Clinick, for his article "If It Moves, Script It" (available in the MSDN library |
57 | | - ' published 2003 Jan), which tipped me off that FileSystemObject is available starting in |
58 | | - ' Windows Scripting Host 2.0. |
59 | | - ' |
60 | | - ' Jerry Lee Ford, Jr., for providing a history of VBScript and Windows Scripting Host in |
61 | | - ' his book, "Microsoft WSH and VBScript Programming for the Absolute Beginner". |
62 | | - ' |
63 | | - ' Gunter Born, for providing a history of Windows Scripting Host in his book "Microsoft |
64 | | - ' Windows Script Host 2.0 Developer's Guide" that corrected some points. |
65 | | - ' |
66 | | - ' The Microsoft "Scripting Guys", who published sample code to obtain a file's regular |
67 | | - ' "file version" using FileSystemObject: |
68 | | - ' https://devblogs.microsoft.com/scripting/how-can-i-determine-the-version-number-of-a-file/ |
69 | | - 'endregion Acknowledgements #################################################### |
70 | | - |
71 | | - 'region DependsOn #################################################### |
72 | | - ' TestObjectForData() |
73 | | - 'endregion DependsOn #################################################### |
74 | | - |
75 | | - Dim intFunctionReturn |
76 | | - Dim objFileSystemObject |
77 | | - Dim boolResult |
78 | | - Dim strWorkingFileVersion |
79 | | - |
80 | | - Err.Clear |
81 | | - |
82 | | - intFunctionReturn = 0 |
83 | | - |
84 | | - On Error Resume Next |
85 | | - Set objFileSystemObject = CreateObject("Scripting.FileSystemObject") |
86 | | - If Err Then |
87 | | - On Error Goto 0 |
88 | | - Err.Clear |
89 | | - intFunctionReturn = -1 |
90 | | - Else |
91 | | - boolResult = objFileSystemObject.FileExists(strFilePath) |
92 | | - If Err Then |
93 | | - On Error Goto 0 |
94 | | - Err.Clear |
95 | | - intFunctionReturn = -2 |
96 | | - Else |
97 | | - On Error Goto 0 |
98 | | - If boolResult = False Then |
99 | | - intFunctionReturn = -3 |
100 | | - Else |
101 | | - On Error Resume Next |
102 | | - strWorkingFileVersion = objFileSystemObject.GetFileVersion(strFilePath) |
103 | | - If Err Then |
104 | | - On Error Goto 0 |
105 | | - Err.Clear |
106 | | - intFunctionReturn = -4 |
107 | | - Else |
108 | | - On Error Goto 0 |
109 | | - If TestObjectForData(strWorkingFileVersion) = False Then |
110 | | - intFunctionReturn = -5 |
111 | | - End If |
112 | | - End If |
113 | | - End If |
114 | | - End If |
115 | | - End If |
116 | | - |
117 | | - If intFunctionReturn = 0 Then |
118 | | - strFileVersion = strWorkingFileVersion |
119 | | - End If |
120 | | - |
121 | | - GetFileVersionAsString = intFunctionReturn |
122 | | -End Function |
| 1 | +Function GetFileVersionAsString(ByRef strFileVersion, ByVal strFilePath) |
| 2 | + 'region FunctionMetadata #################################################### |
| 3 | + ' Safely obtains the file version of a binary file. This is the "file version" displayed in |
| 4 | + ' the properties of the file, details tab, when viewed from Windows Explorer. |
| 5 | + ' |
| 6 | + ' Function takes two positional arguments: |
| 7 | + ' The first argument (strFileVersion) will be the string representation of the |
| 8 | + ' file's version (whose path is strFilePath). |
| 9 | + ' The second argument (strFilePath) is the path to the file for which we want to know the |
| 10 | + ' file version. |
| 11 | + ' |
| 12 | + ' The function returns 0 if the file's version was retrieved successfully. A negative |
| 13 | + ' number is returned if the file's product version was not retrieved successfully. |
| 14 | + ' |
| 15 | + ' Example: |
| 16 | + ' strFilePath = "C:\Windows\System32\hal.dll" |
| 17 | + ' intReturnCode = GetFileVersionAsString(strFileVersion, strFilePath) |
| 18 | + ' If intReturnCode = 0 Then |
| 19 | + ' ' The product version of hal.dll was retrieved successfully and is stored in |
| 20 | + ' ' strFileVersion in string format. |
| 21 | + ' End If |
| 22 | + ' |
| 23 | + ' Note: the technique used in this function requires Windows Scripting Host 2.0 or newer, |
| 24 | + ' which was included in Windows releases beginning with Windows 2000 and Windows ME. It was |
| 25 | + ' available as a separate download for Windows 95, 98, and NT 4.0. |
| 26 | + ' |
| 27 | + ' Version: 1.0.20210614.0 |
| 28 | + 'endregion FunctionMetadata #################################################### |
| 29 | + |
| 30 | + 'region License #################################################### |
| 31 | + ' Copyright 2021 Frank Lesniak |
| 32 | + ' |
| 33 | + ' Permission is hereby granted, free of charge, to any person obtaining a copy of this |
| 34 | + ' software and associated documentation files (the "Software"), to deal in the Software |
| 35 | + ' without restriction, including without limitation the rights to use, copy, modify, merge, |
| 36 | + ' publish, distribute, sublicense, and/or sell copies of the Software, and to permit |
| 37 | + ' persons to whom the Software is furnished to do so, subject to the following conditions: |
| 38 | + ' |
| 39 | + ' The above copyright notice and this permission notice shall be included in all copies or |
| 40 | + ' substantial portions of the Software. |
| 41 | + ' |
| 42 | + ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 43 | + ' INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR |
| 44 | + ' PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE |
| 45 | + ' FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 46 | + ' OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 47 | + ' DEALINGS IN THE SOFTWARE. |
| 48 | + 'endregion License #################################################### |
| 49 | + |
| 50 | + 'region DownloadLocationNotice #################################################### |
| 51 | + ' The most up-to-date version of this script can be found on the author's GitHub repository |
| 52 | + ' at https://github.com/franklesniak/VBScript_Resources |
| 53 | + 'endregion DownloadLocationNotice #################################################### |
| 54 | + |
| 55 | + 'region Acknowledgements #################################################### |
| 56 | + ' Andrew Clinick, for his article "If It Moves, Script It" (available in the MSDN library |
| 57 | + ' published 2003 Jan), which tipped me off that FileSystemObject is available starting in |
| 58 | + ' Windows Scripting Host 2.0. |
| 59 | + ' |
| 60 | + ' Jerry Lee Ford, Jr., for providing a history of VBScript and Windows Scripting Host in |
| 61 | + ' his book, "Microsoft WSH and VBScript Programming for the Absolute Beginner". |
| 62 | + ' |
| 63 | + ' Gunter Born, for providing a history of Windows Scripting Host in his book "Microsoft |
| 64 | + ' Windows Script Host 2.0 Developer's Guide" that corrected some points. |
| 65 | + ' |
| 66 | + ' The Microsoft "Scripting Guys", who published sample code to obtain a file's regular |
| 67 | + ' "file version" using FileSystemObject: |
| 68 | + ' https://devblogs.microsoft.com/scripting/how-can-i-determine-the-version-number-of-a-file/ |
| 69 | + 'endregion Acknowledgements #################################################### |
| 70 | + |
| 71 | + 'region DependsOn #################################################### |
| 72 | + ' TestObjectForData() |
| 73 | + 'endregion DependsOn #################################################### |
| 74 | + |
| 75 | + Dim intFunctionReturn |
| 76 | + Dim objFileSystemObject |
| 77 | + Dim boolResult |
| 78 | + Dim strWorkingFileVersion |
| 79 | + |
| 80 | + Err.Clear |
| 81 | + |
| 82 | + intFunctionReturn = 0 |
| 83 | + |
| 84 | + On Error Resume Next |
| 85 | + Set objFileSystemObject = CreateObject("Scripting.FileSystemObject") |
| 86 | + If Err Then |
| 87 | + On Error Goto 0 |
| 88 | + Err.Clear |
| 89 | + intFunctionReturn = -1 |
| 90 | + Else |
| 91 | + boolResult = objFileSystemObject.FileExists(strFilePath) |
| 92 | + If Err Then |
| 93 | + On Error Goto 0 |
| 94 | + Err.Clear |
| 95 | + intFunctionReturn = -2 |
| 96 | + Else |
| 97 | + On Error Goto 0 |
| 98 | + If boolResult = False Then |
| 99 | + intFunctionReturn = -3 |
| 100 | + Else |
| 101 | + On Error Resume Next |
| 102 | + strWorkingFileVersion = objFileSystemObject.GetFileVersion(strFilePath) |
| 103 | + If Err Then |
| 104 | + On Error Goto 0 |
| 105 | + Err.Clear |
| 106 | + intFunctionReturn = -4 |
| 107 | + Else |
| 108 | + On Error Goto 0 |
| 109 | + If TestObjectForData(strWorkingFileVersion) = False Then |
| 110 | + intFunctionReturn = -5 |
| 111 | + End If |
| 112 | + End If |
| 113 | + End If |
| 114 | + End If |
| 115 | + End If |
| 116 | + |
| 117 | + If intFunctionReturn = 0 Then |
| 118 | + strFileVersion = strWorkingFileVersion |
| 119 | + End If |
| 120 | + |
| 121 | + GetFileVersionAsString = intFunctionReturn |
| 122 | +End Function |
0 commit comments