非常简单的代码,我正在尝试使用读取主机搜索某些具有特定扩展名的文件。然后,我尝试将其传递给get-childitem行,以便搜索所述扩展名,但它无法正常工作。
$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
我运行了此代码:
$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的回答开头添加*.
。
关于将Get-ChildItem
与-Include
一起使用还有更多上下文:Difference between -include and -filter in get-childitem