Powershell用父文件夹名替换文件名而不更改文件结尾

时间:2016-01-29 19:36:52

标签: file powershell rename windows-server-2012

我有一个父文件夹,每个文件夹中有超过1000个1级嵌套文件夹和数千个文本文件:

Parent folder ==> Folder1 ==> File1.txt
Parent folder ==> Folder1 ==> File2.txt
Parent folder ==> Folder1 ==> File3.txt
Parent folder ==> Folder2 ==> File1.txt
Parent folder ==> Folder2 ==> File2.txt
Parent folder ==> Folder2 ==> File3.txt
Parent folder ==> Folder3 ==> File1.txt
Parent folder ==> Folder3 ==> File2.txt
Parent folder ==> Folder3 ==> File3.txt

我需要能够使用PowerShell重命名文件,如下所示:

Parent folder ==> Folder1 ==> Folder1_File1.txt
Parent folder ==> Folder1 ==> Folder1_File2.txt
Parent folder ==> Folder1 ==> Folder1_File3.txt
Parent folder ==> Folder2 ==> Folder2_File1.txt
Parent folder ==> Folder2 ==> Folder2_File2.txt
Parent folder ==> Folder2 ==> Folder2_File3.txt
Parent folder ==> Folder3 ==> Folder3_File1.txt
Parent folder ==> Folder3 ==> Folder3_File2.txt
Parent folder ==> Folder3 ==> Folder3_File3.txt

有没有办法在PowerShell,Windows Server 2012中执行此操作

提前谢谢你。

1 个答案:

答案 0 :(得分:0)

dir C:\temp\folder\ -directory | % {dir $_.fullname | % {ren $_.fullname $((split-path (split-path $_.fullname) -leaf) + '_' + $_.name)}}