首页 > 解决方案 > PowerShell - 将父文件夹名称添加到文件名

问题描述

我有这个文件夹结构

root\1\2\3\1.txt
root\3\2\4\6\1.txt

我想将文本文件重命名为root_1.txt 我尝试过

Get-ChildItem -Recurse *.txt | Rename-Item -NewName %{$_.Parent.Parent.Name + $_.Name} 

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

标签: powershellscripting

解决方案


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

Get-ChildItem -Recurse *.txt | Rename-Item -NewName {$_.Directory.Parent.Name + $_.Name}

推荐阅读