Powershell script to delete files not specified in a list

后端 未结 3 781
自闭症患者
自闭症患者 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:26

    If the files exist in the current folder then you can do this:

    Get-ChildItem -exclude (gc exclusions.txt) | Remove-Item -whatif
    

    This approach assumes each file is on a separate line. If the files exist in subfolders then I would go with Oisin's approach.

提交回复
热议问题