Diondk
Diondk

Reputation: 105

Compare 2 folders

I got 2 folders which i want to compare.

In both folders there are directory's with numbers Folder a is like = 000123 till 000999 Folder b is like = 000123 TEXT till 000999 Text

Now i want to match the digits in the folder name's when the digits match i want to copy the content of the folderb\000123 text to the foldera\000123.

I got the following script but this isnt working

$doel = "G:\Testkopiescript\"
$regex = "^[0-9]*{6}"

$bronfolder = Get-ChildItem "$p001" | ForEach-Object{$_.Name} | where {$_name -like          $regex}
$checkfolder = Get-ChildItem "$doel*" | ForEach-Object{$_.Name} | where {$_name -like $regex}

foreach ($folder in $bronfolder){
$result = test-path -path $doel\$folder -Filter $regex
Copy-Item $m001\$folder $doel\$folder\ where {$_directoryname -like $folder}
}

Upvotes: 1

Views: 641

Answers (2)

Shay Levy
Shay Levy

Reputation: 126722

Another option:

Get-ChildItem .\b | Where-Object {$_.PSIsContainer -and $_.Name -match '^\d{6}' -and (Test-Path ".a\$($_.name.substring(0,6))" -PathType Container) } | Foreach-Object{
    Copy-Item -Recurse -Force -Path $_.FullName -Destination ".\a\$($_.name.substring(0,6))"
}

Upvotes: 2

Rynant
Rynant

Reputation: 24283

I think this does what you are looking for:

$source = 'C:\Temp\a'
$dest = 'C:\Temp\b'
$regex = '^\d{6}'

# Get all folders under folder 'a' whose name starts with 6 digits.
$sourceDirs = @(Get-ChildItem -Path $source| 
        Where-Object{$_.PSIsContainer -and $_.name -match $regex})

foreach($dir in $sourceDirs) {
    # Get destination folders that match the first 6 digits of the
    # source folder.
    $digits = $dir.Name.Substring(0,6)
    $destFolders = $null
    $destFolders = @(Get-ChildItem "$dest\$digits*"| %{$_.FullName})

    # Copy if the destination path exists.
    foreach($d in $destFolders) {
        Get-ChildItem -Path $dir.FullName| 
            %{Copy-Item -Path $_.FullName -Destination $d -Recurse}
    }
}

Upvotes: 3

Related Questions