PoshCode Archive  Check-in [d8577740bc]

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:When you wish to compare to directory trees, you need the relative pathes of the files with respect to root of the directory tree, so that you can match them.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | trunk
Files: files | file ages | folders
SHA3-256:d8577740bc60dc53e678569484966212a05d3f26e5fd7160526d5e3e203ed1c3
User & Date: Bernd Kriszio 2018-06-10 14:28:03
Context
2018-06-10
14:28
When you wish to compare to directory trees, you need the relative pathes of the files with respect to root of the directory tree, so that you can match them. Leaf check-in: d8577740bc user: Bernd Kriszio tags: trunk
14:28
Copy Data between Folders including a Progressbar check-in: 9a83e1547e user: unknown tags: trunk
Changes

Added Get-ChilditemsRecursive.ps1.





































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# encoding: ascii
# api: powershell
# title: Get-ChilditemsRecursive
# description: When you wish to compare to directory trees, you need the relative pathes of the files with respect to root of the directory tree, so that you can match them.
# version: 0.1
# type: function
# author: Bernd Kriszio
# license: CC0
# function: Get-ChildItem2
# x-poshcode-id: 999
# x-archived: 2015-05-11T20:44:11
# x-published: 2009-04-05T02:04:00
#
# This is my private start to work with offline files
# Bernd Kriszio http://pauerschell.blogspot.com/
#
function Get-ChildItem2 ($path)
{
    # path can either be absolut or relative, I only tried filesystem
    # perhaps to named Get-ChildItemsRecursive
    $root = gi $path
    $PathLength = $root.FullName.length
    # it would be nice if Split-Path could do the trick, I didn't grok it 
    gci $path -rec | % {
    Add-Member -InputObject $_ -MemberType NoteProperty -Name RelativePath -Value $_.FullName.substring($PathLength+1)
    $_
    }
}

# Try for example:
# Get-ChildItem2 $pshome | % {$_.RelativePath }
# Now generate demo data

cd f:
md tmp
md tmp\gcir\dir1
md tmp\gcir\dir2
"file1" > tmp\gcir\dir1\file1.txt
"file2" > tmp\gcir\dir1\file2.txt
copy-Item tmp\gcir\dir1\file1.txt tmp\gcir\dir2\file1.txt # both file1 have same length and LastWriteTime 
"file2changed" > tmp\gcir\dir2\file2.txt  # different length forced by different contents

# now we get pathnames relative to the starting path
Get-ChildItem2 tmp | % {$_.RelativePath }
<#
gcir
gcir\dir1
gcir\dir2
gcir\dir1\file1.txt
gcir\dir1\file2.txt
gcir\dir2\file1.txt
gcir\dir2\file2.txt
#>


# and it is easy to compare two trees of files
Compare-Object (Get-ChildItem2 tmp\gcir\dir1) (Get-ChildItem2 tmp\gcir\dir2) -prop RelativePath, LastWriteTime, Length -includeEqual
<#
RelativePath        LastWriteTime                    Length SideIndicator      
------------        -------------                    ------ -------------      
file1.txt           05.04.2009 10:45:03                  16 ==                 
file2.txt           05.04.2009 10:45:03                  30 =>                 
file2.txt           05.04.2009 10:45:03                  16 <=                 
#>

# well we look only for the different files
Compare-Object (Get-ChildItem2 tmp\gcir\dir1) (Get-ChildItem2 tmp\gcir\dir2) -prop RelativePath, LastWriteTime, Length

# add some fiddeling to the newest file only
$last = $null
Compare-Object (Get-ChildItem2 tmp\gcir\dir1) (Get-ChildItem2 tmp\gcir\dir2) -prop RelativePath, LastWriteTime, Length |
Sort RelativePath, LastWriteTime -desc | % {
if ($last -ne $_.RelativePath)
{ $_ }
$last = $_.RelativePath
} | sort RelativePath

<#
RelativePath        LastWriteTime                    Length SideIndicator      
------------        -------------                    ------ -------------      
file2.txt           05.04.2009 10:45:03                  30 =>                 
#>