如何计算成功查找(和替换)
How to count successful find (and replace)
我正在尝试计算像这样的简单脚本所做的替换次数:
$count = 0
Function findAndReplace($objFind, $FindText, $ReplaceWith) {
$count += $objFind.Execute($FindText, $matchCase, $matchWholeWord, \`
$matchWildCards, $matchSoundsLike, $matchAllWordForms, \`
$forward, $findWrap, $format, $ReplaceWith, $replace)
}
替换完成,但 $count
仍为 0
...
$count
必须在要使用的函数内部或将其作为参数。
试试这个
Function findAndReplace($objFind, $FindText, $ReplaceWith) {
$count = 0
$replacementfound = $objFind.Execute($FindText, $matchCase, $matchWholeWord, \`
$matchWildCards, $matchSoundsLike, $matchAllWordForms, \`
$forward, $findWrap, $format, $ReplaceWith, $replace)
if ($replacementfound -eq "True"){$count++}
write-host $count
}
This is a scoping issue. AFAIK $count
不用先初始化
增量逻辑看起来找到了。但是,您需要在增量后从函数中 return 它。否则它仍将是 0
在函数外的范围内定义的。
Function findAndReplace($objFind, $FindText, $ReplaceWith) {
$count += $objFind.Execute($FindText, $matchCase, $matchWholeWord, \`
$matchWildCards, $matchSoundsLike, $matchAllWordForms, \`
$forward, $findWrap, $format, $ReplaceWith, $replace)
return $count;
}
$myCountOutSideFunctionScope = findAndReplace -objFind ... -FindText ... -ReplaceWith ...
我正在尝试计算像这样的简单脚本所做的替换次数:
$count = 0
Function findAndReplace($objFind, $FindText, $ReplaceWith) {
$count += $objFind.Execute($FindText, $matchCase, $matchWholeWord, \`
$matchWildCards, $matchSoundsLike, $matchAllWordForms, \`
$forward, $findWrap, $format, $ReplaceWith, $replace)
}
替换完成,但 $count
仍为 0
...
$count
必须在要使用的函数内部或将其作为参数。
试试这个
Function findAndReplace($objFind, $FindText, $ReplaceWith) {
$count = 0
$replacementfound = $objFind.Execute($FindText, $matchCase, $matchWholeWord, \`
$matchWildCards, $matchSoundsLike, $matchAllWordForms, \`
$forward, $findWrap, $format, $ReplaceWith, $replace)
if ($replacementfound -eq "True"){$count++}
write-host $count
}
This is a scoping issue. AFAIK $count
不用先初始化
增量逻辑看起来找到了。但是,您需要在增量后从函数中 return 它。否则它仍将是 0
在函数外的范围内定义的。
Function findAndReplace($objFind, $FindText, $ReplaceWith) {
$count += $objFind.Execute($FindText, $matchCase, $matchWholeWord, \`
$matchWildCards, $matchSoundsLike, $matchAllWordForms, \`
$forward, $findWrap, $format, $ReplaceWith, $replace)
return $count;
}
$myCountOutSideFunctionScope = findAndReplace -objFind ... -FindText ... -ReplaceWith ...