Windows Powershell을 사용하여 두 폴더 구조의 내용에서 차이점을 찾으려고합니다. 파일 이름이 동일한 지 확인하기 위해 다음 방법을 사용했지만이 방법으로 파일의 내용이 동일한 지 여부를 알 수 없습니다.
$firstFolder = Get-ChildItem -Recurse folder1
$secondFolder = Get-ChildItem -Recurse folder2
Compare-Object -ReferenceObject $firstFolder -DifferenceObject $secondFolder
이 ServerFault 질문에 설명 된 기술은 단일 파일을 비교하는 데 효과적이지만 이러한 폴더에는 다양한 깊이의 수백 개의 파일이 있습니다.
이 솔루션은 파일의 특정 내용이 다른 것이 무엇인지 말해 줄 필요는 없습니다. 나는 이미 다른 것으로 알려진 날짜와 같은 메타 데이터의 차이점에 관심이 없습니다.
답변
비교를 루프로 감싸려면 다음 접근법을 사용하십시오.
$folder1 = "C:\Users\jscott"
$folder2 = "C:\Users\public"
# Get all files under $folder1, filter out directories
$firstFolder = Get-ChildItem -Recurse $folder1 | Where-Object { -not $_.PsIsContainer }
$firstFolder | ForEach-Object {
# Check if the file, from $folder1, exists with the same path under $folder2
If ( Test-Path ( $_.FullName.Replace($folder1, $folder2) ) ) {
# Compare the contents of the two files...
If ( Compare-Object (Get-Content $_.FullName) (Get-Content $_.FullName.Replace($folder1, $folder2) ) ) {
# List the paths of the files containing diffs
$_.FullName
$_.FullName.Replace($folder1, $folder2)
}
}
}
이 존재하지 않는 파일을 무시하도록 주 모두 $folder1 와 $folder2.
답변
jscott의 대답을 확장하여 한 유형에는 있지만 그 유형의 기능에 관심이없는 사람들을 위해 다른 파일은 출력하지 않는 파일을 출력했습니다. 또한 큰 폴더가 주어 졌을 때 큰 차이가없는 것을 알기가 어려웠으므로 진행 상황을 보여줍니다. 대본이 나에게 걸려있는 것처럼 보였습니다. 이를위한 powershell 코드는 다음과 같습니다.
$folder1 = "C:\Folder1"
$folder2 = "C:\Folder2"
# Get all files under $folder1, filter out directories
$firstFolder = Get-ChildItem -Recurse $folder1 | Where-Object { -not $_.PsIsContainer }
$failedCount = 0
$i = 0
$totalCount = $firstFolder.Count
$firstFolder | ForEach-Object {
$i = $i + 1
Write-Progress -Activity "Searching Files" -status "Searching File $i of $totalCount" -percentComplete ($i / $firstFolder.Count * 100)
# Check if the file, from $folder1, exists with the same path under $folder2
If ( Test-Path ( $_.FullName.Replace($folder1, $folder2) ) ) {
# Compare the contents of the two files...
If ( Compare-Object (Get-Content $_.FullName) (Get-Content $_.FullName.Replace($folder1, $folder2) ) ) {
# List the paths of the files containing diffs
$fileSuffix = $_.FullName.TrimStart($folder1)
$failedCount = $failedCount + 1
Write-Host "$fileSuffix is on each server, but does not match"
}
}
else
{
$fileSuffix = $_.FullName.TrimStart($folder1)
$failedCount = $failedCount + 1
Write-Host "$fileSuffix is only in folder 1"
}
}
$secondFolder = Get-ChildItem -Recurse $folder2 | Where-Object { -not $_.PsIsContainer }
$i = 0
$totalCount = $secondFolder.Count
$secondFolder | ForEach-Object {
$i = $i + 1
Write-Progress -Activity "Searching for files only on second folder" -status "Searching File $i of $totalCount" -percentComplete ($i / $secondFolder.Count * 100)
# Check if the file, from $folder2, exists with the same path under $folder1
If (!(Test-Path($_.FullName.Replace($folder2, $folder1))))
{
$fileSuffix = $_.FullName.TrimStart($folder2)
$failedCount = $failedCount + 1
Write-Host "$fileSuffix is only in folder 2"
}
}
답변
이미 답변 한 링크 된 질문에서 정답 주위를 반복하고 모든 파일을 동일한 이름으로 비교하는 디렉토리 트리를 살펴보십시오.
/ 편집 : 만약 이것이 실제로 당신의 질문이라면, 당신이 정기적 인 기여자 인 것처럼 SO에 더 적합합니다. 프로그래밍 질문을하고 있습니다. sysadmin 유형의 목적으로 수행하고 있음을 이해합니다.이 경우 WinDiff와 같은 특수 목적 도구를 사용하도록 지시합니다.
답변
이 작업을 수행:
compare (Get-ChildItem D:\MyFolder\NewFolder) (Get-ChildItem \\RemoteServer\MyFolder\NewFolder)
그리고 재귀 적으로 :
compare (Get-ChildItem -r D:\MyFolder\NewFolder) (Get-ChildItem -r \\RemoteServer\MyFolder\NewFolder)
그리고 심지어 잊기 어렵다 🙂