我试图比较两个文件夹结构:
来源结构
$SrcFolders
C:\Temp\FolderName\FolderA
C:\Temp\FolderName\FolderB\SubFolderBA
C:\Temp\FolderName\FolderB\SubFolderBB
C:\Temp\FolderName\FolderC
目标结构
$TrgFolders
S:\Docs\FolderName\FolderA
S:\Docs\FolderName\FolderB\SomethingWrong
S:\Docs\FolderName\FolderB\SubFolderBA
S:\Docs\FolderName\FolderB\SubFolderBB
S:\Docs\FolderName\FolderB\SubFolderBB\SomethingWrong
S:\Docs\FolderName\FolderC
S:\Docs\FolderName\SomethingWrong
我试图找到最佳匹配:
代码
$Source = 'C:\Temp\FolderName\'
$Target = 'S:\Docs\FolderName'
foreach ($t in ($TrgFolders.FullName)) {
$Folder = $t.Replace($Target,$Source)
# Exact match
# 'S:\Docs\FolderName\FolderA' > Desired result: 'C:\Temp\FolderName\FolderA'
if ($SrcFolders.FullName -contains $Folder) {
$FileACL = Get-Acl ($Folder + '\' + $TempFile)
break
}
# Next best match
# 'S:\Docs\FolderName\FolderB\SomethingWrong' > Desired result: 'C:\Temp\FolderName\FolderB'
# 'S:\Docs\FolderName\FolderB\SubFolderBB\SomethingWrong' > Desired result: 'C:\Temp\FolderName\FolderB\SubFolderBB'
$F = $SrcFolders.FullName | where {$_ -like "$Folder*"} | sort length | select -First 1
$FileACL = Get-Acl ($F + '\' + $TempFile)
}
正如您所看到的那样-contains
很容易找到完全匹配,但找到下一个最佳匹配并不是真的有效。感谢您的帮助。
答案 0 :(得分:0)
与此同时,我解决了自己的问题:
foreach ($t in ($TrgFolders.FullName)) {
$SrcFolder = $t.Replace($Target,$Source)
$Split = $SrcFolder.Split('\')
for ($i=1; $i -lt $Split.count; $i++) {
$R = $Split[0..($Split.Count-$i)] -join '\'
if ($SrcFolders.FullName -contains $R) {
$FileACL = Get-Acl ($R + '\' + $TempFile)
break
}
}
}