Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.5k views
in Technique[技术] by (71.8m points)

regex - Using VBScript to find and replace all multi-line text between curly braces of a node within a JSON file

As part of a Windows logon script (hence the VBScript requirement), I would like to set values in a user's Google Chrome preferences (stored in a JSON file in the user profile) to apply download settings when they logon.

I'm trying to achieve the following:

  1. Open a JSON file (%userprofile%Local SettingsApplication DataGoogleChromeUser DataDefaultPreferences) and read the contents to a string;
  2. Search for a particular node named "download", which by is pre-populated with multi-line values that may vary between builds;
  3. Replace the entire text between the braces with specified multi-line text; and
  4. Write the updated string to the original file and save.

The full JSON file is fairly large, but as a sample to use as the input, this is copied from a typical Google Chrome prefs JSON file:

"bookmark_bar": {
    "show_on_all_tabs": false
},
"download": {
    "directory_upgrade": true,
    "prompt_for_download": false
},
"sync": {
    "suppress_start": true
},

I would like to programmatically search for the "download" node, and replace everything between the braces of just this node so that it reads:

"download": {
    "default_directory": "C:\Windows\Temp",
    "extensions_to_open": "pdf",
    "prompt_for_download": false
},

...with the rest of the file's contents unchanged.

Given the whitespace and multiple lines in the section of the JSON to be replaced, as well as the wildcard requirement to include all/any text between the braces, I can't do this using the VBScript Replace function, but my RegEx knowledge is limited.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can do the replacement with a regular expression:

prefsFile = "%userprofile%Local Settings...Preferences"
prefsFile = CreateObject("WScript.Shell").ExpandEnvironmentStrings(prefsFile)

newPrefs = "..."

Set fso = CreateObject("Scripting.FileSystemObject")

json = fso.OpenTextFile(prefsFile).ReadAll

Set re = New RegExp
re.Pattern = """download"": {[sS]*?},"

json = re.Replace(json, """download"": {" & vbCrLf & newPrefs & vbCrLf & "},")

fso.OpenTextFile(prefsFile, 2).Write(json)

The pattern [sS] matches any whitespace or non-whitespace character. You can't use . in this case, because that special character does not match newlines, and you want the expression to span multiple lines. The qualifiers * and ? mean "match any number of characters" and "use the shortest match" respectively. That way the expression matches everything between a pair of curly braces after the "download": keyword.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...