header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
shell.application .copyhere option problems
Last Post 11 Jun 2010 03:51 PM by cameronove. 10 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
BruceUser is Offline
New Member
New Member
Posts:4
Avatar

--
02 Mar 2010 06:48 AM

    Hi,

    I run the following to unzip and want to over write the current files if they exist but the microsoft solution found here(http://msdn.microsoft.com/en-us/lib...5%29.aspx) doesn't seem to work. I haven't found anything that says it works so is there a work around to select the 'yes to all' option when overwriting?

    < code >

    $zipPackage = (new-object -com shell.application).NameSpace($zipfilename)

    $destinationFolder = (new-object -com shell.application).NameSpace($destination)

    $destinationFolder.CopyHere($zipPackage.Items(),16)

    < / code >

    Thanks

    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    02 Mar 2010 07:45 AM
    Try this:

    $zipPackage = (new-object -com shell.application).NameSpace($zipfilename)
    $destinationFolder = (new-object -com shell.application).NameSpace($destination)

    foreach ($item in $zipPackage.Items())
    {
    $destinationFolder.CopyHere($item, 16)
    }
    BruceUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    02 Mar 2010 08:51 AM
    Hi,

    Nope that doesn't work. The 'Yes to All' still pops ups and several times since you are doing it for each item in this case.

    Thanks
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    02 Mar 2010 09:31 AM
    Looking around, I think this is actually a bug. A work-around would be:

    $zipPackage = (new-object -com shell.application).NameSpace($zipfilename)
    $destinationFolder = (new-object -com shell.application).NameSpace($destination)

    foreach ($item in $zipPackage.Items())
    {
    if (!(Test-Path "$destinationFolder\$item")) # If the file doesn't exist in the desination directory...
    {
    $destinationFolder.CopyHere($item)
    }
    else
    {
    # Remove the already-existing item, then copy the new one over
    Remove-Item "$destinationFolder\$item"
    $destinationFolder.CopyHere($item)
    }
    }
    BruceUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    02 Mar 2010 11:32 AM
    Hi,

    The only problem with that is what if their are folders in the zip?  You will skip whole folders or delete folders depending.

    and maybe my test-path is wrong but i can put the test-path in the command line but when I run it from the script it fails.  I print out the variables used in the test path to make sure they are correct also but for some reason I get failure for existing files.  Example
    test-path "$destination\$item.Name"

    Thanks
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    03 Mar 2010 01:15 AM

    OK, now that you've introduced that requirement, we've gotten this far:

    $zipPackage = (New-Object -COM Shell.Application).NameSpace($zipfilename)
    $destinationFolder = (New-Object -COM Shell.Application).NameSpace($destination)

    foreach ($item in $zipPackage.Items())
    {
    $destinationItem = "$destinationFolder\$item"

    $exists = Test-Path -Path $destinationItem

    if (!$exists) # If the item doesn't exist in the desination directory...
    {
    if ($destinationItem.PsIsContainer)
    {
    # If the item DOES NOT exist, and it is a folder, what do we need to do?
    }
    else
    {
    $destinationFolder.CopyHere($item)
    }
    }
    else
    {
    if ($destinationItem.PsIsContainer)
    {
    # If the item DOES exist, and it is a folder, what do we need to do?
    }
    else
    {
    # Remove the already-existing item, then copy the new one over
    Remove-Item -Path $destinationItem
    $destinationFolder.CopyHere($item)
    }
    }
    }

    BruceUser is Offline
    New Member
    New Member
    Posts:4
    Avatar

    --
    03 Mar 2010 11:15 AM
    Hi,

    For the folder that already exists:
    If the folder exists when unziping and you choose not to copy do the items in the folder not copy over or do you have to create another loop to go through those items?

    If the folder doesn't exist you will just do the normal copy.

    Thanks
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    05 Mar 2010 06:16 AM
    Posted By Bruce227 on 03 Mar 2010 11:15 AM
    Hi,

    For the folder that already exists:
    If the folder exists when unziping and you choose not to copy do the items in the folder not copy over or do you have to create another loop to go through those items?

    If the folder doesn't exist you will just do the normal copy.

    Thanks


    If you try to copy a folder over which already exists and you choose not to copy, it will do just that.

    I've made an assumption here, and I think what you're looking for is this?:

    $zipPackage = (New-Object -COM Shell.Application).NameSpace($zipfilename)
    $destinationFolder = (New-Object -COM Shell.Application).NameSpace($destination)

    foreach ($item in $zipPackage.Items())
    {
        $destinationItem = "$destinationFolder\$item"
       
        $exists = Test-Path -Path $destinationItem
       
        if (!$exists) # If the item doesn't exist in the desination directory...
        {
            $destinationFolder.CopyHere($item)
        }
        else
        {
            if ($destinationItem.PsIsContainer) # If we are trying to copy a folder over...
            {
                $childDestinationItems = Get-ChildItem -Path $destinationItem -Recurse # Get child items in the destination folder
                $childItems = Get-ChildItem -Path $item -Recurse # Get child items in the source folder
               
                foreach ($childDestinationItem in $childDestinationItems)
                {
                    foreach ($childItem in $childItems)
                    {
                        $exists = Test-Path -Path "$destinationItem\$childItem"
                        
                        if (!$exists) # If the source child item does not exist, copy it over
                        {
                            $destinationFolder.CopyHere("$item\$childItem")
                        }
                    }
                }
            }
            else
            {
                # Remove the already-existing item, then copy the new one over
                Remove-Item -Path $destinationItem
                $destinationFolder.CopyHere($item)
            }
        }
    }

    This script will not overwite the contents of already-existing items in the destination folder, but it will copy items over if they do not exist in the destination folder.

    PS: I haven't tested this, so there are no guarantees.

    BrandonUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    11 Jun 2010 01:43 AM
    Hello Bruce,

    I know this is an old thread but I thought better late than never lol

    I've been having the same problem. Are you running this under windows XP?

    First off, Try doing this

    [System.Int32]$yestoall = "16"
    [System.Int32]$progressbar = "4"

    then for the Copy here, type

    $destinationFolder.CopyHere($zipPackage.Items(),$yestoall)

    If you are running this in Windows XP, so far from what I've tested and looked this won't work, but this DOES work with Windows 7 (some good news), so there might be hope where this could eventually work with Windows XP as well.

    If anyone else can shed some light on why it doesn't work on XP but works on Windows 7 would be great =D
    BrandonUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    11 Jun 2010 01:50 AM
    After seeing this I decided to search again

    Found the answer to why it's not working

    http://www.eggheadcafe.com/software...ancel.aspx

    Pretty much the CopyHere options are meant for normal folder structures, not Zip files (at least in Windows Xp)

    So it doesn't seem it will be supported without a third party app like Winzip or 7Zip
    cameronoveUser is Offline
    Basic Member
    Basic Member
    Posts:332
    Avatar

    --
    11 Jun 2010 03:51 PM
    Hey check out this:

    This is a managed project on codeplex
    http://dotnetzip.codeplex.com/

    Here are the examples in PowerShell they provide.
    http://dotnetzip.codeplex.com/wikipage?title=PS-Examples&referringTitle=Examples
    #Examples for using DotNetZip from within Powershell scripts            
    #http://dotnetzip.codeplex.com/wikipage?title=PS-Examples&referringTitle=Examples

    #Read a Zip file from within Powershell
    #Be sure to call Dispose() at the end or the zipfile will remain open (locked).
    $SourceFile = "c:\files\filename.zip"
    [System.Reflection.Assembly]::LoadFrom("c:\Files\Ionic.Zip.dll")
    $zipfile = [Ionic.Zip.ZipFile]::Read($sourceFile)
    foreach($file in $zipfile)
    {$file.FileName}
    $zipfile.Dispose()

    #Zip up a folder from within Powershell - zip an entire directory
    [System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll");

    $directoryToZip = "c:\\temp"
    $zipfile = new-object Ionic.Zip.ZipFile
    $e = $zipfile.AddDirectory($directoryToZip, "home")
    $zipfile.Save("ZipFiles.ps1.out.zip")
    $zipfile.Dispose()

    #Create an AES-encrypted zipfile from within Powershell
    [System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll");

    $directoryToZip = "c:\\temp"
    $zipfile = new-object Ionic.Zip.ZipFile
    $zipfile.Encryption = [Ionic.Zip.EncryptionAlgorithm]::WinZipAes256
    $zipfile.Password = "Albatros$"
    $e= $zipfile.AddDirectory($directoryToZip, "home")
    $zipfile.Save("ZipFiles.ps1.out.zip")
    $zipfile.Dispose()

    #Create a zipfile, and include content from a string -
    #This sample shows the AddEntry() method, which adds an entry to the zip archive,
    #using content from a string.

    [System.Reflection.Assembly]::LoadFrom("c:\\dinoch\\bin\\Ionic.Zip.dll");

    $directoryToZip = "c:\\home"
    $zipfile = new-object Ionic.Zip.ZipFile
    $e= $zipfile.AddEntry("Readme.txt", "", "This content will appear in a file, within the zip")
    $e= $zipfile.AddDirectory($directoryToZip, "home")
    $zipfile.Save("ZipFiles.ps1.out.zip")
    $zipfile.Dispose()


    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer