Powershell v1 - 删除所有文件&除指定文件夹外的文件夹
我想删除给定根文件夹的所有内容和子文件夹,但是我想保留一些文件(日志),它们全部驻留在日志文件夹中 在 powershell 中执行此操作的优雅方法是什么? 目前我正在分多个步骤进行,这当然可以轻松完成...我有一种感觉我的 oo-ness/语言偏见正在妨碍例如
c:\temp
c:\temp\logs
c:\temp\logs\mylog.txt
c:\temp\logs\myotherlog.log
c:\temp\filea.txt
c:\temp\fileb.txt
c:\temp\folderA...
c:\temp\folderB...
删除后应该是
c:\temp
c:\temp\logs
c:\temp\logs\mylog.txt
c:\temp\logs\myotherlog.log
这应该很简单;我想 12:47am-itis 正在让我...
提前感谢
RhysC
I want to delete all the content and the children of a given root folder, however i want to keep some files (the logs), which all reside in a logs folder
What is the elegant way of doing this in powershell.
Currently i am doing it in multile steps, surely this can be done easily... i have a feeling my oo-ness/language bias is getting in the way
eg
c:\temp
c:\temp\logs
c:\temp\logs\mylog.txt
c:\temp\logs\myotherlog.log
c:\temp\filea.txt
c:\temp\fileb.txt
c:\temp\folderA...
c:\temp\folderB...
after delete should just be
c:\temp
c:\temp\logs
c:\temp\logs\mylog.txt
c:\temp\logs\myotherlog.log
this should be simple; i think 12:47am-itis is getting me...
Thanks in advance
RhysC
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目录 c:\temp |其中 {$_.name -ne '日志'}|删除项目-递归-force
dir c:\temp | where {$_.name -ne 'logs'}| Remove-Item -Recurse -force
这是一个通用的解决方案:
它以递归方式工作。
CleanDir
的第一个参数是起始目录。第二个 ($skipDir
) 是不应删除的目录(并且其内容也不应删除)。已编辑:更正了令人困惑的示例;)
Here is a general solution:
It works recursivelly. The first parameter to
CleanDir
is the start directory. The second one ($skipDir
) is the directory that should not be deleted (and its content shouldn't be as well).Edited: corrected confusing example ;)