仅当键名不包含特定单词时,我才必须导出一组注册表项
前任:
reg 导出“HKLM\SYSTEM\CurrentControlSet\Control\Print\Monitors\Standard TCP/IP Port\Ports”
如果 tcp/ip 端口名称包含“Session 2”,则忽略并跳转到下一个
谢谢你的帮助
答案1
我为此编写了一个 PHP 脚本。它读取 Regedit 生成的注册表导出文件,并生成一个类似的文件,但会过滤掉您选择的键。
true
回调接收遍历的注册表项作为参数,并且如果必须过滤掉该项则返回。
function filter_reg_file($inputFile, $outputFile, $callback) {
$content = file_get_contents($inputFile);
$content = mb_convert_encoding($content, 'UTF-8', 'UCS-2LE');
$content = preg_replace('@^(\xEF\xBB\xBF)?Windows Registry Editor Version 5\.00\r\n\r\n@', '', $content);
$lines = explode("\r\n", $content);
$skipping = false;
$result = [];
foreach ($lines as $line) {
if (substr($line, 0, 1) === '[') {
$keyName = substr($line, 1, -1);
$skipping = $callback($keyName);
}
if (!$skipping) {
$result[] = $line;
}
}
$result = "Windows Registry Editor Version 5.00\r\n\r\n" . implode("\r\n", $result);
$result = "\xFF\xFE" . mb_convert_encoding($result, 'UCS-2LE', 'UTF-8');
file_put_contents($outputFile, $result);
}
以下是示例用法。请特别小心过滤根键(例如BagMRU
)和子键(例如BagMRU\foo\bar
)。
filter_reg_file('HKCU_Software.reg', 'HKCU_Software__filtered.reg', function ($key) {
$keysToSkip = [
'HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\BagMRU',
'HKEY_CURRENT_USER\Software\Classes\Local Settings\Software\Microsoft\Windows\Shell\Bags',
];
foreach ($keysToSkip as $keyToSkip) {
if ($key === $keyToSkip || strpos($key, $keyToSkip.'\\') === 0) {
return true;
}
}
return false;
});