Windows Vista Forums

How to copy directories with PowerShell incrementally
  1. #1


    Flea# Guest

    How to copy directories with PowerShell incrementally

    Hello all,



    I am wanting to have a powershell script run each morning that will perform
    back up of several directories, containing hundreds of files. The Copy-Item
    cmd-let works great for copying the directories, but, I didn't see an
    explicit option to set that will only back up the files that are different or
    don't exist, thus giving me an incremental backup? The files are to large to
    just do a force overwrite each time.

    Before I delve into writing custom functions to perform this task, I was
    wondering if there was an easy option or property I was missing that will do
    this?

    Thanks,
    Flea#
    --
    http://fleasharp.blogspot.com/

      My System SpecsSystem Spec

  2. #2


    Tomas Restrepo [MVP] Guest

    Re: How to copy directories with PowerShell incrementally

    Hi Flea,

    > I am wanting to have a powershell script run each morning that will
    > perform
    > back up of several directories, containing hundreds of files. The
    > Copy-Item
    > cmd-let works great for copying the directories, but, I didn't see an
    > explicit option to set that will only back up the files that are different
    > or
    > don't exist, thus giving me an incremental backup? The files are to large
    > to
    > just do a force overwrite each time.
    This may be a "low-tech" way of doing it, but for this I just use my
    powershell script to drive robocopy.exe, which can do that (and it's really
    fast as well).


    --
    Tomas Restrepo
    http://www.devdeo.com/
    http://www.winterdom.com/weblog/


      My System SpecsSystem Spec

  3. #3


    Roman Kuzmin Guest

    Re: How to copy directories with PowerShell incrementally

    As far as I know there is no such an option in PowerShell. If you want to
    write this function for fun, do it, indeed. But for serious tasks like this
    I would rather recommend to use some existing tools with tons of options,
    one of them Robocopy for example (which by default copies folders exactly as
    you want).

    --
    Thanks,
    Roman Kuzmin

    http://code.google.com/p/farnet/
    PowerShell and .NET in Far Manager



      My System SpecsSystem Spec

  4. #4


    Flea# Guest

    Re: How to copy directories with PowerShell incrementally

    Roman, Tomas, thank you both!

    -Flea

    --
    http://fleasharp.blogspot.com/


    "Tomas Restrepo [MVP]" wrote:

    > Hi Flea,
    >

    > > I am wanting to have a powershell script run each morning that will
    > > perform
    > > back up of several directories, containing hundreds of files. The
    > > Copy-Item
    > > cmd-let works great for copying the directories, but, I didn't see an
    > > explicit option to set that will only back up the files that are different
    > > or
    > > don't exist, thus giving me an incremental backup? The files are to large
    > > to
    > > just do a force overwrite each time.
    >
    > This may be a "low-tech" way of doing it, but for this I just use my
    > powershell script to drive robocopy.exe, which can do that (and it's really
    > fast as well).
    >
    >
    > --
    > Tomas Restrepo
    > http://www.devdeo.com/
    > http://www.winterdom.com/weblog/
    >
    >

      My System SpecsSystem Spec

  5. #5


    Ozone Guest

    Re: How to copy directories with PowerShell incrementally

    Look at the first post in this thread. This script may be what you are
    looking for...
    http://groups.google.com/group/micro...55886ab29290ae

    HTH
    Ozone

      My System SpecsSystem Spec

  6. #6


    Kiron Guest

    Re: How to copy directories with PowerShell incrementally

    This script takes one or more source directories and one destination, e.g.

    .\script dir1, e:\dir2, dir3 if:\backup

    < script.ps1 >
    param ([array]$source, $destination)
    if (!$source -or !$destination) {throw 'Missing argument'}
    if (!(test-path $destination -is)) {
    throw "$destination << is not a valid path"
    }

    function Compare-Hash ($f1, $f2) {
    $sha1 = [System.Security.Cryptography.SHA1]::create()
    $stream1 = new-object IO.StreamReader $f1
    $h1 = $sha1.computeHash($stream1.baseStream)
    $stream1.close()
    $stream2 = new-object IO.StreamReader $f2
    $h2 = $sha1.computeHash($stream2.baseStream)
    $stream2.close()
    "$h1" -eq "$h2"
    }

    foreach ($src in $source) {
    $srcDir = $(if (!(test-path $src)) {
    throw "Verify ${src}'s path."} else {
    (rvpa $src).path})

    $dst = $destination + ($srcDir -replace '^.+(\\.+)$','$1')
    $dstDir = $(if (!(test-path $dst)) {
    (ni $dst -i d*).fullName} else {
    (rvpa $dst).path})

    $escSrcDir = [regex]::escape($srcDir)

    $srcSub = ls $srcDir -r | ? {$_.psIsContainer} | % {$_.fullName} | sort
    if ($srcSub) {
    $srcSub -replace $escSrcDir, $dstDir |
    % {if (!(test-path $_)) {[void](ni $_ -i d*)}}
    }

    $testSrc = ls $srcDir -r | ? {!$_.psIsContainer} |
    % {$_.fullName -replace $escSrcDir} | sort

    $testDst = ls $dstDir -r | ? {!$_.psIsContainer} |
    % {$_.fullName -replace [regex]::escape($dstDir)} | sort

    $testSrc | ? {$testDst -notContains $_} |
    cpi -pat {$srcDir + $_} -des {$dstDir + $_}

    $testDst | ? {$testSrc -contains $_ -and
    !(compare-hash ($srcDir + $_) ($dstDir + $_))} |
    cpi -pat {$srcDir + $_} -des {$dstDir + $_}
    }
    < end >

    --
    Kiron

      My System SpecsSystem Spec

  7. #7


    CodeTestDummy Guest

    Re: How to copy directories with PowerShell incrementally

    I think WMI is the best way to go for this. I just wrote this but did not
    test it. It could also use some more error checking. If I get the change
    over the weekend I will test and improve it. If you make changes and
    improve it, please post it for other to see and use.

    $strSource = "c:\test"
    $strTarget = "c:\ftp\test"

    function CopyDir([string]$strSource, [string]$strTarget)
    {

    $strComputer = "."

    $colFolder = Get-WmiObject -Class Win32_Directory -namespace
    "root\CIMV2" -computername $strComputer | where{$_.name -eq $strSource}

    foreach($objFolder in $colFolders)
    {
    [int]$errResults = $colFolders.CopyEx($strTarget)

    switch($errResults)
    {
    0 {Write-Host "The request was successful."}
    2 {Write-Host "Access was denied."}
    8 {Write-Host "An unspecified failure occurred."}
    9 {Write-Host "The name specified was invalid."}
    10 {Write-Host "The object specified already exists."}
    11 {Write-Host "The file system is not NTFS."}
    12 {Write-Host "The platform is not Windows 2000 or Windows NT."}
    13 {Write-Host "The drive is not the same."}
    14 {Write-Host "The directory is not empty."}
    15 {Write-Host "There has been a sharing violation."}
    16 {Write-Host "The start file specified was invalid."}
    17 {Write-Host "A privilege required for the operation is not held."}
    21 {Write-Host "A parameter specified is invalid."}
    }
    }

    }



    "Flea#" <Flea@xxxxxx> wrote in message
    news:7B82DEE2-26C3-46AB-9636-4F45655DE2F9@xxxxxx

    > Hello all,
    >
    > I am wanting to have a powershell script run each morning that will
    > perform
    > back up of several directories, containing hundreds of files. The
    > Copy-Item
    > cmd-let works great for copying the directories, but, I didn't see an
    > explicit option to set that will only back up the files that are different
    > or
    > don't exist, thus giving me an incremental backup? The files are to large
    > to
    > just do a force overwrite each time.
    >
    > Before I delve into writing custom functions to perform this task, I was
    > wondering if there was an easy option or property I was missing that will
    > do
    > this?
    >
    > Thanks,
    > Flea#
    > --
    > http://fleasharp.blogspot.com/


      My System SpecsSystem Spec

How to copy directories with PowerShell incrementally problems?

Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy files from multiple directories.. Steve Grosz PowerShell 8 12 Jan 2009
Powershell and WSUS: Copy approvals fdaa PowerShell 0 22 Oct 2008
Powershell to copy a some file/folder to multiple location Jian An Lim PowerShell 1 10 Apr 2008
excluding copy directories in robocopy Nicholas Hall Vista file management 0 13 Mar 2008
Delete YYYYMMDD Directories Using PowerShell? dm3281 PowerShell 3 20 Dec 2007