搜索目录结构并报告文件夹是否存在?
Search a directory structure and report folder present or missing?
我正在寻求有关在 PowerShell 中编写代码以进行检查和报告的帮助。
我想搜索 C:\MyData\MyCustomerNames
文件夹 MyTests
、MyProducts
和 MyReturns
,然后返回 C:\MyData\MyCustomerNames\MyTests is Present
或 C:\MyData\MyCustomerNames\MyProducts is Missing
.
我知道代码 Test-Path C:\MyData\MyCustomerNames\MyProducts
可以工作,但我还想测试 *.xml
、*.docx
等。此外,路径中唯一的部分是变量是 MyCustomerNames
.
如果你能指出一些有用的东西,那就太棒了,或者提供比我提供的更多的例子。
拜托,谢谢!
扩展 sodawillow 推荐的内容。将您的位置保存在数组中,然后使用 foreach
语句 运行 数组中每个值的循环。您可以在 if
语句中使用 Get-ChildItem
来搜索这些目录并评估它们是否存在。
$folders = "MyTests", "MyProducts", "MyReturns"
foreach ($folder in $folders) {
if (Get-ChildItem -Path "C:\MyData\MyCustomerNames" -Filter $folder -Directory) {
"$folder exists"
} else {
"$folder does not exist"
}
}
我正在寻求有关在 PowerShell 中编写代码以进行检查和报告的帮助。
我想搜索 C:\MyData\MyCustomerNames
文件夹 MyTests
、MyProducts
和 MyReturns
,然后返回 C:\MyData\MyCustomerNames\MyTests is Present
或 C:\MyData\MyCustomerNames\MyProducts is Missing
.
我知道代码 Test-Path C:\MyData\MyCustomerNames\MyProducts
可以工作,但我还想测试 *.xml
、*.docx
等。此外,路径中唯一的部分是变量是 MyCustomerNames
.
如果你能指出一些有用的东西,那就太棒了,或者提供比我提供的更多的例子。
拜托,谢谢!
扩展 sodawillow 推荐的内容。将您的位置保存在数组中,然后使用 foreach
语句 运行 数组中每个值的循环。您可以在 if
语句中使用 Get-ChildItem
来搜索这些目录并评估它们是否存在。
$folders = "MyTests", "MyProducts", "MyReturns"
foreach ($folder in $folders) {
if (Get-ChildItem -Path "C:\MyData\MyCustomerNames" -Filter $folder -Directory) {
"$folder exists"
} else {
"$folder does not exist"
}
}