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爲數據源文件夾
相關文章
相關標籤/搜索