使用 VBScript 查找和替换 JSON 文件中节点的大括号之间的所有多行文本 [英] Using VBScript to find and replace all multi-line text between curly braces of a node within a JSON file

查看:31
本文介绍了使用 VBScript 查找和替换 JSON 文件中节点的大括号之间的所有多行文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 打开一个 JSON 文件 (%userprofile%\Local Settings\Application Data\Google\Chrome\User Data\Default\Preferences) 并将内容读取为字符串;
  2. 搜索名为download"的特定节点,该节点预先填充了可能因构建而异的多行值;
  3. 用指定的多行文本替换大括号之间的整个文本;和
  4. 将更新后的字符串写入原始文件并保存.

完整的 JSON 文件相当大,但作为用作输入的示例,这是从典型的 Google Chrome 首选项 JSON 文件复制的:

"bookmark_bar": {show_on_all_tabs":假},下载": {directory_upgrade":真,prompt_for_download":假},同步": {suppress_start":真},

我想以编程方式搜索下载"节点,并替换此节点大括号之间的所有内容,使其显示为:

下载":{"default_directory": "C:\\Windows\\Temp","extensions_to_open": "pdf",prompt_for_download":假},

...文件的其余内容保持不变.

鉴于要替换的 JSON 部分中的空格和多行,以及在大括号之间包含所有/任何文本的通配符要求,我无法使用 VBScript Replace 函数执行此操作,但是我的 RegEx知识有限.

解决方案

可以用正则表达式替换:

prefsFile = "%userprofile%\Local Settings\...\Preferences"prefsFile = CreateObject("WScript.Shell").ExpandEnvironmentStrings(prefsFile)newPrefs = "..."Set fso = CreateObject("Scripting.FileSystemObject")json = fso.OpenTextFile(prefsFile).ReadAll设置 re = 新正则表达式re.Pattern = """download"": {[\s\S]*?},"json = re.Replace(json, """download"": {" & vbCrLf & newPrefs & vbCrLf & "},")fso.OpenTextFile(prefsFile, 2).Write(json)

模式 [\s\S] 匹配任何空白或非空白字符.在这种情况下,您不能使用 .,因为该特殊字符不匹配换行符,并且您希望表达式跨越多行.限定符 *? 分别表示匹配任意数量的字符"和使用最短匹配".这样,表达式匹配 "download": 关键字后一对大括号之间的所有内容.

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 Settings\Application Data\Google\Chrome\User Data\Default\Preferences) 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.

解决方案

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.

这篇关于使用 VBScript 查找和替换 JSON 文件中节点的大括号之间的所有多行文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆