PowerShell查找替换同名文件

param(
    $DriverFolder=$(throw "DriverFolder is null"),
    $CatFolder=$(throw "CatFolder is null")
)

$Script:DriverCats = New-Object -TypeName System.Collections.ArrayList
$Script:Cats=$ar = New-Object -TypeName System.Collections.ArrayList

function FindCatPath($DriverFolder)
{
    $CatArray = New-Object -TypeName System.Collections.ArrayList
    Get-ChildItem $DriverFolder -Recurse | ForEach-Object{
        if($_.Extension -eq  '.txt')
        {
            $CatArray.Add($_)
        }
    }
    return $CatArray
}

function ReplaceFile($SourceFile,$TagretFile)
{
     Copy-Item -Path $SourceFile -Destination $TagretFile -Force -Recurse
}

#main
$DriverCats=FindCatPath $DriverFolder
$Cats=FindCatPath $CatFolder
for($i=1;$i -lt $DriverCats.Count;$i++)
{
    for($j=1;$j -lt $Cats.Count;$j++)
    {
        if($DriverCats[$i].Name -eq $Cats[$j].Name)
        {
            ReplaceFile $Cats[$j].FullName $DriverCats[$i].FullName
        }
    }
}
DriverFolder为目标文件夹,
CatFolder为数据源文件夹
posted @ 2017-03-02 00:03  ohlucky  阅读(552)  评论(0编辑  收藏  举报