PowerShell: Rainer Leemet, Kopeeri-Manti.ps1

From ICO wiki
Revision as of 12:30, 2 February 2012 by Rleemet (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

<source lang="powershell">

<# .SYNOPSIS

   Skript mis kopeerib failid ja kaustad algallikast sihtkohta

.DESCRIPTION

   Kui sihtkaust puudub see luuakse ning vajalikud failid ja kaustad luuakse.
   Kui kaust on olemas ja seal on faile ning kaustu, siis kirjutatakse üle ainult need failid, mille suurus sihtkaustas on erinev lähtekaustas olevast faili suurusest ning kui alamkaustad puuduvad, need luuakse.
   Lisaks on skriptil parameeter -remove, mille kasutamisel kustutatakse sihtkaustast failid, mida lähtekaustas ei ole.

.NOTES

   Autor: Rainer Leemet
   Kõik veel päris 100% ei tööta

.EXAMPLE

   PS> Kopeeri-Manti.ps1 -sourcedir c:\test -targetdir c:\test2 -remove
  1. >

Param ( [Parameter(Mandatory = $true)] [String] $sourcedir, [Parameter(Mandatory = $true)] [String] $targetdir, [Parameter(Mandatory = $false)] [boolean] $remove = $false )

if (Test-Path $targetdir) {

$source = Get-ChildItem $sourcedir -Recurse -Force $target = Get-ChildItem $targetdir -Recurse -Force $compare = Compare-Object $source $target -Property Name, Length -PassThru | Where-Object {$_.SideIndicator -eq "<="} $compare2 = Compare-Object $source $target -Property Name, Length -PassThru | Where-Object {$_.SideIndicator -eq "=>"}

if($remove)

       {
   foreach ($file in $compare2) {                
       Remove-Item $file -Force
       Write-Host $file
           }
       }

ForEach ($item in $compare) {

   $strdir = $targetdir + ($item | Split-Path -NoQualifier | Out-String).trim()
   Copy-Item $item -Destination $strdir -Force -Recurse
   }

} else {

  Copy-Item $sourcedir $destdir -Recurse -Force

}