非常简单的代码,我正在尝试使用read-host搜索一些具有特定扩展名的文件。然后,我尝试将其传递给一个get-子项目行,以便搜索上述扩展,但它就是不起作用。
$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发布于 2019-11-27 09:42:25
我运行了以下代码:
$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文件。如果您希望最终用户在不将*.放在前面的情况下指定扩展名,则可以这样做:
$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
https://stackoverflow.com/questions/59061519
复制相似问题