Mrc*_*ief 11 powershell file list delete-file
我在文本文件中有一个文件名列表,如下所示:
f1.txt
f2
f3.jpg
Run Code Online (Sandbox Code Playgroud)
除了Powershell中的这些文件外,如何从文件夹中删除其他所有内容?
伪代码:
x0n*_*x0n 17
数据:
-- begin exclusions.txt --
a.txt
b.txt
c.txt
-- end --
Run Code Online (Sandbox Code Playgroud)
码:
# read all exclusions into a string array
$exclusions = Get-Content .\exclusions.txt
dir -rec *.* | Where-Object {
$exclusions -notcontains $_.name } | `
Remove-Item -WhatIf
Run Code Online (Sandbox Code Playgroud)
-WhatIf如果您对结果满意,请取下开关.-WhatIf告诉你它会做什么(即它不会删除)
-Oisin
如果文件存在于当前文件夹中,则可以执行以下操作:
Get-ChildItem -exclude (gc exclusions.txt) | Remove-Item -whatif
Run Code Online (Sandbox Code Playgroud)
此方法假定每个文件都在一个单独的行上.如果文件存在于子文件夹中,那么我会选择Oisin的方法.
| 归档时间: |
|
| 查看次数: |
10810 次 |
| 最近记录: |