这与问题类似将文件夹名称添加到文件名开头但使用 PowerShell。
我有一个如下的目录结构:
Folder
> SubFolder1
> FileName1.abc
> Filename2.abc
> .............
> SubFolder2
> FileName11.abc
> Filename12.abc
> ..............
> ..........
等等。我想将子文件夹内的文件重命名为:
SubFolder1_Filename1.abc
SubFolder1_Filename2.abc
SubFolder2_Filename11.abc
SubFolder2_Filename12.abc
即在文件名开头添加文件夹名,并以分隔符“_”结尾。
我试过:
get-childitem -recurse | Rename-Item -NewName {$_.Directory.Name + "_" + $_.Name}
但这会将文件夹名称更改为 _SubFolder1、_SubFolder2 等,然后将文件名更改为 _SubFolder1_Filename1.abc 等。
如何确保目录名称保持不变但文件名会改变?
答案1
为了确保目录名称保持不变,请使用以下命令:
get-childitem -recurse |
Where-Object {$_.Psiscontainer -eq $false} |
Rename-Item -NewName {$_.Directory.Name + "_" + $_.Name}
这将仅过滤文件。
答案2
要扩展@Wasif_Hasan 所显示的内容,请注意 Get-ChildItem 具有内置切换参数,用于处理文件和文件夹,您可以捕获这些参数并在用例中利用它们。
因此,这可以重构。这是一个选择示例...
Get-ChildItem -Path 'D:\Temp' -File -Filter '*.txt' -Recurse |
ForEach-Object{$($PSItem.Directory.BaseName + '_' + $PSItem.Name)}
...但您可以在该 ForEach 循环中使用 Rename-Item cmdlet 执行相同的操作。例如:
Get-ChildItem -Path 'D:\Temp' -File -Filter '*.txt' -Recurse |
ForEach-Object{
$RenameItemSplat = @{
Path = $PSItem.FullName
NewName = $PSItem.Directory.BaseName + '_' + $PSItem.Name
WhatIf = $true
Confirm = $true
}
Rename-Item @RenameItemSplat
}
无论何时执行破坏性代码,都应在最终执行之前检查结果。
# Results
<#
What if: Performing the operation "Rename File" on target "Item: D:\Temp\abc.txt Destination: D:\Temp\Temp_abc.txt".
...
What if: Performing the operation "Rename File" on target "Item: D:\Temp\AddressFiles\File1.txt Destination: D:\Temp\AddressFiles\AddressFiles_File1.txt".
...
What if: Performing the operation "Rename File" on target "Item: D:\Temp\NewFolder\awél.txt Destination: D:\Temp\NewFolder\NewFolder_awél.txt".
...
What if: Performing the operation "Rename File" on target "Item: D:\Temp\Reference\awél.txt Destination: D:\Temp\Reference\Reference_awél.txt".
...
What if: Performing the operation "Rename File" on target "Item: D:\Temp\Source\awél.txt Destination: D:\Temp\Source\Source_awél.txt".
...
#>
PowerShell 帮助文件:
Get-ChildItem -File 'D:\Temp'
# Results
<#
Directory: D:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 25-May-20 15:44 25 abc.bat
-a---- 20-May-20 11:40 94 abc.txt
-a---- 10-Jan-20 17:59 0 awél.txt
...
#>
Get-ChildItem -Directory 'D:\Temp'
# Results
<#
Directory: D:\Temp
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 17-Feb-20 15:50 est
d----- 14-Mar-20 17:03 here
d----- 26-May-20 10:38 hold
...
#>
# Get specifics for a module, cmdlet, or function
(Get-Command -Name Get-ChildItem).Parameters
(Get-Command -Name Get-ChildItem).Parameters.Keys
# Results
<#
...
Directory
File
...
#>
Get-help -Name Get-ChildItem -Examples
Get-help -Name Get-ChildItem -Full
# Results
<#
-Directory
To get a list of directories, use the Directory parameter or the Attributes parameter with the Directory property. You can use the Recurse parameter with Directory.
TABLE 3
Type: SwitchParameter
Aliases: ad, d
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
-File
To get a list of files, use the File parameter. You can use the Recurse parameter with File.
TABLE 5
Type: SwitchParameter
Aliases: af
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
#>
Get-help -Name Get-ChildItem -Online