首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Powershell -使用Get-ChildItem和Read-host搜索带有扩展名的文件

Powershell -使用Get-ChildItem和Read-host搜索带有扩展名的文件
EN

Stack Overflow用户
提问于 2019-11-27 08:53:47
回答 1查看 66关注 0票数 0

非常简单的代码,我正在尝试使用read-host搜索一些具有特定扩展名的文件。然后,我尝试将其传递给一个get-子项目行,以便搜索上述扩展,但它就是不起作用。

代码语言:javascript
复制
 $SelectedFiles = Read-Host "Which type of file(s) do you wish to delete?"

 $TargetedLocation = Read-Host "Which location are the files located?"

 Get-ChildItem -Path $TargetedLocation -Include $SelectedFiles -Recurse
EN

回答 1

Stack Overflow用户

发布于 2019-11-27 09:42:25

我运行了以下代码:

代码语言:javascript
复制
$SelectedFiles = Read-Host "Which type of file(s) do you wish to delete?"

$TargetedLocation = Read-Host "Which location are the files located?"

Get-ChildItem -Path $TargetedLocation -Include $SelectedFiles -Recurse

对提示做出以下响应:

您要删除哪种类型的文件?:txt

这些文件位于哪个位置?:C:\temp

这不会返回任何输出。但是,如果我重复使用*.txt作为第一个响应,那么我确实会得到C:\temp中的所有txt文件。如果您希望最终用户在不将*.放在前面的情况下指定扩展名,则可以这样做:

代码语言:javascript
复制
$SelectedFiles = Read-Host "Which type of file(s) do you wish to delete?"

$TargetedLocation = Read-Host "Which location are the files located?"

Get-ChildItem -Path $TargetedLocation -Include "*.$SelectedFiles" -Recurse

这会将*.添加到对问题1的回答的开头。

下面是关于在-Include中使用Get-ChildItem的更多信息:Difference between -include and -filter in get-childitem

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59061519

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档