Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 比较两个文件列表及其内容_Xml_Powershell_Compare_Config - Fatal编程技术网

Xml 比较两个文件列表及其内容

Xml 比较两个文件列表及其内容,xml,powershell,compare,config,Xml,Powershell,Compare,Config,虽然这看起来很简单(而且可能是!),但我似乎找不到解决方法 我想做的是比较两个过滤文件列表的内容。一个例子是,如果两个列表返回,说它们在Stuff\files\morefiles位置有一个名为file.config的项,那么这将比较这些文件,并输出更改的位置和内容。本质上,对.config文件进行区分并显示更改的位置。这对于比较两个文件来说通常很简单(可以使用compare object等),但因为它是两个文件列表,而不是单个文件,所以我不知所措 我需要这样做,以显示在软件升级中配置文件所需的所

虽然这看起来很简单(而且可能是!),但我似乎找不到解决方法

我想做的是比较两个过滤文件列表的内容。一个例子是,如果两个列表返回,说它们在Stuff\files\morefiles位置有一个名为file.config的项,那么这将比较这些文件,并输出更改的位置和内容。本质上,对.config文件进行区分并显示更改的位置。这对于比较两个文件来说通常很简单(可以使用compare object等),但因为它是两个文件列表,而不是单个文件,所以我不知所措

我需要这样做,以显示在软件升级中配置文件所需的所有更改的列表,因此从软件的一个版本到下一个版本,对配置文件所做的更改是什么。我之所以在powershell中这样做,是因为它能够轻松地与HG mercurial交互,并由经验较少的用户(通过bat文件)运行

目标是创建一个.txt文件,列出与旧安装或类似安装相比在新安装中更改的所有文件

以下是我目前掌握的情况:

$A = Get-ChildItem -Recurse -path "C:\repos\Dev\Projects\Bat\CurrentVersionRepoCloneTemp" -filter "*.config"

$B = Get-ChildItem -Recurse -path "C:\repos\Dev\Projects\Bat\UpgradeVersionRepoCloneTemp" -filter "*.config"

$C = Compare-Object $A $B -Property ('Name', 'Length') -PassThru | Where-Object {$_.FullName -eq $_.FullName} | ForEach-Object 
{    
    Compare-Object (Get-Content FileA)(Get-Content FileB) #I know this doesn't work 
}$C
想法还是解决方案

你可以做一个比较

$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$hash = [System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($file)))

Tim Ferrill检查更新文件的想法似乎是比较文件的更好方法。做点像

$A = Get-ChildItem -Recurse -path "C:\repos\Dev\Projects\Bat\CurrentVersionRepoCloneTemp" -filter "*.config"
$B = Get-ChildItem -Recurse -path "C:\repos\Dev\Projects\Bat\UpgradeVersionRepoCloneTemp" -filter "*.config"
$A | %{$_ | Add-Member "MD5" ([System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($_))))}
$B | %{$_ | Add-Member "MD5" ([System.BitConverter]::ToString($md5.ComputeHash([System.IO.File]::ReadAllBytes($_))))}
然后我会按目录进行比较和分组

$C = Compare-Object $A $B -Property ('Name', 'MD5') - Passthrough | Group Directory
在那之后,得到实际的改变,这会有点慢。对文件内容进行逐行匹配是很粗糙的,但是如果它们不是太大的话,它仍然会在眨眼之间发生。我的建议是:

$Output = @()
ForEach($File in $C[1].Group){
    $OldData = GC $File
    $C[0].Group | ?{$_.Name -eq $File.Name} | %{
        $NewData = GC $_
        $UpdatedLines = $NewData | ?{$OldData -inotcontains $_}
        $OldLines = $OldData | ?{$NewData -inotcontains $_}
        $Output += New-Object PSObject -Property @{
            UpdatedFile=$_.FullName
            OriginalFile=$File.FullName
            Changes=$UpdatedLines
            Removed=$OldLines
        }
    }
}
一旦你有了它,你只需要输出一些可读的东西。也许是这样的:

Get-Date | Out-File "C:\repos\Dev\Projects\Bat\UpgradeVersionRepoCloneTemp\ChangeLog.txt"
$Output|%{$_|FT OriginalFile,UpdatedFile; "New/Changed Lines"; "-----------------"; $_.Changes; " "; "Old/Removed Lines"; "-----------------"; $_.Removed} | Out-File "C:\repos\Dev\Projects\Bat\UpgradeVersionRepoCloneTemp\ChangeLog.txt" -Append