PowerShell 技能连载 - 从目录结构中删除所有文件

有些任务听起来复杂,实际上并没有那么复杂。假设我们需要清除一个目录结构,移除所有文件,而留下空白的文件夹。我们进一步假设有一些文件位于白名单上,不应移除。如果用 PowerShell,那么实现起来很容易:

1
2
3
4
5
6
7
8
9
10
11
12
# Task:
# remove all files from a folder structure, but keep all folders,
# and keep all files on a whitelist

$Path = "c:\sample"
$WhiteList = "important.txt", "something.csv"

Get-ChildItem -Path $Path -File -Exclude $WhiteList -Recurse -Force |
# remove -WhatIf if you want to actually delete files
# ATTENTION: test thoroughly before doing this!
# you may want to add -Force to Remove-Item to forcefully remove files
Remove-Item -WhatIf

PowerShell 技能连载 - 从目录结构中删除所有文件

http://blog.vichamp.com/2018/11/15/deleting-all-files-from-a-folder-structure/

作者

吴波

发布于

2018-11-15

更新于

2022-07-06

许可协议

评论