windows - 用于删除以“。”开头的文件夹的 Powershell 脚本 不起作用 - 为什么?
问题描述
我整理了一个简单的 powershell 脚本,用于删除特定文件夹:
gci -include .vs -recurse | remove-item -force -recurse
但是,.vs 文件夹不会被删除(如果点被删除,那么名为“vs”的文件夹会被删除)。我肯定错过了什么。
$PSVersionTable.PSVersion
回报告:
Major Minor Build Revision
----- ----- ----- --------
5 1 16299 666
我通过文件浏览器ala“右键单击->运行Powershell脚本”运行脚本。不知道这是否会使脚本在最新最好的 powershell 版本下运行。
更新:
原来,罪魁祸首是 .vs 文件夹被标记为“只读”。由于某种原因,即使确实指定了“-force”标志,powershell 脚本也无法将其删除。有什么可以做的吗?
解决方案
您在评论中提到要删除的目录同时具有Hidden
和ReadOnly
filesystem 属性集。
虽然-Force
在您的Remove-Item
调用中能够强制删除具有该ReadOnly
属性的项目,但您的输入 Get-ChildItem
调用-Force
也需要,否则它不会找到hidden files 和 folder,因此Remove-Item
也永远不会看到它们:
# Note the -Force added to Get-ChildItem.
Get-ChildItem -Force -Include .vs -Recurse | Remove-Item -Force -Recurse -WhatIf
推荐阅读
- ios - 如何将可观察的元组元素与可观察的单个元素结合起来?
- java - 使用 RestHighLevelClient 的 ElasticSearch 批量插入错误
- javascript - 根据操作系统分辨率和 CSS 位置,会出现轻微的空间
- c# - 无法捕获在具有调试模式的任务中生成的异常
- javascript - 奇怪的 Java Base64.decode 结果
- ethernet - 将芯片从 CANFD 或 SPI 信号转换为以太包
- python - 将列表中的值附加到不同的集合列表中
- react-native - 在 React 本机项目中链接 Google Place API 时构建失败
- python - Pyspark 的 sqlContext.read.csv() 函数读取的行数比实际 .csv 文件中的行数多
- java - 有没有办法创建父类方法,在子类对象上调用返回另一个子类对象?