I am very new to PowerShell scripting.
I have two folders with hundreds of files with the same name in both folders. I am writing a long script to compare all the files and send output to a text file. When I use this script:
compare-object (get-content "D:\Folder1\file1.js") (get-content "D:\Folder2\file1.js")
compare-object (get-content "D:\Folder1\file2.js") (get-content "D:\Folder2\file2.js")
compare-object (get-content "D:\Folder1\file3.js") (get-content "D:\Folder2\file3.js")
All I get is this:
InputObject SideIndicator
----------- -------------
* New Text =>
* <=
This doesn't tell me which file has this difference. I want it to display the file name also (anyone, first or second file name).
How can I achieve that?
I know I should be using Get-Children, but I need to learn the simple thing first which can be implemented in a loop.
========= Solution Posted As a Separate Answer ================
With kind help of participants I have managed to create the following script that might be helpful for others:
$ErrorActionPreference = 'Continue'
$output = "result.txt"
del $output
get-Date >> $output
Get-ChildItem -Recurse -Path 'D:\Compare\Source\' -Include @('*.js','*.txt', '*.html', '*.css', '*.mxml') |
Foreach-Object {
$f1 = $_.FullName
$f2 = $f1.replace("\Source\","\Comparewith\")
echo "`n" >> $output
echo $f2 >> $output
Test-Path -Path $f2 -PathType Leaf >> $output
compare-object (get-content $f1) (get-content $f2) >> $output
}
get-Date >> $output
Basically I have two folders. Source
and Comparewith
. I am picking up each file from the Source
folder as $f1
, creating a similar file name $f2
while replacing Source
with Comparewith
but keeping same file name, and then comparing both. I am using echo to print the name before each compare-object and also testing if the file exists in the Comparewith
folder.
Any improvement suggestion are welcome.