微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

PowerShell - 将父文件夹名称添加到文件名

如何解决PowerShell - 将父文件夹名称添加到文件名

我有这个文件夹结构

android.useandroidX=true 
android.enableJetifier=true

我想将文本文件重命名root\1\2\3\1.txt root\3\2\4\6\1.txt 我试过了

root_1.txt

但这不起作用,因为父级仅适用于目录

解决方法

对于文件项,使用 Directory 属性:

Get-ChildItem -Recurse *.txt | Rename-Item -NewName {$_.Directory.Parent.Name + $_.Name}
,
Get-ChildItem -Recurse *.txt | 
    Where-Object { ($_.FullName | Split-Path -NoQualifier ) -match '^\\(.+?)\\' } | # Get root folder using -match and regex
    Rename-Item -NewName { $Matches[1] + '_' + $_.Name } -WhatIf    # Build new name from $Matches[1] and filename
                                                                    # Remove -WhatIf after testing

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。