Skip to content

Instantly share code, notes, and snippets.

@jbratu
Last active August 3, 2016 17:43
Show Gist options
  • Save jbratu/e928bd3b463d4bafa31fa610e23c1a55 to your computer and use it in GitHub Desktop.
Save jbratu/e928bd3b463d4bafa31fa610e23c1a55 to your computer and use it in GitHub Desktop.
Simple utility for OpenInsight to use the Universal Driver revparam file as a template to update all revparam files found in the path specified. Usage example: cscript UpdateRevparamFilesFromUD.vbs "C:\Revsoft\Universal Driver" "C:\Revsoft\OInsight94"
Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
If WScript.Arguments.Count = 0 OR WScript.Arguments.Count > 2 Then
'Wrong number of arguments. Show help and exit
WScript.Echo "Update revparam files. Use the revparam file in the source folder and update all found in destination path"
WScript.echo "Example: cscript.exe UpdateRevparamFilesFromUD.vbs ""C:\Revsoft\Universal Driver"" ""C:\Revsoft\OInsight94Eval"""
WScript.quit
End If
'Path from which the source revparam file should be read.
'i.e. c:\Revsoft\Universal Driver 5.0
UDFolder = Trim(WScript.Arguments(0))
FindFile = "revparam"
'Revparam files that could not be updated
Dim FailedUpdates(0)
'List of locations that could not be searched
Dim FailedSearch(0) : FailedSearch(0) = ""
'The path that should be searched for Revparam files and updates
DestBasePath = Trim(WScript.Arguments(1))
RevparamSrc = UDFolder & "\" & FindFile
If objFSO.FileExists(RevparamSrc) Then
WScript.echo "Using " & RevparamSrc & " to update all files found in " & DestBasePath
Else
Wscript.Echo "The file " & RevparamSrc & " does not exists"
WScript.quit
End If
'Update the revparam file (if found) in the base path.
updateFolder(DestBasePath)
'Search files and folders in path
ShowSubfolders objFSO.GetFolder(DestBasePath)
Dim CompletedWithErrors : CompletedWithErrors = False
If UBound(FailedUpdates) > 0 Or FailedUpdates(0) <> "" Then
WScript.echo ""
WScript.echo "!!!!!!!!!!!!WARNING!!!!!!!!!!!!!!!!!"
WScript.echo ""
WScript.echo "The following files failed to update:"
For Each File in FailedUpdates
WScript.echo File
Next
CompletedWithErrors = True
End If
If UBound(FailedSearch) > 0 Or FailedSearch(0) <> "" Then
WScript.echo ""
WScript.echo "!!!!!!!!!!!!WARNING!!!!!!!!!!!!!!!!!"
WScript.echo ""
WScript.echo "The following locations could not be searched:"
For Each File in FailedSearch
WScript.echo File
Next
CompletedWithErrors = True
End If
If CompletedWithErrors Then
WScript.Echo "Completed with errors."
Else
WScript.echo "Completed."
End If
'
' End Main Program
'
Sub ShowSubFolders(Folder)
On Error Resume Next
Err.Clear 'Clear any errors
For Each Subfolder in Folder.SubFolders
'Was there an error in the For Each Loop?
If Err.Number <> 0 Then
'There was an error for nexting the Subfolder
WScript.echo "Failed to search " + Folder
'All arrays must have at least 1 element, is this the first entry?
If FailedSearch(0) = "" Then
'Yes, use the first index
FailedSearch(0) = Folder
Else
'No, Add another index
ReDim Preserve FailedSearch(UBound(FailedSearch) + 1)
FailedSearch(UBound(FailedSearch)) = Folder
End If
Err.Clear
'Reset the error handling to crash on error
On Error Goto 0
'Skip this for each loop since it errored out
Exit For
End If
'Reset the error handling to crash on error
On Error Goto 0
'Show folder being searched
'Wscript.Echo Subfolder.Path
updateFolder(Subfolder.Path)
'Recursively search another subfolder in current path.
ShowSubFolders Subfolder
Next
End Sub
Sub updateFolder(Path)
Set objFolder = objFSO.GetFolder(Path)
Set colFiles = objFolder.Files
Dim RevparamFound : RevparamFound = False
WScript.echo "Searching path " & Path
On Error Resume Next
Err.Clear 'Clear any errors
For Each objFile in colFiles
'Was there an error in the For Each Loop?
If Err.Number <> 0 Then
'There was an error for nexting the objFile
WScript.echo "Failed to list files in " + Path
'All arrays must have at least 1 element, is this the first entry?
If FailedSearch(0) = "" Then
'Yes, use the first index
FailedSearch(0) = Path
Else
'No, Add another index
ReDim Preserve FailedSearch(UBound(FailedSearch) + 1)
FailedSearch(UBound(FailedSearch)) = Path
End If
Err.Clear
'Skip this for each loop since it errored out
Exit For
End If
'Reset the error handling to crash on error
On Error Goto 0
'Assume no revparam found
RevparamFound = False
Dim FullPath : FullPath = Path & "\" & objFile.Name
'WScript.echo "Checking " & FullPath & " against " & FindFile
if StrComp(objFile.Name, FindFile, vbTextCompare) = 0 Then
'Found match
Err.Clear 'Clear any errors
'Setup error handling to skip if CopyFile fails
On Error Resume Next
Call objFSO.CopyFile(RevparamSrc, objFile.Path, True)
If Err.Number <> 0 Then
'There was an error, log and skip
WScript.echo "Failed to update " + objFile.Path
'All arrays must have at least 1 element, is this the first entry?
If FailedUpdates(0) = "" Then
'Yes, use the first index
FailedUpdates(0) = FullPath
Else
'No, Add another index
ReDim Preserve FailedUpdates(UBound(FailedUpdates) + 1)
FailedUpdates(UBound(FailedUpdates)) = FullPath
End If
Err.Clear
Else
'No error
WScript.echo "Updated " + objFile.Path
RevparamFound = True
End If
'Reset the error handling to crash on error
On Error Goto 0
If RevparamFound Then
'No sense in searching rest of folder. Only 1 revparam file
'can exist in any one folder at a time.
'Break the for Each objFile loop
Exit For
End If
End If
Next 'For Each objFile in colFiles
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment