powershell - 如何使用 PowerShell 递归删除具有数字名称的文件夹
问题描述
我有根据修改日期删除文件夹的脚本。如果文件夹名称中仅包含数字,有人可以帮我删除吗?
$locations=import-csv "C:\Temp\Scripts\AgeOffDirsGeneral.csv"
foreach ($location in $locations)
{
$Source=$location.Source
Get-ChildItem $source |Where-Object {$_.psiscontainer} | Foreach-Object {Remove-Item -Recurse -Force $_.FullName}
}
解决方案
Remove-Item 接受管道输入,因此不需要 ForEach。
如果输出看起来正常,请删除-WhatIf
locations=import-csv "C:\Temp\Scripts\AgeOffDirsGeneral.csv"
foreach ($location in $locations){
$Source=$location.Source
Get-ChildItem $source -Dir -Recurse|
Where-Object { $_.Name -match '^\d+$'} |
Remove-Item -Force -WhatIf
}
顺便说一句,中间变量 $source 可以避免直接插入 $location.Source
推荐阅读
- python - Python从pyinstaller创建.exe返回错误
- php - 在分隔符后查找单词,如果单词被大括号包围,则忽略
- jquery - Jquery 无法运行,如何检查错误?
- vuejs2 - Stripe redirectToCheckout 方法在 .then 部分显示任何响应之前重定向到成功的 url
- javascript - 禁用/重新启用 safari 扩展时如何防止多次注入内容脚本?
- javascript - 按数组 2 的 ID 对数组 1 进行排序。根据数组 2 从数组 1 中删除和添加元素时
- c# - MediaPlayerElement 不播放视频,除了不从我的可移动设备中获取路径
- docker - 未定义时,Docker Compose env 文件优先于 shell 传递变量
- python-2.7 - 如何验证对 AWS 签名 v4 的支持?
- firebase - Cloud Functions 部署失败:“加载用户代码时函数失败。错误消息:无法加载文件 lib/index.js 中的代码。”