比较2个文件夹

时间:2011-06-15 10:32:08

标签: powershell

我有2个文件夹,我想比较。

在两个文件夹中都有带数字的目录 文件夹a就像= 000123直到000999 文件夹b类似于= 000123 TEXT直到000999文本

现在我想匹配数字匹配时文件夹名称中的数字我想将folderb \ 000123文本的内容复制到foldera \ 000123。

我得到了以下脚本,但这不起作用

$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}
}

2 个答案:

答案 0 :(得分:3)

我认为这可以满足您的需求:

$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}
    }
}

答案 1 :(得分:2)

另一种选择:

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))"
}