Replace Revit 2020 INI %USERPROFILE% with user path VBS (Pairs with CMD Cache update file)
Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''This program looks at the Revit INI and 'escapes'
''or replaces the %USERPROFILE% with the actual path under the executed profile
''Run-as the user to make the replacement in the path.
''Adapt this script to modify poritons of the INI using RegExp
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Const ShowMessages = True ''Set to false to turn of debug messages
''Source File Path less user location
Const sourceFP = "\AppData\Roaming\Autodesk\Revit\Autodesk Revit 2020\Revit.ini"
''TextStream options
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const TristateTrue = -1
Const TristateFalse = 0
Const TristateUseDefault = -2
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
MAIN ''Execute MAIN
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub MAIN() ''Main sub entry point
Dim objRE 'AS New RegExp ''Regular Expressions
Dim FSO 'As FileSystemObject ''File system object for reading files
Dim TS 'AS TextStream ''Text stream for reading file
Dim strTxtFile 'AS String ''Read entire file
Dim strUsrProf 'AS String ''UserProfile Path
Dim FP 'AS String ''File path- direct for this script
''initialize objects
Set objRE = CreateObject("VBScript.RegExp")
Set FSO = CreateObject("Scripting.FileSystemObject")
''Get USer Profile from WScript Shell
strUsrProf = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%UserProfile%")
''In cases wher eescaping replacement value is required. I.e. $1 $2 values incorporated
''Prep fpor Regex epscial character escapes '\'
'strUsrProf = Replace(strUsrProf, "\", "\\", , , vbTextCompare)
''Prep fpor Regex epscial character escapes ':'
'strUsrProf = Replace(strUsrProf, ":", "\:", , , vbTextCompare)
''read text file - swal %USER% with loggedin user profile
FP = strUsrProf & sourceFP ''filepath
Set TS = FSO.OpenTextFile(FP, ForReading, False, TristateTrue) ''text stream open
strTxtFile = TS.ReadAll ''read entire TXT/INI file
''Regular Expression to find/replace
With objRE
.Global = True ''Whole string including after returns/LFs
.Pattern = "\%USERPROFILE\%" ''String Sought
.IgnoreCase = True ''Ignore case
.MultiLine = True ''searc and replace all multilines
If .test(strTxtFile) = False Then ''If not found exit without doing anything
MsgBox "Not found, nothing done", vbInformation + vbOKOnly
Exit Sub
End If
strTxtFile = .Replace(strTxtFile, strUsrProf) ''Replace and set new strUsrProf
End With
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''CREATE NEW INI from old text'''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim DateStart 'AS Date ''compare start date to see if modified
Dim DateEnd 'AS Date ''compare end date to see if modified
DateStart = FSO.GetFile(FP).DateLastModified
TS.Close
Set TS = Nothing
FSO.DeleteFile (FP) ''delete existing file
Set TS = FSO.OpenTextFile(FP, ForWriting, True, TristateTrue) ''text stream open
TS.Write strTxtFile ''Re-write file
TS.Close ''close file
DateEnd = FSO.GetFile(FP).DateLastModified
If DateStart < DateEnd Then MsgBox "Success - updated" & DateEnd, vbOKOnly, "Update complete"
End Sub
Comments
Post a Comment