【发布时间】:2021-06-20 06:04:45
【问题描述】:
我有一个需要搜索的数百个文件的列表。大多数没有文件扩展名,有些有。如果需要,我可以将它们分开并多次运行该作业。
我有一个脚本,我一直在尝试正确,但它似乎不起作用。
我只需要匹配文件名而不是扩展名。
我正在使用“-Like”选项,但这并没有让我得到我需要的结果。如果我包含文件扩展名,那么它可以工作。不幸的是,我有许多扩展名未知的文件名,我只需要匹配名称。
此外,脚本似乎并未扫描子目录以查找匹配项。 -Recurse 在我的示例中不起作用吗?
最后,当测试和 I FORCE 匹配时,它不会显示找到匹配的子目录。
非常欢迎任何帮助。 问候, -罗恩
#Start in this DIR
$folder = 'C:\Workspace\'
#Get the file list here
$Dir2 = 'C:\Workspace\'
$files=Get-Content $Dir2\MISSING_BMS.txt
Write-Host "Starting Folder: $folder"
# Get only files and only their names
$folderFiles = Get-ChildItem -Recurse $folder -File -Name
#Read through Directory and sub-directories
foreach ($f in $files) {
if ($folderFiles -contains $f) {
Write-Host "File $f was found." -foregroundcolor green
} else {
Write-Host "File $f was not found!" -foregroundcolor red
}
}
【问题讨论】:
标签: powershell search regexp-like