使用VBScript查找并替换JSON文件中节点花括号之间的所有多行文本

时间:2022-06-29 16:50:18

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.

作为Windows登录脚本(因此需要VBScript)的一部分,我希望在用户的谷歌Chrome首选项(存储在用户配置文件中的JSON文件中)中设置值,以便在用户登录时应用下载设置。

I'm trying to achieve the following:

我正在努力实现以下目标:

  1. Open a JSON file (%userprofile%\Local Settings\Application Data\Google\Chrome\User Data\Default\Preferences) and read the contents to a string;
  2. 打开一个JSON文件(%userprofile%\Local Settings %\ Application Data\谷歌\ \Chrome User Data\ \Default\ \ \ \ \ \Preferences),将内容读入一个字符串;
  3. Search for a particular node named "download", which by is pre-populated with multi-line values that may vary between builds;
  4. 搜索一个名为“download”的特定节点,该节点被预填充为多个行值,这些值在不同的构建中可能会有所不同;
  5. Replace the entire text between the braces with specified multi-line text; and
  6. 用指定的多行文本替换大括号之间的整个文本;和
  7. Write the updated string to the original file and save.
  8. 将更新后的字符串写入原始文件并保存。

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:

完整的JSON文件相当大,但作为输入示例,它是从典型的谷歌Chrome prefs JSON文件中复制的:

"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.

考虑到要替换的JSON部分中的空格和多行,以及在大括号中包含所有/任何文本的通配符要求,我不能使用VBScript替换函数来实现这一点,但我的RegEx知识是有限的。

1 个解决方案

#1


3  

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"": {[\s\S]*?},"

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

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

The pattern [\s\S] 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.

模式[\s\ s]匹配任何空格或非空白字符。你不能使用。在这种情况下,因为特殊字符不匹配换行符,所以您希望表达式跨多行。限定符*和?意思是“匹配任意数量的字符”和“使用最短的匹配”。这样,表达式在“下载”:关键字之后匹配一对花括号之间的所有内容。

#1


3  

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"": {[\s\S]*?},"

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

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

The pattern [\s\S] 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.

模式[\s\ s]匹配任何空格或非空白字符。你不能使用。在这种情况下,因为特殊字符不匹配换行符,所以您希望表达式跨多行。限定符*和?意思是“匹配任意数量的字符”和“使用最短的匹配”。这样,表达式在“下载”:关键字之后匹配一对花括号之间的所有内容。