我正在尝试编写一个脚本,以便在path下的子文件夹中search具有不同扩展名的重复的基本文件名。
例如,一个文件夹可能包含file1.zip和file1.pdf,我只想删除文件夹内的重复文件。
我来到这将只会在pathvariables设置的path。 向Get-Child Item添加-recurse似乎不起作用。
$Path = "C:\logs" ForEach ($File in (Get-ChildItem $Path\*.pdf)) { If ((Test-Path "$Path\$($File.Basename).zip")) { $File | Remove-Item } }
有关如何让这个逻辑适用于所有子文件夹的任何build议?
以下是你需要的。 你遇到的问题是你没有提供正确的道路。 由于您正在使用-Recurse ,所以path$Path\$($File.Basename).zip并不总是正确的,因为只有当文件是$Path的直接子$Path\$($File.Basename).zip它才是正确的。
$path = "C:\logs" Get-ChildItem $path\*.pdf -Recurse | ForEach-Object { if( Test-Path "$($_.DirectoryName)\$($_.Basename).zip" ) { Remove-Item "$($_.DirectoryName)\$($_.Basename).pdf" } }