首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PowerShell -重命名对象数组中的重复文件名

PowerShell -重命名对象数组中的重复文件名
EN

Stack Overflow用户
提问于 2019-05-30 03:45:35
回答 1查看 131关注 0票数 0

如果我有像这样的JSON:

代码语言:javascript
复制
$inputJson = @"
{
    "attachments" :
        [
            {
                "name":  "attachment.eml",
                "attachment_url":  "https://www.attachment1.com"
            },
            {
                "name":  "attachment.eml",
                "attachment_url":  "https://www.attachment2.com"
            },
            {
                "name":  "attachment.eml",
                "attachment_url":  "https://www.attachment3.com"
            }
        ]
}

如果有一个attachments数组,并且数组中的每个元素都具有相同的名称但URL不同,我如何更改所有的名称以使其输出如下:

代码语言:javascript
复制
$outputJson = @"
{
    "attachments" :
        [
            {
                "name":  "(attachment.eml)[1].eml",
                "attachment_url":  "https://www.attachment1.com"
            },
            {
                "name":  "(attachment.eml)[2].eml",
                "attachment_url":  "https://www.attachment2.com"
            },
            {
                "name":  "(attachment.eml)[3].eml",
                "attachment_url":  "https://www.attachment3.com"
            }
        ]
}
"@

重命名每个附件以防止重复名称,但保留URL。

理想情况下,代码还应确保数组中不存在任何新名称。因此,如果数组中已经有一个名为(attachment.eml)[1].eml的附件,它也会处理该附件。

我曾经考虑过使用Group-Object,但我还没有完全弄清楚如何实现它。

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56367507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档