Powershell script to delete files not specified in a list

后端 未结 3 775
自闭症患者
自闭症患者 2020-12-25 08:46

I have a list of filenames in a text file like this:

f1.txt
f2
f3.jpg

How do I delete everything else from a folder except these files in P

3条回答
  •  醉酒成梦
    2020-12-25 09:38

    actually this only seems to work for the first directory rather than recursing - my altered script recurses properly.

    $exclusions = Get-Content .\exclusions.txt
    
    dir -rec | where-object {-not($exclusions -contains [io.path]::GetFileName($_))} | `  
    where-object {-not($_ -is [system.IO.directoryInfo])} | remove-item -whatif
    

提交回复
热议问题